A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
building-list.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Jaume Nin <jaume.nin@cttc,cat>
19  * Based on BuildingList implemenation by Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  *
21  */
22 #include "building-list.h"
23 #include "ns3/simulator.h"
24 #include "ns3/object-vector.h"
25 #include "ns3/config.h"
26 #include "ns3/log.h"
27 #include "ns3/assert.h"
28 #include "building-list.h"
29 #include "building.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("BuildingList");
34 
38 class BuildingListPriv : public Object
39 {
40 public:
41  static TypeId GetTypeId (void);
44 
45  uint32_t Add (Ptr<Building> building);
46  BuildingList::Iterator Begin (void) const;
47  BuildingList::Iterator End (void) const;
48  Ptr<Building> GetBuilding (uint32_t n);
49  uint32_t GetNBuildings (void);
50 
51  static Ptr<BuildingListPriv> Get (void);
52 
53 private:
54  virtual void DoDispose (void);
55  static Ptr<BuildingListPriv> *DoGet (void);
56  static void Delete (void);
57  std::vector<Ptr<Building> > m_buildings;
58 };
59 
61 
62 TypeId
64 {
65  static TypeId tid = TypeId ("ns3::BuildingListPriv")
66  .SetParent<Object> ()
67  .AddAttribute ("BuildingList", "The list of all buildings created during the simulation.",
70  MakeObjectVectorChecker<Building> ())
71  ;
72  return tid;
73 }
74 
77 {
78  return *DoGet ();
79 }
82 {
83  static Ptr<BuildingListPriv> ptr = 0;
84  if (ptr == 0)
85  {
86  ptr = CreateObject<BuildingListPriv> ();
89  }
90  return &ptr;
91 }
92 void
94 {
97  (*DoGet ()) = 0;
98 }
99 
100 
102 {
104 }
106 {
107 }
108 void
110 {
112  for (std::vector<Ptr<Building> >::iterator i = m_buildings.begin ();
113  i != m_buildings.end (); i++)
114  {
115  Ptr<Building> building = *i;
116  building->Dispose ();
117  *i = 0;
118  }
119  m_buildings.erase (m_buildings.begin (), m_buildings.end ());
121 }
122 
123 
124 uint32_t
126 {
127  uint32_t index = m_buildings.size ();
128  m_buildings.push_back (building);
130  return index;
131 
132 }
135 {
136  return m_buildings.begin ();
137 }
140 {
141  return m_buildings.end ();
142 }
143 uint32_t
145 {
146  return m_buildings.size ();
147 }
148 
151 {
152  NS_ASSERT_MSG (n < m_buildings.size (), "Building index " << n <<
153  " is out of range (only have " << m_buildings.size () << " buildings).");
154  return m_buildings.at (n);
155 }
156 
157 }
158 
164 namespace ns3 {
165 
166 uint32_t
168 {
169  return BuildingListPriv::Get ()->Add (building);
170 }
173 {
174  return BuildingListPriv::Get ()->Begin ();
175 }
178 {
179  return BuildingListPriv::Get ()->End ();
180 }
183 {
184  return BuildingListPriv::Get ()->GetBuilding (n);
185 }
186 uint32_t
188 {
189  return BuildingListPriv::Get ()->GetNBuildings ();
190 }
191 
192 } // namespace ns3
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
Definition: object-vector.h:51
static Ptr< BuildingListPriv > Get(void)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
static TypeId GetTypeId(void)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
static uint32_t GetNBuildings(void)
Time TimeStep(uint64_t ts)
Definition: nstime.h:997
static Ptr< Building > GetBuilding(uint32_t n)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
private implementation detail of the BuildingList API.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:751
static Iterator End(void)
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static Iterator Begin(void)
uint32_t Add(Ptr< Building > building)
static void Delete(void)
BuildingList::Iterator End(void) const
std::vector< Ptr< Building > >::const_iterator Iterator
Definition: building-list.h:36
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:905
Ptr< Building > GetBuilding(uint32_t n)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:745
static uint32_t Add(Ptr< Building > building)
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:179
uint32_t GetNBuildings(void)
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Schedule an event to expire at Destroy time.
Definition: simulator.h:1077
a base class which provides memory management and object aggregation
Definition: object.h:64
contain a set of ns3::Object pointers.
std::vector< Ptr< Building > > m_buildings
static Ptr< BuildingListPriv > * DoGet(void)
BuildingList::Iterator Begin(void) const
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610