Go to the documentation of this file.
5 #ifndef CNOID_UTIL_REFERENCED_H
6 #define CNOID_UTIL_REFERENCED_H
17 #include "exportdecl.h"
30 isObjectAlive_ =
true;
34 void add() { ++weakCount; }
37 if(weakCount.fetch_sub(1) == 1){
43 return isObjectAlive_;
47 std::atomic<int> weakCount;
48 std::atomic<bool> isObjectAlive_;
60 mutable std::atomic<int> refCount_;
68 void releaseRef()
const {
70 if(refCount_.fetch_sub(1) == 1) {
76 void decrementRef()
const {
84 WeakCounter* weakCounter(){
86 weakCounter_ =
new WeakCounter();
96 int refCount()
const {
return refCount_.load(); }
182 operator T*()
const {
187 assert(px !=
nullptr);
192 assert(px !=
nullptr);
207 friend struct std::hash<
ref_ptr<T>>;
213 return a.
get() == b.
get();
218 return a.
get() != b.
get();
253 return static_cast<T*
>(p.
get());
258 return const_cast<T*
>(p.
get());
263 return dynamic_cast<T*
>(p.
get());
280 counter = px->weakCounter();
303 px = rhs.
lock().get();
309 px = rhs.
lock().get();
343 explicit operator bool()
const {
return px !=
nullptr; }
366 counter = other.counter;
367 other.counter = counter_;
371 return counter == rhs.counter;
374 return counter < rhs.counter;
407 struct hash<
cnoid::ref_ptr<T>>
412 return hash<T*>()(p.px);
417 struct hash<
cnoid::weak_ref_ptr<T>>
422 return hash<cnoid::WeakCounter*>()(p.counter);
int refCount() const
Definition: Referenced.h:96
ref_ptr< T > lock() const
Definition: Referenced.h:345
bool isObjectAlive()
Definition: Referenced.h:42
weak_ref_ptr & operator=(weak_ref_ptr< Y > const &rhs)
Definition: Referenced.h:302
ref_ptr & operator=(ref_ptr const &rhs)
Definition: Referenced.h:149
ref_ptr(ref_ptr const &rhs)
Definition: Referenced.h:123
friend class ref_ptr
Definition: Referenced.h:205
void reset(T *rhs)
Definition: Referenced.h:163
T & operator*() const
Definition: Referenced.h:186
weak_ref_ptr(weak_ref_ptr< Y > const &rhs)
Definition: Referenced.h:293
ref_ptr(T *p)
Definition: Referenced.h:110
Definition: Referenced.h:276
bool _internal_equal(weak_ref_ptr< Y > const &rhs) const
Definition: Referenced.h:370
T element_type
Definition: Referenced.h:288
T * operator->() const
Definition: Referenced.h:191
weak_ref_ptr(weak_ref_ptr const &rhs)
Definition: Referenced.h:297
CNOID_EXPORT std::ostream & operator<<(std::ostream &os, const BoundingBox &bb)
Definition: BoundingBox.cpp:160
weak_ref_ptr()
Definition: Referenced.h:290
ref_ptr< T > dynamic_pointer_cast(ref_ptr< U > const &p)
Definition: Referenced.h:261
Definition: Referenced.h:103
T element_type
Definition: Referenced.h:106
WeakCounter()
Definition: Referenced.h:29
weak_ref_ptr(ref_ptr< Y > const &rhs)
Definition: Referenced.h:327
void release()
Definition: Referenced.h:36
Definition: AbstractSceneLoader.h:11
~ref_ptr()
Definition: Referenced.h:129
ref_ptr & operator=(T *rhs)
Definition: Referenced.h:154
ref_ptr< Referenced > ReferencedPtr
Definition: Referenced.h:273
ref_ptr & operator=(ref_ptr &&rhs)
Definition: Referenced.h:144
void swap(ref_ptr< T > &lhs, ref_ptr< T > &rhs)
Definition: Referenced.h:246
weak_ref_ptr & operator=(ref_ptr< Y > const &rhs)
Definition: Referenced.h:337
bool operator==(ref_ptr< T > const &a, ref_ptr< U > const &b)
Definition: Referenced.h:211
friend class weak_ref_ptr
Definition: Referenced.h:381
void swap(ref_ptr &rhs)
Definition: Referenced.h:196
ref_ptr< T > static_pointer_cast(ref_ptr< U > const &p)
Definition: Referenced.h:251
ref_ptr< T > const_pointer_cast(ref_ptr< U > const &p)
Definition: Referenced.h:256
weak_ref_ptr(weak_ref_ptr &&rhs)
Definition: Referenced.h:314
weak_ref_ptr(Y *const &rhs)
Definition: Referenced.h:332
Definition: Referenced.h:54
Definition: Referenced.h:26
bool expired() const
Definition: Referenced.h:353
ref_ptr()
Definition: Referenced.h:108
Referenced(const Referenced &)
Definition: Referenced.h:93
void reset()
Definition: Referenced.h:357
bool _internal_less(weak_ref_ptr< Y > const &rhs) const
Definition: Referenced.h:373
void add()
Definition: Referenced.h:34
bool operator!=(ref_ptr< T > const &a, ref_ptr< U > const &b)
Definition: Referenced.h:216
ref_ptr & operator=(ref_ptr< U > const &rhs)
Definition: Referenced.h:135
bool operator<(ref_ptr< T > const &a, ref_ptr< T > const &b)
Definition: Referenced.h:241
ref_ptr(ref_ptr< U > const &rhs)
Definition: Referenced.h:117
Referenced()
Definition: Referenced.h:92
ref_ptr(ref_ptr &&rhs) noexcept
Definition: Referenced.h:140
void reset()
Definition: Referenced.h:159
weak_ref_ptr & operator=(weak_ref_ptr const &rhs)
Definition: Referenced.h:308
T * retn()
Definition: Referenced.h:172
T * get() const
Definition: Referenced.h:168
void swap(weak_ref_ptr &other)
Definition: Referenced.h:361
weak_ref_ptr & operator=(weak_ref_ptr &&rhs)
Definition: Referenced.h:319