oILAB
Loading...
Searching...
No Matches
StaticID.h
Go to the documentation of this file.
1/* This file is part of gbLAB.
2 *
3 * gbLAB is distributed without any warranty under the MIT License.
4 */
5
6
7#ifndef gbLAB_STATICID_H_
8#define gbLAB_STATICID_H_
9
10namespace oILAB {
11
12/************************************************************/
13/************************************************************/
23template <typename Derived> class StaticID {
24
25 // The increment
26 static size_t increment;
27
28 // The incremental counters
29 static size_t count;
30 static bool count_used;
31
32public:
34 const size_t sID;
35
36 /**********************************************************************/
38 count_used = true;
40 }
41
42 /**********************************************************************/
43 StaticID(const StaticID &) : sID(count) {
44 count_used = true;
46 }
47
48 /**********************************************************************/
49 static size_t nextID() { return count; }
50
51 static size_t &get_count() { return count; }
52
53 /**********************************************************************/
54 static void set_count(const size_t &newCount) {
55 if (newCount < count) {
56 throw std::runtime_error(
57 "StaticID error: YOU ARE TRYING TO SET THE COUNTER TO A LOWER VALUE "
58 "THAN THE CURRENT ONE\n");
59 }
60 count = newCount;
61 count_used = false;
62 }
63
64 /**********************************************************************/
65 static void set_increment(const size_t &newIncrement) {
66 if (newIncrement < 1) {
67 throw std::runtime_error("StaticID error: newIncrement MUST BE >=1\n");
68 }
69 if (count_used) {
71 count += newIncrement;
72 }
73 increment = newIncrement;
74 }
75};
76
77/* Static data members *****************************/
78template <typename Derived> size_t StaticID<Derived>::increment = 1;
79
80template <typename Derived> size_t StaticID<Derived>::count = 0;
81
82template <typename Derived> bool StaticID<Derived>::count_used = false;
83
84} // namespace oILAB
85#endif
A class template that implements a counter of the number of instances of Derived type that are create...
Definition StaticID.h:23
static size_t & get_count()
Definition StaticID.h:51
static void set_increment(const size_t &newIncrement)
Definition StaticID.h:65
const size_t sID
The static ID of this.
Definition StaticID.h:34
static size_t increment
Definition StaticID.h:26
static size_t nextID()
Definition StaticID.h:49
static size_t count
Definition StaticID.h:29
static void set_count(const size_t &newCount)
Definition StaticID.h:54
static bool count_used
Definition StaticID.h:30
StaticID(const StaticID &)
Definition StaticID.h:43