This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefcnt.h
142 lines (114 loc) · 3.33 KB
/
refcnt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @file refcnt.h
* @brief Declarations for RefCnt class
* @author Nicu Tofan <nicu.tofan@gmail.com>
* @copyright Copyright 2014 piles contributors. All rights reserved.
* This file is released under the
* [MIT License](http://opensource.org/licenses/mit-license.html)
*/
#ifndef GUARD_REFCNT_H_INCLUDE
#define GUARD_REFCNT_H_INCLUDE
#include <refcnt/refcnt-config.h>
//! A reference counted base class.
class REFCNT_EXPORT RefCnt {
private:
int ref_cnt_; ///< The actual reference count.
# ifdef REFCNT_DEBUG
void ** owners_list_; ///< List of owners - pointer.
int owners_list_used_; ///< List of owners - number of
///< used entries.
int owners_list_alloc_; ///< List of owners - number of
///< allocated entries.
# endif // REFCNT_DEBUG
protected:
//! Default constructor.
RefCnt ();
//! Destructor.
virtual ~RefCnt();
//! Copy constructor.
RefCnt (const RefCnt& other) :
ref_cnt_(other.ref_cnt_),
owners_list_(other.owners_list_),
owners_list_used_(other.owners_list_used_),
owners_list_alloc_(other.owners_list_alloc_)
{}
//! assignment operator
RefCnt& operator= (const RefCnt& other) {
ref_cnt_ = other.ref_cnt_;
owners_list_ = other.owners_list_;
owners_list_used_ = other.owners_list_used_;
owners_list_alloc_ = other.owners_list_alloc_;
return *this;
}
public:
//! increments the reference counter
///
/// @return the value of the reference counter after increment.
int
reference (
void * owner);
//! decrements the reference counter
///
/// @return the value of the reference counter after decrement.
int
dereference (
void * owner);
//! decrements the reference counter and deletes the instance if 0
///
/// The caller must make sure that the reference count does not go below 0.
/// @return the value of the reference counter after decrement.
int
release (
void * owner);
//! get the value of the reference count
///
/// @return the value of the reference counter after decrement.
int
referenceCount () {
return ref_cnt_;
}
//! deletes the instance
///
/// @return the value of the reference counter.
int
forceRelease ();
//! change the owner of a reference
///
/// This does absolutely nothing in release builds.
# ifdef REFCNT_DEBUG
void
changeOwner (
void * current_owner,
void * new_owner);
# else // REFCNT_DEBUG
inline void
changeOwner (
void *,
void *) {}
# endif // REFCNT_DEBUG
//! tell if a pointer owns a reference to this object
///
/// In release builds this always returns false.
# ifdef REFCNT_DEBUG
bool
isOwner (
void * value);
# else // REFCNT_DEBUG
inline bool
isOwner (
void *) {
return false;
}
# endif // REFCNT_DEBUG
# ifdef REFCNT_DEBUG
//! print the list of owners for this instance
void
printReferenceList ();
//! locate an owner (-1 if not found)
int
locateOwner (
void * owner);
# endif // REFCNT_DEBUG
public: virtual void anchorVtable() const;
};
#endif // GUARD_REFCNT_H_INCLUDE