SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUIGlObjectStorage.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A storage for displayed objects via their numerical id
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <map>
34 #include <iostream>
35 #include <cassert>
37 #include "GUIGlObject.h"
38 #include "GUIGlObjectStorage.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static variables (instances in this case)
47 // ===========================================================================
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  : myAktID(0) {}
56 
57 
59 
60 
61 GUIGlID
62 GUIGlObjectStorage::registerObject(GUIGlObject* object, const std::string& fullName) {
64  GUIGlID id = myAktID++;
65  myMap[id] = object;
66  myFullNameMap[fullName] = object;
67  return id;
68 }
69 
70 
74  ObjectMap::iterator i = myMap.find(id);
75  if (i == myMap.end()) {
76  i = myBlocked.find(id);
77  if (i != myBlocked.end()) {
78  GUIGlObject* o = (*i).second;
79  return o;
80  }
81  return 0;
82  }
83  GUIGlObject* o = (*i).second;
84  myMap.erase(id);
85  myBlocked[id] = o;
86  return o;
87 }
88 
89 
91 GUIGlObjectStorage::getObjectBlocking(const std::string& fullName) {
93  if (myFullNameMap.count(fullName)) {
94  GUIGlID id = myFullNameMap[fullName]->getGlID();
95  return getObjectBlocking(id);
96  }
97  return 0;
98 }
99 
100 
101 bool
104  ObjectMap::iterator i = myMap.find(id);
105  if (i == myMap.end()) {
106  i = myBlocked.find(id);
107  assert(i != myBlocked.end());
108  GUIGlObject* o = (*i).second;
109  myFullNameMap.erase(o->getFullName());
110  myBlocked.erase(id);
111  my2Delete[id] = o;
112  return false;
113  }
114  myFullNameMap.erase(i->second->getFullName());
115  myMap.erase(id);
116  return true;
117 }
118 
119 
120 void
123  myMap.clear();
124  myAktID = 0;
125 }
126 
127 
128 void
131  ObjectMap::iterator i = myBlocked.find(id);
132  if (i == myBlocked.end()) {
133  return;
134  }
135  GUIGlObject* o = (*i).second;
136  myBlocked.erase(id);
137  myMap[id] = o;
138 }
139 
140 
141 std::set<GUIGlID>
144  std::set<GUIGlID> result;
145  for (ObjectMap::const_iterator it = myMap.begin(); it != myMap.end(); it++) {
146  result.insert(it->first);
147  }
148  return result;
149 }
150 
151 
152 /****************************************************************************/
153 
ObjectMap my2Delete
Objects to delete.
bool remove(GUIGlID id)
Removes the named object from this container.
std::set< GUIGlID > getAllIDs() const
Returns the set of all known ids.
std::map< std::string, GUIGlObject * > myFullNameMap
ObjectMap myBlocked
The currently accessed objects.
void clear()
Clears this container.
GUIGlID myAktID
The next id to give; initially zero, increased by one with each object registration.
GUIGlObjectStorage()
Constructor.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
A storage for of displayed objects via their numerical id.
~GUIGlObjectStorage()
Destructor.
unsigned int GUIGlID
Definition: GUIGlObject.h:48
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:71
GUIGlID registerObject(GUIGlObject *object, const std::string &fullName)
Registers an object.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
ObjectMap myMap
The known objects which are not accessed currently.
MFXMutex myLock
A lock to avoid parallel access on the storages.