00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Authors: 00019 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>, 00020 */ 00021 00022 #include "ns3/simulator.h" 00023 #include "ns3/object-vector.h" 00024 #include "ns3/config.h" 00025 #include "ns3/log.h" 00026 #include "ns3/assert.h" 00027 #include "node-list.h" 00028 #include "node.h" 00029 00030 namespace ns3 { 00031 00032 NS_LOG_COMPONENT_DEFINE ("NodeList"); 00033 00034 /** 00035 * \brief private implementation detail of the NodeList API. 00036 */ 00037 class NodeListPriv : public Object 00038 { 00039 public: 00040 static TypeId GetTypeId (void); 00041 NodeListPriv (); 00042 ~NodeListPriv (); 00043 00044 uint32_t Add (Ptr<Node> node); 00045 NodeList::Iterator Begin (void) const; 00046 NodeList::Iterator End (void) const; 00047 Ptr<Node> GetNode (uint32_t n); 00048 uint32_t GetNNodes (void); 00049 00050 static Ptr<NodeListPriv> Get (void); 00051 00052 private: 00053 static Ptr<NodeListPriv> *DoGet (void); 00054 static void Delete (void); 00055 std::vector<Ptr<Node> > m_nodes; 00056 }; 00057 00058 NS_OBJECT_ENSURE_REGISTERED (NodeListPriv); 00059 00060 TypeId 00061 NodeListPriv::GetTypeId (void) 00062 { 00063 static TypeId tid = TypeId ("ns3::NodeListPriv") 00064 .SetParent<Object> () 00065 .AddAttribute ("NodeList", "The list of all nodes created during the simulation.", 00066 ObjectVectorValue (), 00067 MakeObjectVectorAccessor (&NodeListPriv::m_nodes), 00068 MakeObjectVectorChecker<Node> ()) 00069 ; 00070 return tid; 00071 } 00072 00073 Ptr<NodeListPriv> 00074 NodeListPriv::Get (void) 00075 { 00076 return *DoGet (); 00077 } 00078 Ptr<NodeListPriv> * 00079 NodeListPriv::DoGet (void) 00080 { 00081 static Ptr<NodeListPriv> ptr = 0; 00082 if (ptr == 0) 00083 { 00084 ptr = CreateObject<NodeListPriv> (); 00085 Config::RegisterRootNamespaceObject (ptr); 00086 Simulator::ScheduleDestroy (&NodeListPriv::Delete); 00087 } 00088 return &ptr; 00089 } 00090 void 00091 NodeListPriv::Delete (void) 00092 { 00093 NS_LOG_FUNCTION_NOARGS (); 00094 Config::UnregisterRootNamespaceObject (Get ()); 00095 (*DoGet ()) = 0; 00096 } 00097 00098 00099 NodeListPriv::NodeListPriv () 00100 { 00101 NS_LOG_FUNCTION_NOARGS (); 00102 } 00103 NodeListPriv::~NodeListPriv () 00104 { 00105 NS_LOG_FUNCTION_NOARGS (); 00106 for (std::vector<Ptr<Node> >::iterator i = m_nodes.begin (); 00107 i != m_nodes.end (); i++) 00108 { 00109 Ptr<Node> node = *i; 00110 node->Dispose (); 00111 *i = 0; 00112 } 00113 m_nodes.erase (m_nodes.begin (), m_nodes.end ()); 00114 } 00115 00116 00117 uint32_t 00118 NodeListPriv::Add (Ptr<Node> node) 00119 { 00120 uint32_t index = m_nodes.size (); 00121 m_nodes.push_back (node); 00122 return index; 00123 00124 } 00125 NodeList::Iterator 00126 NodeListPriv::Begin (void) const 00127 { 00128 return m_nodes.begin (); 00129 } 00130 NodeList::Iterator 00131 NodeListPriv::End (void) const 00132 { 00133 return m_nodes.end (); 00134 } 00135 uint32_t 00136 NodeListPriv::GetNNodes (void) 00137 { 00138 return m_nodes.size (); 00139 } 00140 00141 Ptr<Node> 00142 NodeListPriv::GetNode (uint32_t n) 00143 { 00144 NS_ASSERT_MSG (n < m_nodes.size (), "Node index " << n << 00145 " is out of range (only have " << m_nodes.size () << " nodes)."); 00146 return m_nodes[n]; 00147 } 00148 00149 } 00150 00151 /** 00152 * The implementation of the public static-based API 00153 * which calls into the private implementation through 00154 * the simulation singleton. 00155 */ 00156 namespace ns3 { 00157 00158 uint32_t 00159 NodeList::Add (Ptr<Node> node) 00160 { 00161 return NodeListPriv::Get ()->Add (node); 00162 } 00163 NodeList::Iterator 00164 NodeList::Begin (void) 00165 { 00166 return NodeListPriv::Get ()->Begin (); 00167 } 00168 NodeList::Iterator 00169 NodeList::End (void) 00170 { 00171 return NodeListPriv::Get ()->End (); 00172 } 00173 Ptr<Node> 00174 NodeList::GetNode (uint32_t n) 00175 { 00176 return NodeListPriv::Get ()->GetNode (n); 00177 } 00178 uint32_t 00179 NodeList::GetNNodes (void) 00180 { 00181 return NodeListPriv::Get ()->GetNNodes (); 00182 } 00183 00184 }//namespace ns3