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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // A class that counts references to itself
18 // We may wish to keep references to junctions/nodes either in the network or in the undoList
19 // to clean up properly we have to resort to reference counting
20 /****************************************************************************/
21 #ifndef GNEReferenceCounter_h
22 #define GNEReferenceCounter_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
35 #include <utils/common/ToString.h>
36 
37 
38 //#define _DEBUG_REFERENCECOUNTER
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
47 public:
50 
53  // If myCount is different of 0, means that references weren't removed correctly
54  if (myCount != 0) {
55  // cannot print id here, it already got destructed
56  WRITE_ERROR("Attempt to delete instance of GNEReferenceCounter with count " + toString(myCount));
57  }
58  }
59 
61  void decRef(const std::string& debugMsg = "") {
62  // debugMsg only used for print debugging
63 #ifdef _DEBUG_REFERENCECOUNTER
64  std::cout << "decRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
65 #else
66  UNUSED_PARAMETER(debugMsg);
67 #endif
68  // write error if decrement results into a negative count
69  if (myCount < 1) {
70  WRITE_ERROR("Attempt to decrement references below zero for instance of GNEReferenceCounter");
71  }
72  myCount--;
73  }
74 
76  void incRef(const std::string& debugMsg = "") {
77  // debugMsg only used for print debugging
78 #ifdef _DEBUG_REFERENCECOUNTER
79  std::cout << "incRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
80 #else
81  UNUSED_PARAMETER(debugMsg);
82 #endif
83  myCount++;
84  }
85 
87  bool unreferenced() {
88  return myCount == 0;
89  }
90 
92  virtual const std::string getID() const = 0;
93 
94 
95 private:
97  int myCount;
98 
99 };
100 
101 
102 #endif
103 
104 /****************************************************************************/
105 
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
int myCount
reference counter
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void incRef(const std::string &debugMsg="")
Increarse reference.
void decRef(const std::string &debugMsg="")
Decrease reference.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
virtual const std::string getID() const =0
return ID of object
bool unreferenced()
check if object ins&#39;t referenced