SUMO - Simulation of Urban MObility
GUIGlObjectStorage.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A storage for displayed objects via their numerical id
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <map>
33 #include <iostream>
34 #include <cassert>
36 #include "GUIGlObject.h"
37 #include "GUIGlObjectStorage.h"
38 
39 
40 // ===========================================================================
41 // static variables (instances in this case)
42 // ===========================================================================
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50  : myAktID(1) {}
51 
52 
54 
55 
56 GUIGlID
57 GUIGlObjectStorage::registerObject(GUIGlObject* object, const std::string& fullName) {
59  GUIGlID id = myAktID++;
60  myMap[id] = object;
61  myFullNameMap[fullName] = object;
62  return id;
63 }
64 
65 
69  ObjectMap::iterator i = myMap.find(id);
70  if (i == myMap.end()) {
71  i = myBlocked.find(id);
72  if (i != myBlocked.end()) {
73  GUIGlObject* o = (*i).second;
74  return o;
75  }
76  return 0;
77  }
78  GUIGlObject* o = (*i).second;
79  myMap.erase(id);
80  myBlocked[id] = o;
81  return o;
82 }
83 
84 
86 GUIGlObjectStorage::getObjectBlocking(const std::string& fullName) {
88  if (myFullNameMap.count(fullName)) {
89  GUIGlID id = myFullNameMap[fullName]->getGlID();
90  return getObjectBlocking(id);
91  }
92  return 0;
93 }
94 
95 
96 bool
99  ObjectMap::iterator i = myMap.find(id);
100  if (i == myMap.end()) {
101  i = myBlocked.find(id);
102  assert(i != myBlocked.end());
103  GUIGlObject* o = (*i).second;
104  myFullNameMap.erase(o->getFullName());
105  myBlocked.erase(id);
106  my2Delete[id] = o;
107  return false;
108  }
109  myFullNameMap.erase(i->second->getFullName());
110  myMap.erase(id);
111  return true;
112 }
113 
114 
115 void
118  myMap.clear();
119  myAktID = 0;
120 }
121 
122 
123 void
126  ObjectMap::iterator i = myBlocked.find(id);
127  if (i == myBlocked.end()) {
128  return;
129  }
130  GUIGlObject* o = (*i).second;
131  myBlocked.erase(id);
132  myMap[id] = o;
133 }
134 
135 
136 std::set<GUIGlID>
139  std::set<GUIGlID> result;
140  for (ObjectMap::const_iterator it = myMap.begin(); it != myMap.end(); it++) {
141  result.insert(it->first);
142  }
143  return result;
144 }
145 
146 
147 /****************************************************************************/
148 
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:49
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:70
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.