Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/common/Named.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file Named.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @date Sept 2002
18
///
19
// Base class for objects which have an id.
20
/****************************************************************************/
21
#pragma once
22
#include <config.h>
23
#include <iostream>
24
#include <string>
25
#include <set>
26
27
28
/// @brief Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle)
29
// @note Numbers of different lengths will not be ordered by alphanumerical sorting
30
struct ComparatorIdLess {
31
template<class T>
32
bool operator()(const T* const a, const T* const b) const {
33
return a->getID() < b->getID();
34
}
35
};
36
37
38
/// @brief Function-object for stable sorting of objects with numerical ids
39
struct ComparatorNumericalIdLess {
40
template<class T>
41
bool operator()(const T* const a, const T* const b) const {
42
return a->getNumericalID() < b->getNumericalID();
43
}
44
};
45
46
47
// ===========================================================================
48
// class definitions
49
// ===========================================================================
50
/**
51
* @class Named
52
* @brief Base class for objects which have an id.
53
*/
54
class Named {
55
public:
56
/** @brief Constructor
57
* @param[in] id The id of the object
58
*/
59
Named(const std::string& id) : myID(id) { }
60
61
62
/// @brief Destructor
63
virtual ~Named() { }
64
65
/// @brief get an identifier for Named-like object which may be Null
66
template<class T>
67
static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
68
return obj == 0 ? fallBack : obj->getID();
69
}
70
71
/** @brief Returns the id
72
* @return The stored id
73
*/
74
const std::string& getID() const {
75
return myID;
76
}
77
78
79
/** @brief resets the id
80
* @param[in] newID The new id of this object
81
*/
82
virtual void setID(const std::string& newID) {
83
myID = newID;
84
}
85
86
87
/** @class StoringVisitor
88
* @brief Allows to store the object; used as context while traveling the rtree in TraCI
89
*/
90
class StoringVisitor {
91
public:
92
/// @brief Constructor
93
StoringVisitor(std::set<const Named*>& objects) : myObjects(objects) {}
94
95
/// @brief Destructor
96
~StoringVisitor() {}
97
98
/// @brief Adds the given object to the container
99
void add(const Named* const o) const {
100
myObjects.insert(o);
101
}
102
103
/// @brief The container
104
std::set<const Named*>& myObjects;
105
106
private:
107
/// @brief invalidated copy constructor
108
StoringVisitor(const StoringVisitor& src);
109
110
/// @brief invalidated assignment operator
111
StoringVisitor& operator=(const StoringVisitor& src);
112
};
113
114
115
/** @brief Adds this object to the given container
116
* @param[in, filled] cont The container to add this item to
117
*/
118
void addTo(const StoringVisitor& cont) const {
119
cont.add(this);
120
}
121
122
123
protected:
124
/// @brief The name of the object
125
std::string myID;
126
127
};
128
129