Eclipse 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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A storage for displayed objects via their numerical id
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <map>
25 #include <iostream>
26 #include <cassert>
28 #include "GUIGlObject.h"
29 #include "GUIGlObjectStorage.h"
30 
31 
32 // ===========================================================================
33 // static variables (instances in this case)
34 // ===========================================================================
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42  myAktID(1),
43  myLock(true)
44 {}
45 
46 
48 
49 
50 GUIGlID
51 GUIGlObjectStorage::registerObject(GUIGlObject* object, const std::string& fullName) {
52  FXMutexLock locker(myLock);
53  GUIGlID id = myAktID++;
54  myMap[id] = object;
55  myFullNameMap[fullName] = object;
56  return id;
57 }
58 
59 
62  FXMutexLock locker(myLock);
63  ObjectMap::iterator i = myMap.find(id);
64  if (i == myMap.end()) {
65  i = myBlocked.find(id);
66  if (i != myBlocked.end()) {
67  GUIGlObject* o = (*i).second;
68  return o;
69  }
70  return nullptr;
71  }
72  GUIGlObject* o = (*i).second;
73  myMap.erase(id);
74  myBlocked[id] = o;
75  return o;
76 }
77 
78 
80 GUIGlObjectStorage::getObjectBlocking(const std::string& fullName) {
81  FXMutexLock locker(myLock);
82  if (myFullNameMap.count(fullName)) {
83  GUIGlID id = myFullNameMap[fullName]->getGlID();
84  return getObjectBlocking(id);
85  }
86  return nullptr;
87 }
88 
89 
90 bool
92  FXMutexLock locker(myLock);
93  ObjectMap::iterator i = myMap.find(id);
94  if (i == myMap.end()) {
95  i = myBlocked.find(id);
96  assert(i != myBlocked.end());
97  GUIGlObject* o = (*i).second;
98  myFullNameMap.erase(o->getFullName());
99  myBlocked.erase(id);
100  my2Delete[id] = o;
101  return false;
102  }
103  myFullNameMap.erase(i->second->getFullName());
104  myMap.erase(id);
105  return true;
106 }
107 
108 
109 void
111  FXMutexLock locker(myLock);
112  myMap.clear();
113  myAktID = 0;
114 }
115 
116 
117 void
119  FXMutexLock locker(myLock);
120  ObjectMap::iterator i = myBlocked.find(id);
121  if (i == myBlocked.end()) {
122  return;
123  }
124  GUIGlObject* o = (*i).second;
125  myBlocked.erase(id);
126  myMap[id] = o;
127 }
128 
129 
130 std::set<GUIGlID>
132  FXMutexLock locker(myLock);
133  std::set<GUIGlID> result;
134  for (ObjectMap::const_iterator it = myMap.begin(); it != myMap.end(); it++) {
135  result.insert(it->first);
136  }
137  return result;
138 }
139 
140 
141 /****************************************************************************/
unsigned int GUIGlID
Definition: GUIGlObject.h:40
const std::string & getFullName() const
A storage for of displayed objects via their numerical id.
GUIGlObjectStorage()
Constructor.
GUIGlID registerObject(GUIGlObject *object, const std::string &fullName)
Registers an object.
void clear()
Clears this container.
~GUIGlObjectStorage()
Destructor.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
GUIGlID myAktID
The next id to give; initially zero, increased by one with each object registration.
ObjectMap myBlocked
The currently accessed objects.
ObjectMap myMap
The known objects which are not accessed currently.
std::set< GUIGlID > getAllIDs() const
Returns the set of all known ids.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
FXMutex myLock
A lock to avoid parallel access on the storages.
bool remove(GUIGlID id)
Removes the named object from this container.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
std::map< std::string, GUIGlObject * > myFullNameMap
ObjectMap my2Delete
Objects to delete.