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-2018 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>
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 
46 
48 
49 
50 GUIGlID
51 GUIGlObjectStorage::registerObject(GUIGlObject* object, const std::string& fullName) {
53  GUIGlID id = myAktID++;
54  myMap[id] = object;
55  myFullNameMap[fullName] = object;
56  return id;
57 }
58 
59 
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) {
82  if (myFullNameMap.count(fullName)) {
83  GUIGlID id = myFullNameMap[fullName]->getGlID();
84  return getObjectBlocking(id);
85  }
86  return nullptr;
87 }
88 
89 
90 bool
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
112  myMap.clear();
113  myAktID = 0;
114 }
115 
116 
117 void
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>
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 /****************************************************************************/
142 
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:43
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:59
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.