Panda3D
 All Classes Functions Variables Enumerations
copyOnWritePointer.h
1 // Filename: copyOnWritePointer.h
2 // Created by: drose (09Apr07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef COPYONWRITEPOINTER_H
16 #define COPYONWRITEPOINTER_H
17 
18 #include "pandabase.h"
19 
20 #include "copyOnWriteObject.h"
21 #include "pointerTo.h"
22 #include "dcast.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : CopyOnWritePointer
26 // Description : This safely stores the primary, owned pointer to a
27 // CopyOnWriteObject. At any time, you may call
28 // get_read_pointer() or get_write_pointer() to get a
29 // read-only or modifiable pointer to the object stored.
30 //
31 // There may be multiple copies of a CopyOnWritePointer
32 // which all refer to the same shared object. They will
33 // negotiate with each other properly.
34 ////////////////////////////////////////////////////////////////////
35 class EXPCL_PANDA_PUTIL CopyOnWritePointer {
36 public:
37  INLINE CopyOnWritePointer(CopyOnWriteObject *object = NULL);
38  INLINE CopyOnWritePointer(const CopyOnWritePointer &copy);
39  INLINE void operator = (const CopyOnWritePointer &copy);
40  INLINE void operator = (CopyOnWriteObject *object);
41  INLINE ~CopyOnWritePointer();
42 
43 #ifdef USE_MOVE_SEMANTICS
44  INLINE CopyOnWritePointer(CopyOnWritePointer &&move) NOEXCEPT;
45  INLINE void operator = (CopyOnWritePointer &&move) NOEXCEPT;
46 #endif
47 
48  INLINE bool operator == (const CopyOnWritePointer &other) const;
49  INLINE bool operator != (const CopyOnWritePointer &other) const;
50  INLINE bool operator < (const CopyOnWritePointer &other) const;
51 
52 #ifdef COW_THREADED
53  CPT(CopyOnWriteObject) get_read_pointer() const;
54  PT(CopyOnWriteObject) get_write_pointer();
55 #else
56  INLINE const CopyOnWriteObject *get_read_pointer() const;
57  INLINE CopyOnWriteObject *get_write_pointer();
58 #endif // COW_THREADED
59 
60  INLINE CopyOnWriteObject *get_unsafe_pointer();
61 
62  INLINE bool is_null() const;
63  INLINE void clear();
64 
65  INLINE bool test_ref_count_integrity() const;
66  INLINE bool test_ref_count_nonzero() const;
67 
68 private:
69  CopyOnWriteObject *_cow_object;
70 };
71 
72 
73 ////////////////////////////////////////////////////////////////////
74 // Class : CopyOnWritePointerTo
75 // Description : A template wrapper around the above class, mainly to
76 // handle the little typecasting niceties.
77 ////////////////////////////////////////////////////////////////////
78 template <class T>
80 public:
81  // By hiding this template from interrogate, we improve compile-time
82  // speed and memory utilization.
83 #ifndef CPPPARSER
84  typedef T To;
85 
86  INLINE CopyOnWritePointerTo(To *object = NULL);
87  INLINE CopyOnWritePointerTo(const CopyOnWritePointerTo<T> &copy);
88  INLINE void operator = (const CopyOnWritePointerTo<T> &copy);
89  INLINE void operator = (To *object);
90 
91 #ifdef USE_MOVE_SEMANTICS
92  INLINE CopyOnWritePointerTo(CopyOnWritePointerTo &&move) NOEXCEPT;
93  INLINE void operator = (CopyOnWritePointerTo &&move) NOEXCEPT;
94 #endif
95 
96 #ifdef COW_THREADED
97  INLINE CPT(To) get_read_pointer() const;
98  INLINE PT(To) get_write_pointer();
99 #else
100  INLINE const To *get_read_pointer() const;
101  INLINE To *get_write_pointer();
102 #endif // COW_THREADED
103 
104  INLINE To *get_unsafe_pointer();
105 #endif // CPPPARSER
106 };
107 
108 #define COWPT(type) CopyOnWritePointerTo< type >
109 
110 #include "copyOnWritePointer.I"
111 
112 #endif
A template wrapper around the above class, mainly to handle the little typecasting niceties...
To * get_write_pointer()
See CopyOnWritePointer::get_write_pointer().
To * get_unsafe_pointer()
See CopyOnWritePointer::get_unsafe_pointer().
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
This safely stores the primary, owned pointer to a CopyOnWriteObject.
const To * get_read_pointer() const
See CopyOnWritePointer::get_read_pointer().