SUMO - Simulation of Urban MObility
GNEReferenceCounter.h
Go to the documentation of this file.
1 /****************************************************************************/
7 // A class that counts references to itself
8 // We may wish to keep references to junctions/nodes either in the network or in the undoList
9 // to clean up properly we have to resort to reference counting
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef GNEReferenceCounter_h
23 #define GNEReferenceCounter_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
36 #include <utils/common/ToString.h>
37 
38 
39 //#define _DEBUG_REFERENCECOUNTER
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
48 public:
51 
54  // If myCount is different of 0, means that references weren't removed correctly
55  if (myCount != 0) {
56  // cannot print id here, it already got destructed
57  WRITE_ERROR("Attempt to delete instance of GNEReferenceCounter with count " + toString(myCount));
58  }
59  }
60 
62  void decRef(const std::string& debugMsg = "") {
63  // debugMsg only used for print debugging
64 #ifdef _DEBUG_REFERENCECOUNTER
65  std::cout << "decRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
66 #else
67  UNUSED_PARAMETER(debugMsg);
68 #endif
69  // write error if decrement results into a negative count
70  if (myCount < 1) {
71  WRITE_ERROR("Attempt to decrement references below zero for instance of GNEReferenceCounter");
72  }
73  myCount--;
74  }
75 
77  void incRef(const std::string& debugMsg = "") {
78  // debugMsg only used for print debugging
79 #ifdef _DEBUG_REFERENCECOUNTER
80  std::cout << "incRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
81 #else
82  UNUSED_PARAMETER(debugMsg);
83 #endif
84  myCount++;
85  }
86 
88  bool unreferenced() {
89  return myCount == 0;
90  }
91 
93  virtual const std::string getID() const = 0;
94 
95 
96 private:
98  int myCount;
99 
100 };
101 
102 
103 #endif
104 
105 /****************************************************************************/
106 
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
int myCount
reference counter
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
void incRef(const std::string &debugMsg="")
Increarse reference.
void decRef(const std::string &debugMsg="")
Decrease reference.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
virtual const std::string getID() const =0
return ID of object
bool unreferenced()
check if object ins&#39;t referenced