Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSharedCounter.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSHAREDCOUNTER__
2 #define __JLANG__JSHAREDCOUNTER__
3 
4 
5 /**
6  * \author mdejong
7  */
8 
9 namespace JLANG {}
10 namespace JPP { using namespace JLANG; }
11 
12 namespace JLANG {
13 
14 
15  /**
16  * Shared counter.
17  */
19  {
20  public:
21  /**
22  * Default constructor.
23  */
25  counter(NULL)
26  {}
27 
28 
29  /**
30  * Initialise counter.
31  */
32  void initialise()
33  {
34  detach();
35 
36  counter = new int(1);
37  }
38 
39 
40  /**
41  * Attach this counter to given shared counter object.
42  *
43  * \param object shared counter
44  */
45  void attach(const JSharedCounter& object)
46  {
47  detach();
48 
49  counter = object.counter;
50 
51  if (counter != NULL) {
52  ++(*counter);
53  }
54  }
55 
56 
57  /**
58  * Detach.
59  *
60  * \return true if counter at zero; else false
61  */
62  bool detach()
63  {
64  if (counter != NULL) {
65 
66  if (--(*counter) == 0) {
67 
68  delete counter;
69 
70  counter = NULL;
71 
72  return true;
73  }
74 
75  counter = NULL;
76  }
77 
78  return false;
79  }
80 
81  protected:
82  int* counter;
83  };
84 }
85 
86 #endif
Shared counter.
void attach(const JSharedCounter &object)
Attach this counter to given shared counter object.
JSharedCounter()
Default constructor.
void initialise()
Initialise counter.
bool detach()
Detach.