Eclipse 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.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // A class that counts references to itself
19 // We may wish to keep references to junctions/nodes either in the network or in the undoList
20 // to clean up properly we have to resort to reference counting
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
26 #include <utils/common/StdDefs.h>
27 
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
36 public:
39 
41  virtual const std::string getID() const = 0;
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 
83 private:
85  int myCount;
86 };
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
void decRef(const std::string &debugMsg="")
Decrease reference.
void incRef(const std::string &debugMsg="")
Increase reference.
bool unreferenced()
check if object ins't referenced
virtual const std::string getID() const =0
return ID of object
int myCount
reference counter