Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GUIGlObjectStorage.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 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 <cassert>
26#include "GUIGlObject.h"
27#include "GUIGlObjectStorage.h"
28
29
30// ===========================================================================
31// static variables (instances in this case)
32// ===========================================================================
34
35
36// ===========================================================================
37// method definitions
38// ===========================================================================
40 myNextID(1),
41 myLock(true) {
42 myObjects.push_back(nullptr);
43}
44
45
47
48
51 FXMutexLock locker(myLock);
52 const GUIGlID id = myNextID;
53 if (id == myObjects.size()) {
54 myObjects.push_back(object);
55 } else {
56 myObjects[id] = object;
57 }
58 while (myNextID < myObjects.size() && myObjects[myNextID] != nullptr) {
59 myNextID++;
60 }
61 return id;
62}
63
64
65void
66GUIGlObjectStorage::changeName(GUIGlObject* object, const std::string& fullName) {
67 FXMutexLock locker(myLock);
68 myFullNameMap.erase(object->getFullName());
69 myFullNameMap[fullName] = object;
70}
71
72
75 FXMutexLock locker(myLock);
76 if (id < myObjects.size() && myObjects[id] != nullptr) {
77 GUIGlObject* const o = myObjects[id];
78 o->setBlocked();
79 return o;
80 }
81 return nullptr;
82}
83
84
86GUIGlObjectStorage::getObjectBlocking(const std::string& fullName) const {
87 FXMutexLock locker(myLock);
88 auto findIt = myFullNameMap.find(fullName);
89 if (findIt != myFullNameMap.end()) {
90 GUIGlObject* const o = findIt->second;
91 o->setBlocked();
92 return o;
93 }
94 return nullptr;
95}
96
97
98bool
100 FXMutexLock locker(myLock);
101 assert(id < myObjects.size() && myObjects[id] != nullptr);
102 myFullNameMap.erase(myObjects[id]->getFullName());
103 const bool wasBlocked = myObjects[id]->isBlocked();
104 myObjects[id] = nullptr;
105 if (id < myNextID) {
106 myNextID = id;
107 }
108 return !wasBlocked;
109}
110
111
112void
114 FXMutexLock locker(myLock);
115 myObjects.clear();
116 myObjects.push_back(nullptr);
117 myFullNameMap.clear();
118 myNextID = 1;
119}
120
121
122void
124 FXMutexLock locker(myLock);
125 if (id < myObjects.size() && myObjects[id] != nullptr) {
126 myObjects[id]->setBlocked(false);
127 }
128}
129
130
131const std::vector<GUIGlObject*>&
135
136/****************************************************************************/
unsigned int GUIGlID
Definition GUIGlObject.h:43
void setBlocked(const bool state=true)
set blocking status
A storage for of displayed objects via their numerical id.
GUIGlObjectStorage()
Constructor.
GUIGlID registerObject(GUIGlObject *object)
Registers an object.
void clear()
Clears this container.
~GUIGlObjectStorage()
Destructor.
const std::vector< GUIGlObject * > & getAllGLObjects() const
Returns the set of all known objects.
void changeName(GUIGlObject *object, const std::string &fullName)
void unblockObject(GUIGlID id)
Marks an object as unblocked.
GUIGlObject * getObjectBlocking(GUIGlID id) const
Returns the object from the container locking it.
std::vector< GUIGlObject * > myObjects
The known objects.
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.
GUIGlID myNextID
The next id to give; initially one, increased by one with each object registration.
std::map< std::string, GUIGlObject * > myFullNameMap
The known objects by their full name.