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