A Discrete-Event Network Simulator
API
building-list.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Jaume Nin <jaume.nin@cttc,cat>
18  * Based on BuildingList implementation by Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  *
20  */
21 #include "building-list.h"
22 
23 #include "building.h"
24 
25 #include "ns3/assert.h"
26 #include "ns3/config.h"
27 #include "ns3/log.h"
28 #include "ns3/object-vector.h"
29 #include "ns3/simulator.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("BuildingList");
35 
39 class BuildingListPriv : public Object
40 {
41  public:
46  static TypeId GetTypeId();
48  ~BuildingListPriv() override;
49 
56  uint32_t Add(Ptr<Building> building);
74  Ptr<Building> GetBuilding(uint32_t n);
79  uint32_t GetNBuildings();
80 
85  static Ptr<BuildingListPriv> Get();
86 
87  private:
88  void DoDispose() override;
93  static Ptr<BuildingListPriv>* DoGet();
100  static void Delete();
101  std::vector<Ptr<Building>> m_buildings;
102 };
103 
105 
106 TypeId
108 {
109  static TypeId tid =
110  TypeId("ns3::BuildingListPriv")
111  .SetParent<Object>()
112  .SetGroupName("Buildings")
113  .AddAttribute("BuildingList",
114  "The list of all buildings created during the simulation.",
117  MakeObjectVectorChecker<Building>());
118  return tid;
119 }
120 
123 {
124  return *DoGet();
125 }
126 
129 {
130  static Ptr<BuildingListPriv> ptr = nullptr;
131  if (!ptr)
132  {
133  ptr = CreateObject<BuildingListPriv>();
136  }
137  return &ptr;
138 }
139 
140 void
142 {
145  (*DoGet()) = nullptr;
146 }
147 
149 {
151 }
152 
154 {
155 }
156 
157 void
159 {
161  for (auto i = m_buildings.begin(); i != m_buildings.end(); i++)
162  {
163  Ptr<Building> building = *i;
164  building->Dispose();
165  *i = nullptr;
166  }
167  m_buildings.erase(m_buildings.begin(), m_buildings.end());
169 }
170 
171 uint32_t
173 {
174  uint32_t index = m_buildings.size();
175  m_buildings.push_back(building);
176  Simulator::ScheduleWithContext(index, TimeStep(0), &Building::Initialize, building);
177  return index;
178 }
179 
182 {
183  return m_buildings.begin();
184 }
185 
188 {
189  return m_buildings.end();
190 }
191 
192 uint32_t
194 {
195  return m_buildings.size();
196 }
197 
200 {
201  NS_ASSERT_MSG(n < m_buildings.size(),
202  "Building index " << n << " is out of range (only have " << m_buildings.size()
203  << " buildings).");
204  return m_buildings.at(n);
205 }
206 
207 } // namespace ns3
208 
214 namespace ns3
215 {
216 
217 uint32_t
219 {
220  return BuildingListPriv::Get()->Add(building);
221 }
222 
225 {
226  return BuildingListPriv::Get()->Begin();
227 }
228 
231 {
232  return BuildingListPriv::Get()->End();
233 }
234 
237 {
238  return BuildingListPriv::Get()->GetBuilding(n);
239 }
240 
241 uint32_t
243 {
244  return BuildingListPriv::Get()->GetNBuildings();
245 }
246 
247 } // namespace ns3
std::vector< Ptr< Building > >::const_iterator Iterator
Const Iterator.
Definition: building-list.h:41
static Ptr< Building > GetBuilding(uint32_t n)
static uint32_t GetNBuildings()
static Iterator End()
static uint32_t Add(Ptr< Building > building)
static Iterator Begin()
private implementation detail of the BuildingList API.
std::vector< Ptr< Building > > m_buildings
Container of Building.
void DoDispose() override
Destructor implementation.
Ptr< Building > GetBuilding(uint32_t n)
Gets the n-th Building in the container.
uint32_t GetNBuildings()
Gets the number of Building in the container.
~BuildingListPriv() override
static TypeId GetTypeId()
Get the type ID.
uint32_t Add(Ptr< Building > building)
Add a Building to the list.
static Ptr< BuildingListPriv > Get()
Get the Singleton instance of BuildingListPriv (or create one)
BuildingList::Iterator Begin() const
Returns an iterator to the start of the list.
static Ptr< BuildingListPriv > * DoGet()
Get the Singleton instance of BuildingListPriv (or create one)
static void Delete()
Dispose the Singleton instance of BuildingListPriv.
BuildingList::Iterator End() const
Returns an iterator to the end of the list.
A base class which provides memory management and object aggregation.
Definition: object.h:89
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition: simulator.h:622
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:86
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1012
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1005
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ObjectPtrContainerValue ObjectVectorValue
ObjectVectorValue is an alias for ObjectPtrContainerValue.
Definition: object-vector.h:40
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:76