Jpp
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  protected:
83  int* counter;
84  };
85 }
86 
87 #endif
JLANG::JSharedCounter::detach
bool detach()
Detach.
Definition: JSharedCounter.hh:63
JLANG::JSharedCounter
Shared counter.
Definition: JSharedCounter.hh:19
JLANG::JSharedCounter::counter
int * counter
Definition: JSharedCounter.hh:83
JLANG::JSharedCounter::attach
void attach(const JSharedCounter &object)
Attach this counter to given shared counter object.
Definition: JSharedCounter.hh:46
JLANG::JSharedCounter::JSharedCounter
JSharedCounter()
Default constructor.
Definition: JSharedCounter.hh:25
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JSharedCounter::initialise
void initialise()
Initialise counter.
Definition: JSharedCounter.hh:33
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10