A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
building-list.cc
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);
43  ~BuildingListPriv ();
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 
60 NS_OBJECT_ENSURE_REGISTERED (BuildingListPriv);
61 
62 TypeId
63 BuildingListPriv::GetTypeId (void)
64 {
65  static TypeId tid = TypeId ("ns3::BuildingListPriv")
66  .SetParent<Object> ()
67  .AddAttribute ("BuildingList", "The list of all buildings created during the simulation.",
69  MakeObjectVectorAccessor (&BuildingListPriv::m_buildings),
70  MakeObjectVectorChecker<Building> ())
71  ;
72  return tid;
73 }
74 
75 Ptr<BuildingListPriv>
76 BuildingListPriv::Get (void)
77 {
78  return *DoGet ();
79 }
80 Ptr<BuildingListPriv> *
81 BuildingListPriv::DoGet (void)
82 {
83  static Ptr<BuildingListPriv> ptr = 0;
84  if (ptr == 0)
85  {
86  ptr = CreateObject<BuildingListPriv> ();
88  Simulator::ScheduleDestroy (&BuildingListPriv::Delete);
89  }
90  return &ptr;
91 }
92 void
93 BuildingListPriv::Delete (void)
94 {
97  (*DoGet ()) = 0;
98 }
99 
100 
101 BuildingListPriv::BuildingListPriv ()
102 {
104 }
105 BuildingListPriv::~BuildingListPriv ()
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
125 BuildingListPriv::Add (Ptr<Building> building)
126 {
127  uint32_t index = m_buildings.size ();
128  m_buildings.push_back (building);
129  Simulator::ScheduleWithContext (index, TimeStep (0), &Building::Initialize, building);
130  return index;
131 
132 }
133 BuildingList::Iterator
134 BuildingListPriv::Begin (void) const
135 {
136  return m_buildings.begin ();
137 }
138 BuildingList::Iterator
139 BuildingListPriv::End (void) const
140 {
141  return m_buildings.end ();
142 }
143 uint32_t
144 BuildingListPriv::GetNBuildings (void)
145 {
146  return m_buildings.size ();
147 }
148 
149 Ptr<Building>
150 BuildingListPriv::GetBuilding (uint32_t n)
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 }
171 BuildingList::Iterator
173 {
174  return BuildingListPriv::Get ()->Begin ();
175 }
176 BuildingList::Iterator
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
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
static uint32_t GetNBuildings(void)
static Ptr< Building > GetBuilding(uint32_t n)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
private implementation detail of the BuildingList API.
virtual void DoDispose(void)
Definition: object.cc:335
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:751
static Iterator End(void)
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
static Iterator Begin(void)
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:900
virtual void DoDispose(void)
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:745
static uint32_t Add(Ptr< Building > building)
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
void Initialize(void)
Definition: object.cc:179
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Definition: simulator.h:1072
a base class which provides memory management and object aggregation
Definition: object.h:63
contain a set of ns3::Object pointers.
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471