Files
unison/src/node/node-list.cc

185 lines
4.0 KiB
C++
Raw Normal View History

/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>,
*/
#include "ns3/simulator.h"
2008-02-26 18:17:29 +01:00
#include "ns3/object-vector.h"
#include "ns3/config.h"
2008-04-09 10:01:16 -07:00
#include "ns3/log.h"
#include "ns3/assert.h"
#include "node-list.h"
2007-06-04 16:21:05 +02:00
#include "node.h"
namespace ns3 {
2008-04-09 10:01:16 -07:00
NS_LOG_COMPONENT_DEFINE ("NodeList");
/**
* \brief private implementation detail of the NodeList API.
*/
2008-02-26 18:17:29 +01:00
class NodeListPriv : public Object
{
public:
2008-02-26 18:17:29 +01:00
static TypeId GetTypeId (void);
NodeListPriv ();
~NodeListPriv ();
2007-06-04 16:17:01 +02:00
uint32_t Add (Ptr<Node> node);
2007-08-28 14:33:53 +02:00
NodeList::Iterator Begin (void) const;
NodeList::Iterator End (void) const;
2007-06-04 16:17:01 +02:00
Ptr<Node> GetNode (uint32_t n);
uint32_t GetNNodes (void);
2008-02-26 18:17:29 +01:00
static Ptr<NodeListPriv> Get (void);
private:
2008-02-26 18:17:29 +01:00
static Ptr<NodeListPriv> *DoGet (void);
static void Delete (void);
2007-06-04 16:17:01 +02:00
std::vector<Ptr<Node> > m_nodes;
};
NS_OBJECT_ENSURE_REGISTERED (NodeListPriv);
2008-02-26 18:17:29 +01:00
TypeId
NodeListPriv::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::NodeListPriv")
2008-02-26 18:17:29 +01:00
.SetParent<Object> ()
.AddAttribute ("NodeList", "The list of all nodes created during the simulation.",
ObjectVectorValue (),
2008-02-26 18:17:29 +01:00
MakeObjectVectorAccessor (&NodeListPriv::m_nodes),
2008-04-09 14:58:52 -07:00
MakeObjectVectorChecker<Node> ())
2008-02-26 18:17:29 +01:00
;
return tid;
}
Ptr<NodeListPriv>
NodeListPriv::Get (void)
{
return *DoGet ();
}
Ptr<NodeListPriv> *
NodeListPriv::DoGet (void)
{
static Ptr<NodeListPriv> ptr = 0;
if (ptr == 0)
{
ptr = CreateObject<NodeListPriv> ();
Config::RegisterRootNamespaceObject (ptr);
2008-02-26 18:17:29 +01:00
Simulator::ScheduleDestroy (&NodeListPriv::Delete);
}
return &ptr;
}
void
NodeListPriv::Delete (void)
{
NS_LOG_FUNCTION_NOARGS ();
Config::UnregisterRootNamespaceObject (Get ());
2008-02-26 18:17:29 +01:00
(*DoGet ()) = 0;
}
NodeListPriv::NodeListPriv ()
2008-04-09 10:01:16 -07:00
{
NS_LOG_FUNCTION_NOARGS ();
2008-04-09 10:01:16 -07:00
}
NodeListPriv::~NodeListPriv ()
{
NS_LOG_FUNCTION_NOARGS ();
2007-06-04 16:17:01 +02:00
for (std::vector<Ptr<Node> >::iterator i = m_nodes.begin ();
i != m_nodes.end (); i++)
{
2007-06-04 16:17:01 +02:00
Ptr<Node> node = *i;
node->Dispose ();
*i = 0;
}
m_nodes.erase (m_nodes.begin (), m_nodes.end ());
}
uint32_t
2007-06-04 16:17:01 +02:00
NodeListPriv::Add (Ptr<Node> node)
{
uint32_t index = m_nodes.size ();
m_nodes.push_back (node);
return index;
}
NodeList::Iterator
2007-08-28 14:33:53 +02:00
NodeListPriv::Begin (void) const
{
return m_nodes.begin ();
}
NodeList::Iterator
2007-08-28 14:33:53 +02:00
NodeListPriv::End (void) const
{
return m_nodes.end ();
}
uint32_t
2007-06-04 16:17:01 +02:00
NodeListPriv::GetNNodes (void)
{
return m_nodes.size ();
}
2007-06-04 16:17:01 +02:00
Ptr<Node>
NodeListPriv::GetNode (uint32_t n)
{
NS_ASSERT_MSG (n < m_nodes.size (), "Node index " << n <<
" is out of range (only have " << m_nodes.size () << " nodes).");
return m_nodes[n];
}
}
/**
* The implementation of the public static-based API
* which calls into the private implementation through
* the simulation singleton.
*/
namespace ns3 {
uint32_t
2007-06-04 16:17:01 +02:00
NodeList::Add (Ptr<Node> node)
{
2008-02-26 18:17:29 +01:00
return NodeListPriv::Get ()->Add (node);
}
NodeList::Iterator
NodeList::Begin (void)
{
2008-02-26 18:17:29 +01:00
return NodeListPriv::Get ()->Begin ();
}
NodeList::Iterator
NodeList::End (void)
{
2008-02-26 18:17:29 +01:00
return NodeListPriv::Get ()->End ();
}
2007-06-04 16:17:01 +02:00
Ptr<Node>
NodeList::GetNode (uint32_t n)
{
2008-02-26 18:17:29 +01:00
return NodeListPriv::Get ()->GetNode (n);
}
2008-06-18 21:17:56 -07:00
uint32_t
NodeList::GetNNodes (void)
{
return NodeListPriv::Get ()->GetNNodes ();
}
}//namespace ns3