From c5eb1536ae59ae4a75720b44daac0d9654dcdfba Mon Sep 17 00:00:00 2001 From: Jaume Nin Date: Fri, 9 Dec 2011 15:16:16 +0100 Subject: [PATCH] Building list implemented --- src/buildings/model/building-list.cc | 192 +++++++++++++++++++++++++++ src/buildings/model/building-list.h | 71 ++++++++++ src/buildings/model/building.cc | 40 +++++- src/buildings/model/building.h | 12 +- src/buildings/wscript | 2 + 5 files changed, 312 insertions(+), 5 deletions(-) create mode 100644 src/buildings/model/building-list.cc create mode 100644 src/buildings/model/building-list.h diff --git a/src/buildings/model/building-list.cc b/src/buildings/model/building-list.cc new file mode 100644 index 000000000..9e469341c --- /dev/null +++ b/src/buildings/model/building-list.cc @@ -0,0 +1,192 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + * Based on BuildingList implemenation by Mathieu Lacage + * + */ +#include "building-list.h" +#include "ns3/simulator.h" +#include "ns3/object-vector.h" +#include "ns3/config.h" +#include "ns3/log.h" +#include "ns3/assert.h" +#include "building-list.h" +#include "building.h" + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("BuildingList"); + +/** + * \brief private implementation detail of the BuildingList API. + */ +class BuildingListPriv : public Object +{ +public: + static TypeId GetTypeId (void); + BuildingListPriv (); + ~BuildingListPriv (); + + uint32_t Add (Ptr building); + BuildingList::Iterator Begin (void) const; + BuildingList::Iterator End (void) const; + Ptr GetBuilding (uint32_t n); + uint32_t GetNBuildings (void); + + static Ptr Get (void); + +private: + virtual void DoDispose (void); + static Ptr *DoGet (void); + static void Delete (void); + std::vector > m_buildings; +}; + +NS_OBJECT_ENSURE_REGISTERED (BuildingListPriv); + +TypeId +BuildingListPriv::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::BuildingListPriv") + .SetParent () + .AddAttribute ("BuildingList", "The list of all buildings created during the simulation.", + ObjectVectorValue (), + MakeObjectVectorAccessor (&BuildingListPriv::m_buildings), + MakeObjectVectorChecker ()) + ; + return tid; +} + +Ptr +BuildingListPriv::Get (void) +{ + return *DoGet (); +} +Ptr * +BuildingListPriv::DoGet (void) +{ + static Ptr ptr = 0; + if (ptr == 0) + { + ptr = CreateObject (); + Config::RegisterRootNamespaceObject (ptr); + Simulator::ScheduleDestroy (&BuildingListPriv::Delete); + } + return &ptr; +} +void +BuildingListPriv::Delete (void) +{ + NS_LOG_FUNCTION_NOARGS (); + Config::UnregisterRootNamespaceObject (Get ()); + (*DoGet ()) = 0; +} + + +BuildingListPriv::BuildingListPriv () +{ + NS_LOG_FUNCTION_NOARGS (); +} +BuildingListPriv::~BuildingListPriv () +{ +} +void +BuildingListPriv::DoDispose (void) +{ + NS_LOG_FUNCTION_NOARGS (); + for (std::vector >::iterator i = m_buildings.begin (); + i != m_buildings.end (); i++) + { + Ptr building = *i; + building->Dispose (); + *i = 0; + } + m_buildings.erase (m_buildings.begin (), m_buildings.end ()); + Object::DoDispose (); +} + + +uint32_t +BuildingListPriv::Add (Ptr building) +{ + uint32_t index = m_buildings.size (); + m_buildings.push_back (building); + Simulator::ScheduleWithContext (index, TimeStep (0), &Building::Start, building); + return index; + +} +BuildingList::Iterator +BuildingListPriv::Begin (void) const +{ + return m_buildings.begin (); +} +BuildingList::Iterator +BuildingListPriv::End (void) const +{ + return m_buildings.end (); +} +uint32_t +BuildingListPriv::GetNBuildings (void) +{ + return m_buildings.size (); +} + +Ptr +BuildingListPriv::GetBuilding (uint32_t n) +{ + NS_ASSERT_MSG (n < m_buildings.size (), "Building index " << n << + " is out of range (only have " << m_buildings.size () << " buildings)."); + return m_buildings[n]; +} + +} + +/** + * The implementation of the public static-based API + * which calls into the private implementation through + * the simulation singleton. + */ +namespace ns3 { + +uint32_t +BuildingList::Add (Ptr building) +{ + return BuildingListPriv::Get ()->Add (building); +} +BuildingList::Iterator +BuildingList::Begin (void) +{ + return BuildingListPriv::Get ()->Begin (); +} +BuildingList::Iterator +BuildingList::End (void) +{ + return BuildingListPriv::Get ()->End (); +} +Ptr +BuildingList::GetBuilding (uint32_t n) +{ + return BuildingListPriv::Get ()->GetBuilding (n); +} +uint32_t +BuildingList::GetNBuildings (void) +{ + return BuildingListPriv::Get ()->GetNBuildings (); +} + +} // namespace ns3 diff --git a/src/buildings/model/building-list.h b/src/buildings/model/building-list.h new file mode 100644 index 000000000..a1989f1e0 --- /dev/null +++ b/src/buildings/model/building-list.h @@ -0,0 +1,71 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Jaume Nin + * Based on NodeList implemenation by Mathieu Lacage + * + */ + +#ifndef BUILDING_LIST_H_ +#define BUILDING_LIST_H_ + +#include +#include "ns3/ptr.h" + +namespace ns3 { + +class Building; + +class BuildingList +{ +public: + typedef std::vector< Ptr >::const_iterator Iterator; + + /** + * \param building building to add + * \returns index of building in list. + * + * This method is called automatically from Building::Building so + * the user has little reason to call it himself. + */ + static uint32_t Add (Ptr building); + /** + * \returns a C++ iterator located at the beginning of this + * list. + */ + static Iterator Begin (void); + /** + * \returns a C++ iterator located at the end of this + * list. + */ + static Iterator End (void); + /** + * \param n index of requested building. + * \returns the Building associated to index n. + */ + static Ptr GetBuilding (uint32_t n); + /** + * \returns the number of buildings currently in the list. + */ + static uint32_t GetNBuildings (void); +}; + +} // namespace ns3 + +#endif /* BUILDING_LIST_H_ */ + + diff --git a/src/buildings/model/building.cc b/src/buildings/model/building.cc index f1a9f46d0..f125ceee1 100644 --- a/src/buildings/model/building.cc +++ b/src/buildings/model/building.cc @@ -21,12 +21,43 @@ #include +#include #include - +#include "ns3/uinteger.h" namespace ns3 { +TypeId +Building::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Building") + .SetParent () + .AddConstructor () + .AddAttribute ("roomX", "The number of rooms in the X axis.", + TypeId::ATTR_GET, // allow only getting it. + UintegerValue (4), + MakeUintegerAccessor (&Building::m_roomX), + MakeUintegerChecker ()) + .AddAttribute ("roomY", "The number of rooms in the Y axis.", + TypeId::ATTR_GET, // allow only getting it. + UintegerValue (1), + MakeUintegerAccessor (&Building::m_roomY), + MakeUintegerChecker ()) + .AddAttribute ("nFloor", "The number of floors of this building.", + TypeId::ATTR_GET, // allow only getting it. + UintegerValue (1), + MakeUintegerAccessor (&Building::m_floor), + MakeUintegerChecker ()) + .AddAttribute ("Id", "The id (unique integer) of this Building.", + TypeId::ATTR_GET, // allow only getting it. + UintegerValue (0), + MakeUintegerAccessor (&Building::m_buildingId), + MakeUintegerChecker ()) + ; + return tid; +} + Building::Building (double _xMin, double _xMax, double _yMin, double _yMax, double _zMin, double _zMax/*, @@ -39,6 +70,7 @@ Building::Building (double _xMin, double _xMax, { m_buldingBounds = Box (_xMin, _xMax, _yMin, _yMax, _zMin, _zMax); + Construct(); } @@ -50,8 +82,14 @@ Building::Building () m_externalWalls (ConcreteWithWindows) { m_buldingBounds = Box (); + Construct(); } +void +Building::Construct () +{ + m_buildingId = BuildingList::Add(this); +} void Building::SetBuildingType (Building::BuildingType_t t) diff --git a/src/buildings/model/building.h b/src/buildings/model/building.h index e24cd4993..6a6336b5d 100644 --- a/src/buildings/model/building.h +++ b/src/buildings/model/building.h @@ -26,7 +26,7 @@ #include #include #include - +#include namespace ns3 { @@ -34,9 +34,11 @@ namespace ns3 { * \ingroup mobility * \brief a 3d building block */ -class Building : public SimpleRefCount +class Building : public Object { public: + static TypeId GetTypeId (void); + enum BuildingType_t { Residential, Office, Commercial @@ -69,7 +71,6 @@ public: */ Building (); - /** * \param t the type of building (i.e., Residential, Office, Commercial) * @@ -150,6 +151,9 @@ public: private: + + void Construct (); + Box m_buldingBounds; /** * number of floors must be greater then 0 and 1 means only one floor @@ -159,7 +163,7 @@ private: uint8_t m_roomX; uint8_t m_roomY; - uint8_t m_buildingId; + uint32_t m_buildingId; BuildingType_t m_buildingType; ExtWallsType_t m_externalWalls; diff --git a/src/buildings/wscript b/src/buildings/wscript index 6ccb7d488..608b79ba8 100644 --- a/src/buildings/wscript +++ b/src/buildings/wscript @@ -5,6 +5,7 @@ def build(bld): module = bld.create_ns3_module('buildings', ['mobility', 'propagation']) module.source = [ 'model/building.cc', + 'model/building-list.cc', 'model/buildings-mobility-model.cc', 'model/buildings-propagation-loss-model.cc', ] @@ -19,6 +20,7 @@ def build(bld): headers.module = 'buildings' headers.source = [ 'model/building.h', + 'model/building-list.h', 'model/buildings-mobility-model.h', 'model/buildings-propagation-loss-model.h', 'test/buildings-pathloss-test.h',