SUMO - Simulation of Urban MObility
GUIGlObjectStorage.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A storage for displayed objects via their numerical id
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <map>
34 #include <iostream>
35 #include <cassert>
37 #include "GUIGlObject.h"
38 #include "GUIGlObjectStorage.h"
39 
40 
41 // ===========================================================================
42 // static variables (instances in this case)
43 // ===========================================================================
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51  : myAktID(1) {}
52 
53 
55 
56 
57 GUIGlID
58 GUIGlObjectStorage::registerObject(GUIGlObject* object, const std::string& fullName) {
60  GUIGlID id = myAktID++;
61  myMap[id] = object;
62  myFullNameMap[fullName] = object;
63  return id;
64 }
65 
66 
70  ObjectMap::iterator i = myMap.find(id);
71  if (i == myMap.end()) {
72  i = myBlocked.find(id);
73  if (i != myBlocked.end()) {
74  GUIGlObject* o = (*i).second;
75  return o;
76  }
77  return 0;
78  }
79  GUIGlObject* o = (*i).second;
80  myMap.erase(id);
81  myBlocked[id] = o;
82  return o;
83 }
84 
85 
87 GUIGlObjectStorage::getObjectBlocking(const std::string& fullName) {
89  if (myFullNameMap.count(fullName)) {
90  GUIGlID id = myFullNameMap[fullName]->getGlID();
91  return getObjectBlocking(id);
92  }
93  return 0;
94 }
95 
96 
97 bool
100  ObjectMap::iterator i = myMap.find(id);
101  if (i == myMap.end()) {
102  i = myBlocked.find(id);
103  assert(i != myBlocked.end());
104  GUIGlObject* o = (*i).second;
105  myFullNameMap.erase(o->getFullName());
106  myBlocked.erase(id);
107  my2Delete[id] = o;
108  return false;
109  }
110  myFullNameMap.erase(i->second->getFullName());
111  myMap.erase(id);
112  return true;
113 }
114 
115 
116 void
119  myMap.clear();
120  myAktID = 0;
121 }
122 
123 
124 void
127  ObjectMap::iterator i = myBlocked.find(id);
128  if (i == myBlocked.end()) {
129  return;
130  }
131  GUIGlObject* o = (*i).second;
132  myBlocked.erase(id);
133  myMap[id] = o;
134 }
135 
136 
137 std::set<GUIGlID>
140  std::set<GUIGlID> result;
141  for (ObjectMap::const_iterator it = myMap.begin(); it != myMap.end(); it++) {
142  result.insert(it->first);
143  }
144  return result;
145 }
146 
147 
148 /****************************************************************************/
149 
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:50
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:71
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.