Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/GNEReferenceCounter.h
169666 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 GNEReferenceCounter.h
15
/// @author Jakob Erdmann
16
/// @date Mar 2011
17
///
18
// A class that counts references to itself
19
// We may wish to keep references to junctions/nodes either in the network or in the undoList
20
// to clean up properly we have to resort to reference counting
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <utils/common/MsgHandler.h>
26
#include <utils/common/StdDefs.h>
27
28
29
// ===========================================================================
30
// class definitions
31
// ===========================================================================
32
/**
33
* @class GNEReferenceCounter
34
*/
35
class GNEReferenceCounter {
36
public:
37
//// @brief constructor
38
GNEReferenceCounter() : myCount(0) {}
39
40
/// @brief return ID of object
41
virtual const std::string getID() const = 0;
42
43
//// @brief destructor
44
virtual ~GNEReferenceCounter() {
45
// If myCount is different of 0, means that references weren't removed correctly
46
if (myCount != 0) {
47
// cannot print id here, it already got destructed
48
WRITE_ERROR("Attempt to delete instance of GNEReferenceCounter with count " + toString(myCount));
49
}
50
}
51
52
/// @brief Decrease reference
53
void decRef(const std::string& debugMsg = "") {
54
// debugMsg only used for print debugging
55
#ifdef _DEBUG_REFERENCECOUNTER
56
std::cout << "decRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
57
#else
58
UNUSED_PARAMETER(debugMsg);
59
#endif
60
// write error if decrement results into a negative count
61
if (myCount < 1) {
62
WRITE_ERROR("Attempt to decrement references below zero for instance of GNEReferenceCounter");
63
}
64
myCount--;
65
}
66
67
/// @brief Increase reference
68
void incRef(const std::string& debugMsg = "") {
69
// debugMsg only used for print debugging
70
#ifdef _DEBUG_REFERENCECOUNTER
71
std::cout << "incRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
72
#else
73
UNUSED_PARAMETER(debugMsg);
74
#endif
75
myCount++;
76
}
77
78
/// @brief check if object ins't referenced
79
bool unreferenced() {
80
return myCount == 0;
81
}
82
83
private:
84
/// @brief reference counter
85
int myCount;
86
};
87
88