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-2016 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) {
55  // cannot print id here, it already got destructed
56  throw ProcessError("Attempt to delete instance of GNEReferenceCounter with count " + toString(myCount));
57  }
58  }
59 
60 
61  void decRef(const std::string& debugMsg = "") {
62 #ifdef _DEBUG_REFERENCECOUNTER
63  std::cout << "decRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
64 #endif
65  UNUSED_PARAMETER(debugMsg); // only used for print debugging
66  if (myCount < 1) {
67  throw ProcessError("Attempt to decrement references below zero for instance of GNEReferenceCounter");
68  }
69  myCount--;
70  }
71 
72 
73  void incRef(const std::string& debugMsg = "") {
74 #ifdef _DEBUG_REFERENCECOUNTER
75  std::cout << "incRef (" + toString(myCount) + ") for " + getID() + ": " << debugMsg << "\n";
76 #endif
77  UNUSED_PARAMETER(debugMsg); // only used for print debugging
78  myCount++;
79  }
80 
81 
82  bool unreferenced() {
83  return myCount == 0;
84  }
85 
86 
87  virtual const std::string getID() const = 0;
88 
89 
90 private:
91  size_t myCount;
92 
93 };
94 
95 
96 #endif
97 
98 /****************************************************************************/
99 
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
GNEReferenceCounter()
constructor
~GNEReferenceCounter()
destructor
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
void incRef(const std::string &debugMsg="")
void decRef(const std::string &debugMsg="")
virtual const std::string getID() const =0