Jpp  master_rocky
the software that should make you happy
JSharedCounter.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSHAREDCOUNTER__
2 #define __JLANG__JSHAREDCOUNTER__
3 
4 #include <cstdlib>
5 
6 /**
7  * \author mdejong
8  */
9 
10 namespace JLANG {}
11 namespace JPP { using namespace JLANG; }
12 
13 namespace JLANG {
14 
15 
16  /**
17  * Shared counter.
18  */
20  {
21  public:
22  /**
23  * Default constructor.
24  */
26  counter(NULL)
27  {}
28 
29 
30  /**
31  * Initialise counter.
32  */
33  void initialise()
34  {
35  detach();
36 
37  counter = new int(1);
38  }
39 
40 
41  /**
42  * Attach this counter to given shared counter object.
43  *
44  * \param object shared counter
45  */
46  void attach(const JSharedCounter& object)
47  {
48  detach();
49 
50  counter = object.counter;
51 
52  if (counter != NULL) {
53  ++(*counter);
54  }
55  }
56 
57 
58  /**
59  * Detach.
60  *
61  * \return true if counter at zero; else false
62  */
63  bool detach()
64  {
65  if (counter != NULL) {
66 
67  if (--(*counter) == 0) {
68 
69  delete counter;
70 
71  counter = NULL;
72 
73  return true;
74  }
75 
76  counter = NULL;
77  }
78 
79  return false;
80  }
81 
82 
83  /**
84  * Get count
85  *
86  * \return count
87  */
88  const int getCount()
89  {
90  return (counter != NULL ? *counter : 0);
91  }
92 
93  protected:
94  int* counter;
95  };
96 }
97 
98 #endif
const int getCount()
Get count.
void initialise()
Initialise counter.
void attach(const JSharedCounter &object)
Attach this counter to given shared counter object.
JSharedCounter()
Default constructor.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).