CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Exceptions/CLHEP/RefCount/ZMuseCount.h
Go to the documentation of this file.
1 #ifndef ZMUSECOUNT_H
2 #define ZMUSECOUNT_H
3 
4 
5 // ----------------------------------------------------------------------
6 //
7 // ZMuseCount.h - utility class for use in reference-counting
8 //
9 //
10 // History:
11 // 19-Sep-1997 WEB Design stolen, and code adapted, from pp 70-73 of
12 // Koenig & Moo: "Ruminations on C++" (1996)
13 //
14 // ----------------------------------------------------------------------
15 
16 
17 class ZMuseCount {
18 
19 public:
20 
21  // --------------------------
22  // Constructors & destructor:
23  // --------------------------
24 
26  // Constructor
27  ZMuseCount( const ZMuseCount & u );
28  // Copy constructor
30  // Destructor
31 
32 
33  // -----------------------------------
34  // Functions useful to a handle class:
35  // -----------------------------------
36 
37  bool only();
38  // Is there exactly one copy of the referent?
39 
40  bool makeonly();
41  // Enables the handle (to a ClassType) to refer to a unique (copy of)
42  // the referent via the following usage:
43  // if ( u.makeonly() ) /* ZMuseCount u; */
44  // p = new ClassType( *p ); /* ClassType * p; */
45  // This is useful in implementing copy-on-write semantics
46  // (i.e., non-shared or value semantics), and would appear just before
47  // updating (any part of) *p in such a context.
48 
49  bool reattach( const ZMuseCount & u );
50  // Simplifies a handle's implementation of assignment via the following
51  // usage:
52  // if ( u.reattach(h.u) ) /* Handle<ClassType> h; */
53  // delete p; /* p and u as above */
54  // p = h.p;
55 
56 
57 private:
58 
59  int * p;
60  // *p is the reference counter itself
61 
62  ZMuseCount & operator=( const ZMuseCount & );
63  // Forbidden operation on a ZMuseCount object
64 
65 }; // ZMuseCount
66 
67 
68 //#include "CLHEP/RefCount/ZMuseCount.icc"
69 
70 
71 #endif // ZMUSECOUNT_H
ZMuseCount(const ZMuseCount &u)
bool makeonly()
Definition: ZMuseCount.cc:19
bool only()
bool reattach(const ZMuseCount &u)
Definition: ZMuseCount.cc:33