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