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