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/StdDefs.h>
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
39 public:
42 
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 
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 
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 
79  bool unreferenced() {
80  return myCount == 0;
81  }
82 
84  virtual const std::string getID() const = 0;
85 
86 
87 private:
89  int myCount;
90 
91 };
92 
93 
94 #endif
95 
96 /****************************************************************************/
97 
#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:247
virtual const std::string getID() const =0
return ID of object
bool unreferenced()
check if object ins&#39;t referenced