-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.h
More file actions
176 lines (141 loc) · 4.21 KB
/
Object.h
File metadata and controls
176 lines (141 loc) · 4.21 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
#include <atomic>
namespace mvm {
template<typename T>
class ObjectPtr {
public:
ObjectPtr() {}
virtual ~ObjectPtr() {
reset();
}
ObjectPtr(T* node) : mPtr(node) {
if (mPtr != NULL) {
mPtr->incRef();
}
}
void swap(ObjectPtr<T>& other) {
std::swap(mPtr, other.mPtr);
}
void reset() {
if (mPtr != NULL) {
mPtr->decRef();
mPtr = NULL;
}
}
//Any object refer to mPtr cannot delete mPtr
T* release() {
T* r = mPtr;
mPtr = NULL;
return r;
}
ObjectPtr(const ObjectPtr<T>& other) : ObjectPtr(other.mPtr) {
}
template<typename U>
ObjectPtr(const ObjectPtr<U>& other) : ObjectPtr(other.mPtr) {
static_assert(std::is_base_of<T, U>::value,
"can only assign of child class ObjectPtr to parent");
}
ObjectPtr(ObjectPtr<T>&& other) : mPtr(other.mPtr) {
other.mPtr = NULL;
}
template<typename U>
ObjectPtr(ObjectPtr<U>&& other) : mPtr(other.mPtr) {
static_assert(std::is_base_of<T, U>::value,
"can only assign of child class ObjectPtr to parent");
other.mPtr = NULL;
}
ObjectPtr<T>& operator=(ObjectPtr<T>&& other) {
ObjectPtr<T>(std::move(other)).swap(*this);
return *this;
}
ObjectPtr<T>& operator=(const ObjectPtr<T>& other) {
ObjectPtr<T>(other).swap(*this);
return *this;
}
T& operator*() const {
return *mPtr;
}
T* operator->() const {
return get();
}
T* get() const {
return mPtr;
};
bool defined() const {
return get() != NULL;
}
int useCount() const {
return mPtr != NULL ? mPtr->useCount() : 0;
}
inline bool unique() const {
return mPtr != NULL && mPtr->useCount() == 1;
};
//inline operator bool() const {
// return defined();
//}
inline bool operator==(const ObjectPtr<T>& other) const {
return mPtr == other.mPtr;
}
inline bool operator!=(const ObjectPtr<T>& other) const {
return mPtr != other.mPtr;
}
inline bool operator<(const ObjectPtr<T>& other) const {
return mPtr < other.mPtr;
}
inline bool operator<=(const ObjectPtr<T>& other) const {
return mPtr <= other.mPtr;
}
private:
T* mPtr{NULL};
};
class ObjectNode;
class Object : public ObjectPtr<ObjectNode> {
public:
Object() = default;
explicit Object(ObjectNode* node) : ObjectPtr<ObjectNode>(node) {
}
explicit Object(const ObjectNode* node)
: ObjectPtr<ObjectNode>(const_cast<ObjectNode*>(node)) {
}
template<typename NODE>
inline const NODE* as() const {
return dynamic_cast<const NODE*>(get());
}
using ContainerType = ObjectNode;
};
class ObjectNode {
public:
virtual ~ObjectNode() = default;
int useCount() const {
return mCount.load(std::memory_order_relaxed);
}
protected:
void decRef() {
if (mCount.fetch_sub(1, std::memory_order_release) == 1) {
std::atomic_thread_fence(std::memory_order_acquire);
delete this;
}
}
void incRef() {
mCount.fetch_add(1, std::memory_order_relaxed);
}
private:
std::atomic<int> mCount{0};
template<typename>
friend class ObjectPtr;
};
#define MVM_OBJECT_METHODS(TypeName, ParentType, NodeType) \
explicit TypeName(NodeType* node) : ParentType(node) { \
} \
explicit TypeName(const NodeType* node) : ParentType(node) { \
} \
NodeType* operator->() const { \
return get(); \
} \
NodeType& operator*() const { \
return *get(); \
} \
NodeType* get() const { \
return static_cast<NodeType*>(ParentType::get()); \
}
} // namespace mvm