Panda3D

Loading...
Searching...
No Matches
copyOnWritePointer.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file copyOnWritePointer.h
10 * @author drose
11 * @date 2007-04-09
12 */
13
14#ifndef COPYONWRITEPOINTER_H
15#define COPYONWRITEPOINTER_H
16
17#include "pandabase.h"
18
19#include "copyOnWriteObject.h"
20#include "pointerTo.h"
21#include "dcast.h"
22
23/**
24 * This safely stores the primary, owned pointer to a CopyOnWriteObject. At
25 * any time, you may call get_read_pointer() or get_write_pointer() to get a
26 * read-only or modifiable pointer to the object stored.
27 *
28 * There may be multiple copies of a CopyOnWritePointer which all refer to the
29 * same shared object. They will negotiate with each other properly.
30 */
31 class EXPCL_PANDA_PUTIL CopyOnWritePointer {
32 public:
33 INLINE CopyOnWritePointer(CopyOnWriteObject *object = nullptr);
34 INLINE CopyOnWritePointer(const CopyOnWritePointer &copy);
35 INLINE CopyOnWritePointer(CopyOnWritePointer &&from) noexcept;
36 INLINE CopyOnWritePointer(PointerTo<CopyOnWriteObject> &&from) noexcept;
37 INLINE ~CopyOnWritePointer();
38
39 INLINE void operator = (const CopyOnWritePointer &copy);
40 INLINE void operator = (CopyOnWritePointer &&from) noexcept;
41 INLINE void operator = (PointerTo<CopyOnWriteObject> &&from) noexcept;
42 INLINE void operator = (CopyOnWriteObject *object);
43
44 INLINE bool operator == (const CopyOnWritePointer &other) const;
45 INLINE bool operator != (const CopyOnWritePointer &other) const;
46 INLINE bool operator < (const CopyOnWritePointer &other) const;
47
48#ifdef COW_THREADED
49 CPT(CopyOnWriteObject) get_read_pointer(Thread *current_thread) const;
50 PT(CopyOnWriteObject) get_write_pointer();
51#else
52 INLINE const CopyOnWriteObject *get_read_pointer(Thread *current_thread) const;
53 INLINE CopyOnWriteObject *get_write_pointer();
54#endif // COW_THREADED
55
56 INLINE CopyOnWriteObject *get_unsafe_pointer();
57
58 INLINE bool is_null() const;
59 INLINE void clear();
60
61 INLINE bool test_ref_count_integrity() const;
62 INLINE bool test_ref_count_nonzero() const;
63
64 protected:
65 CopyOnWriteObject *_cow_object;
66};
67
68
69/**
70 * A template wrapper around the above class, mainly to handle the little
71 * typecasting niceties.
72 */
73 template <class T>
74 class CopyOnWritePointerTo : public CopyOnWritePointer {
75 public:
76 // By hiding this template from interrogate, we improve compile-time speed
77 // and memory utilization.
78#ifndef CPPPARSER
79 typedef T To;
80
81 INLINE CopyOnWritePointerTo(To *object = nullptr);
82 INLINE CopyOnWritePointerTo(const CopyOnWritePointerTo<T> &copy);
83 INLINE CopyOnWritePointerTo(CopyOnWritePointerTo &&from) noexcept;
84 INLINE CopyOnWritePointerTo(PointerTo<T> &&from) noexcept;
85
86 INLINE void operator = (const CopyOnWritePointerTo<T> &copy);
87 INLINE void operator = (To *object);
88 INLINE void operator = (CopyOnWritePointerTo &&from) noexcept;
89 INLINE void operator = (PointerTo<T> &&from) noexcept;
90
91#ifdef COW_THREADED
92 INLINE CPT(To) get_read_pointer(Thread *current_thread = Thread::get_current_thread()) const;
93 INLINE PT(To) get_write_pointer();
94#else
95 INLINE const To *get_read_pointer(Thread *current_thread = Thread::get_current_thread()) const;
96 INLINE To *get_write_pointer();
97#endif // COW_THREADED
98
99 INLINE To *get_unsafe_pointer();
100#endif // CPPPARSER
101};
102
103#define COWPT(type) CopyOnWritePointerTo< type >
104
105#include "copyOnWritePointer.I"
106
107#endif
CopyOnWriteObject
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
Definition copyOnWriteObject.h:41
CopyOnWritePointerTo::get_unsafe_pointer
To * get_unsafe_pointer()
See CopyOnWritePointer::get_unsafe_pointer().
Definition copyOnWritePointer.I:416
CopyOnWritePointerTo::get_write_pointer
To * get_write_pointer()
See CopyOnWritePointer::get_write_pointer().
Definition copyOnWritePointer.I:404
CopyOnWritePointerTo::get_read_pointer
const To * get_read_pointer(Thread *current_thread=Thread::get_current_thread()) const
See CopyOnWritePointer::get_read_pointer().
Definition copyOnWritePointer.I:376
CopyOnWritePointer::is_null
bool is_null() const
Returns true if the CopyOnWritePointer contains a NULL pointer, false otherwise.
Definition copyOnWritePointer.I:212
CopyOnWritePointer::get_read_pointer
const CopyOnWriteObject * get_read_pointer(Thread *current_thread) const
Returns a pointer locked for read.
Definition copyOnWritePointer.I:167
CopyOnWritePointer::test_ref_count_integrity
bool test_ref_count_integrity() const
Does some easy checks to make sure that the reference count isn't completely bogus.
Definition copyOnWritePointer.I:232
CopyOnWritePointer::get_unsafe_pointer
CopyOnWriteObject * get_unsafe_pointer()
Returns an unlocked pointer that you can write to.
Definition copyOnWritePointer.I:203
CopyOnWritePointer::test_ref_count_nonzero
bool test_ref_count_nonzero() const
Does some easy checks to make sure that the reference count isn't zero, or completely bogus.
Definition copyOnWritePointer.I:242
CopyOnWritePointer::get_write_pointer
CopyOnWriteObject * get_write_pointer()
Returns a pointer locked for write.
Definition copyOnWritePointer.I:183
CopyOnWritePointer::clear
void clear()
Sets the pointer to NULL.
Definition copyOnWritePointer.I:220
PointerTo
This file defines the classes PointerTo and ConstPointerTo (and their abbreviations,...
Definition pointerTo.h:69
Thread
A thread; that is, a lightweight process.
Definition thread.h:46
Thread::get_current_thread
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition thread.h:109
copyOnWriteObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
copyOnWritePointer.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcast.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pointerTo.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.