A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("BuildingList");
35
36/**
37 * \brief private implementation detail of the BuildingList API.
38 */
40{
41 public:
42 /**
43 * \brief Get the type ID.
44 * \return The object TypeId.
45 */
46 static TypeId GetTypeId();
48 ~BuildingListPriv() override;
49
50 /**
51 * Add a Building to the list.
52 *
53 * \param building building to add
54 * \returns index of building in list.
55 */
56 uint32_t Add(Ptr<Building> building);
57 /**
58 * Returns an iterator to the start of the list.
59 *
60 * \returns iterator to the begin of the container.
61 */
63 /**
64 * Returns an iterator to the end of the list.
65 *
66 * \returns iterator to the end of the container.
67 */
69 /**
70 * Gets the n-th Building in the container
71 * \param n Building position
72 * \returns a pointer to the Building
73 */
75 /**
76 * Gets the number of Building in the container
77 * \returns the container size
78 */
80
81 /**
82 * Get the Singleton instance of BuildingListPriv (or create one)
83 * \return the BuildingListPriv instance
84 */
86
87 private:
88 void DoDispose() override;
89 /**
90 * Get the Singleton instance of BuildingListPriv (or create one)
91 * \return the BuildingListPriv instance
92 */
94 /**
95 * Dispose the Singleton instance of BuildingListPriv.
96 *
97 * \note: this function is automatically called at the simulation end.
98 *
99 */
100 static void Delete();
101 std::vector<Ptr<Building>> m_buildings; //!< Container of Building
102};
103
105
106TypeId
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
140void
142{
145 (*DoGet()) = nullptr;
146}
147
149{
151}
152
154{
155}
156
157void
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
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
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
209/**
210 * The implementation of the public static-based API
211 * which calls into the private implementation through
212 * the simulation singleton.
213 */
214namespace ns3
215{
216
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
243{
244 return BuildingListPriv::Get()->GetNBuildings();
245}
246
247} // namespace ns3
static Ptr< Building > GetBuilding(uint32_t n)
std::vector< Ptr< Building > >::const_iterator Iterator
Const Iterator.
Definition: building-list.h:43
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:214
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
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:932
#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
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
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1016
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1009
#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.