NamedObjectCont< T > Class Template Reference

#include <NamedObjectCont.h>


Detailed Description

template<class T>
class NamedObjectCont< T >

A map of named object pointers.

An associative storage (map) for objects (pointers to them to be exact), which do have a name. In order to get the stored objects as a list, each insertion/deletion sets the internal state value "myHaveChanged" to true, indicating the list must be rebuild.

Definition at line 51 of file NamedObjectCont.h.


Public Member Functions

virtual bool add (const std::string &id, T item) throw ()
 Adds an item.
const std::vector< T > & buildAndGetStaticVector () const throw ()
void clear () throw ()
 Removes all items from the container (deletes them, too).
bool erase (const std::string &id) throw ()
 Removes the named item from the container.
get (const std::string &id) const throw ()
 Retrieves an item.
const std::map< std::string, T > & getMyMap () const throw ()
std::vector< T > getTempVector () const throw ()
void insertIDs (std::vector< std::string > &into) const throw ()
 NamedObjectCont () throw ()
 Constructor.
virtual bool remove (const std::string &id) throw ()
 Removes an item.
unsigned int size () const throw ()
 Returns the number of items within the container.
virtual ~NamedObjectCont () throw ()
 Destructor.

Private Types

typedef std::map< std::string, T > IDMap
 Definition of the key to pointer map type.
typedef IDMap::iterator myContIt
 Definition of the container type iterator.
typedef std::vector< T > ObjectVector
 Definition objects vector.

Private Attributes

bool myHaveChanged
 Information whether the vector is out of sync with the map.
IDMap myMap
 The map from key to object.
ObjectVector myVector
 The stored vector of all known items.

Member Typedef Documentation

template<class T>
typedef std::map< std::string, T > NamedObjectCont< T >::IDMap [private]

Definition of the key to pointer map type.

Definition at line 225 of file NamedObjectCont.h.

template<class T>
typedef IDMap::iterator NamedObjectCont< T >::myContIt [private]

Definition of the container type iterator.

Definition at line 228 of file NamedObjectCont.h.

template<class T>
typedef std::vector<T> NamedObjectCont< T >::ObjectVector [private]

Definition objects vector.

Definition at line 234 of file NamedObjectCont.h.


Constructor & Destructor Documentation

template<class T>
NamedObjectCont< T >::NamedObjectCont (  )  throw () [inline]

Constructor.

Definition at line 54 of file NamedObjectCont.h.

00054 : myHaveChanged(false) { }

template<class T>
virtual NamedObjectCont< T >::~NamedObjectCont (  )  throw () [inline, virtual]

Destructor.

Definition at line 58 of file NamedObjectCont.h.

00058                                        {
00059         for (typename IDMap::iterator i=myMap.begin(); i!=myMap.end(); i++) {
00060             delete(*i).second;
00061         }
00062     }


Member Function Documentation

template<class T>
virtual bool NamedObjectCont< T >::add ( const std::string &  id,
item 
) throw () [inline, virtual]

Adds an item.

If another item with the same name is already known, false is reported and the item is not added.

Parameters:
[in] id The id of the item to add
[in] item The item to add
Returns:
If the item could been added (no item with the same id was within the container before)

Reimplemented in ROVehicleCont.

Definition at line 74 of file NamedObjectCont.h.

Referenced by MSDetectorControl::add(), RONet::addEdge(), RONet::addNode(), RONet::addRouteDef(), RONet::addVehicleType(), ODDistrictHandler::closeDistrict(), and NLJunctionControlBuilder::closeJunction().

00074                                                           {
00075         if (myMap.find(id)!=myMap.end()) {
00076             return false;
00077         }
00078         myMap.insert(std::make_pair(id, item));
00079         myHaveChanged = true;
00080         return true;
00081     }

template<class T>
const std::vector<T>& NamedObjectCont< T >::buildAndGetStaticVector (  )  const throw () [inline]

Definition at line 173 of file NamedObjectCont.h.

Referenced by MSDetectorControl::updateDetectors().

00173                                                                 {
00174         if (myHaveChanged) {
00175             myVector.clear();
00176             typename IDMap::const_iterator i;
00177             for (i=myMap.begin(); i!=myMap.end(); ++i) {
00178                 myVector.push_back((*i).second);
00179             }
00180             myHaveChanged = false;
00181         }
00182         return myVector;
00183     }

template<class T>
void NamedObjectCont< T >::clear (  )  throw () [inline]

Removes all items from the container (deletes them, too).

Reimplemented in ROVehicleCont.

Definition at line 117 of file NamedObjectCont.h.

Referenced by ROVehicleCont::clear(), MSDetectorControl::~MSDetectorControl(), and RONet::~RONet().

00117                          {
00118         for (typename IDMap::iterator i=myMap.begin(); i!=myMap.end(); i++) {
00119             delete(*i).second;
00120         }
00121         myMap.clear();
00122         myVector.clear();
00123         myHaveChanged = true;
00124     }

template<class T>
bool NamedObjectCont< T >::erase ( const std::string &  id  )  throw () [inline]

Removes the named item from the container.

If the named object exists, it is deleted, the key is removed from the map, and true is returned. If the id was not known, false is returned.

Parameters:
[in] id The id of the item to delete
Returns:
Whether the object could be deleted (was within the map)

Reimplemented in ROVehicleCont.

Definition at line 145 of file NamedObjectCont.h.

00145                                             {
00146         typename IDMap::iterator i=myMap.find(id);
00147         if (i==myMap.end()) {
00148             return false;
00149         }
00150         T o = (*i).second;
00151         myMap.erase(i);
00152         // and from the vector
00153         typename ObjectVector::iterator i2 =
00154             find(myVector.begin(), myVector.end(), o);
00155         myHaveChanged = true;
00156         if (i2!=myVector.end()) {
00157             myVector.erase(i2);
00158         }
00159         delete o;
00160         return true;
00161     }

template<class T>
T NamedObjectCont< T >::get ( const std::string &  id  )  const throw () [inline]

Retrieves an item.

Returns 0 when no item with the given id is stored within the container

Parameters:
[in] id The id of the item to retrieve
Returns:
The item stored under the given id, or 0 if no such item exists

Definition at line 107 of file NamedObjectCont.h.

Referenced by ODMatrix::add(), RONet::getEdge(), GUINet::getJunctionPosition(), RONet::getNode(), RONet::getRouteDef(), RONet::getVehicleTypeSecure(), traci::TraCIServer::handleTrafficLightDomain(), and TraCIServerAPI_Junction::processGet().

00107                                              {
00108         typename std::map<std::string, T>::const_iterator i = myMap.find(id);
00109         if (i==myMap.end()) {
00110             return 0;
00111         }
00112         return (*i).second;
00113     }

template<class T>
const std::map<std::string, T>& NamedObjectCont< T >::getMyMap (  )  const throw () [inline]

Definition at line 218 of file NamedObjectCont.h.

Referenced by RONet::checkSourceAndDestinations(), RONet::getEdgeMap(), and GUINet::initGUIStructures().

00218                                                          {
00219         return myMap;
00220     }

template<class T>
std::vector<T> NamedObjectCont< T >::getTempVector (  )  const throw () [inline]

Definition at line 193 of file NamedObjectCont.h.

00193                                                {
00194         std::vector<T> ret;
00195         typename IDMap::const_iterator i;
00196         for (i=myMap.begin(); i!=myMap.end(); ++i) {
00197             ret.push_back((*i).second);
00198         }
00199         return ret;
00200     }

template<class T>
void NamedObjectCont< T >::insertIDs ( std::vector< std::string > &  into  )  const throw () [inline]

Definition at line 206 of file NamedObjectCont.h.

Referenced by TraCIServerAPI_Junction::processGet().

00206                                                              {
00207         typename IDMap::const_iterator i;
00208         for (i=myMap.begin(); i!=myMap.end(); ++i) {
00209             into.push_back((*i).first);
00210         }
00211     }

template<class T>
virtual bool NamedObjectCont< T >::remove ( const std::string &  id  )  throw () [inline, virtual]

Removes an item.

Parameters:
[in] id The id of the item to remove
Returns:
If the item could been removed (an item with the id was within the container before)

Definition at line 88 of file NamedObjectCont.h.

00088                                                      {
00089         if (myMap.find(id)==myMap.end()) {
00090             return false;
00091         }
00092         typename std::map<std::string, T>::iterator i = myMap.find(id);
00093         delete i->second;
00094         myMap.erase(i);
00095         myHaveChanged = true;
00096         return true;
00097     }

template<class T>
unsigned int NamedObjectCont< T >::size (  )  const throw () [inline]

Returns the number of items within the container.

Returns:
The number of stored items

Definition at line 131 of file NamedObjectCont.h.

Referenced by RONet::furtherStored(), RONet::getEdgeNo(), GUINet::initGUIStructures(), main(), and RONet::saveAndRemoveRoutesUntil().

00131                                       {
00132         return (unsigned int) myMap.size();
00133     }


Field Documentation

template<class T>
bool NamedObjectCont< T >::myHaveChanged [mutable, private]

template<class T>
IDMap NamedObjectCont< T >::myMap [private]

template<class T>
ObjectVector NamedObjectCont< T >::myVector [mutable, private]


The documentation for this class was generated from the following file:

Generated on Wed May 5 00:06:50 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6