Eclipse SUMO - Simulation of Urban MObility
GNEReferenceCounter.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 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 /****************************************************************************/
14 // A class that counts references to itself
15 // We may wish to keep references to junctions/nodes either in the network or in the undoList
16 // to clean up properly we have to resort to reference counting
17 /****************************************************************************/
18 #ifndef GNEReferenceCounter_h
19 #define GNEReferenceCounter_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
28 #include <utils/common/StdDefs.h>
29 
30 
31 // ===========================================================================
32 // class definitions
33 // ===========================================================================
38 public:
41 
44  // If myCount is different of 0, means that references weren't removed correctly
45  if (myCount != 0) {
46  // cannot print id here, it already got destructed
47  WRITE_ERROR("Attempt to delete instance of GNEReferenceCounter with count " + toString(myCount));
48  }
49  }
50 
52  void decRef(const std::string& debugMsg = "") {
53  // debugMsg only used for print debugging
54 #ifdef _DEBUG_REFERENCECOUNTER
55  std::cout << "decRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
56 #else
57  UNUSED_PARAMETER(debugMsg);
58 #endif
59  // write error if decrement results into a negative count
60  if (myCount < 1) {
61  WRITE_ERROR("Attempt to decrement references below zero for instance of GNEReferenceCounter");
62  }
63  myCount--;
64  }
65 
67  void incRef(const std::string& debugMsg = "") {
68  // debugMsg only used for print debugging
69 #ifdef _DEBUG_REFERENCECOUNTER
70  std::cout << "incRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
71 #else
72  UNUSED_PARAMETER(debugMsg);
73 #endif
74  myCount++;
75  }
76 
78  bool unreferenced() {
79  return myCount == 0;
80  }
81 
83  virtual const std::string getID() const = 0;
84 
85 
86 private:
88  int myCount;
89 
90 };
91 
92 
93 #endif
94 
95 /****************************************************************************/
96 
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
GNEReferenceCounter::unreferenced
bool unreferenced()
check if object ins't referenced
Definition: GNEReferenceCounter.h:78
MsgHandler.h
GNEReferenceCounter::getID
virtual const std::string getID() const =0
return ID of object
GNEReferenceCounter::myCount
int myCount
reference counter
Definition: GNEReferenceCounter.h:88
GNEReferenceCounter::decRef
void decRef(const std::string &debugMsg="")
Decrease reference.
Definition: GNEReferenceCounter.h:52
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
GNEReferenceCounter::GNEReferenceCounter
GNEReferenceCounter()
Definition: GNEReferenceCounter.h:40
GNEReferenceCounter::incRef
void incRef(const std::string &debugMsg="")
Increarse reference.
Definition: GNEReferenceCounter.h:67
GNEReferenceCounter
Definition: GNEReferenceCounter.h:37
config.h
StdDefs.h
GNEReferenceCounter::~GNEReferenceCounter
~GNEReferenceCounter()
Definition: GNEReferenceCounter.h:43
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283