18 #ifndef LIBCWD_LOCKABLE_AUTO_PTR_H
19 #define LIBCWD_LOCKABLE_AUTO_PTR_H
25 #define LIBCWD_ASSERT(x)
28 #ifndef LIBCWD_PRIVATE_ASSERT_H
50 template<
class X,
bool array = false>
51 class lockable_auto_ptr {
52 typedef X element_type;
55 template<
class Y,
bool ARRAY>
friend class lockable_auto_ptr;
66 explicit lockable_auto_ptr(X* p = 0) : ptr(p), locked(false), owner(p) { }
69 lockable_auto_ptr(lockable_auto_ptr
const& r) :
70 ptr(r.ptr), locked(false), owner(r.owner && !r.locked)
71 {
if (!r.locked) r.owner = 0; }
75 lockable_auto_ptr(lockable_auto_ptr<Y, array>
const& r) :
76 ptr(r.ptr), locked(false), owner(r.owner && !r.locked)
77 {
if (!r.locked) r.owner = 0; }
85 lockable_auto_ptr& operator=(lockable_auto_ptr<Y, array>
const& r);
87 lockable_auto_ptr& operator=(lockable_auto_ptr
const& r) {
return operator= <X> (r); }
94 ~lockable_auto_ptr() {
if (owner) {
if (array)
delete [] ptr;
else delete ptr; } }
100 X& operator*()
const {
return *ptr; }
103 X* operator->()
const {
return ptr; }
106 X*
get()
const {
return ptr; }
109 bool strict_owner()
const { LIBCWD_ASSERT(is_owner());
return locked; }
114 bool is_owner(
void)
const {
return owner; }
137 X* release()
const { LIBCWD_ASSERT(is_owner()); owner = 0;
return ptr; }
141 void lock() { LIBCWD_ASSERT(is_owner()); locked =
true; }
145 void unlock() { locked =
false; }
149 template<
class X,
bool array>
151 lockable_auto_ptr<X, array>&
152 lockable_auto_ptr<X, array>::operator=(lockable_auto_ptr<Y, array>
const& r)
154 if ((
void*)&r != (
void*)
this)
177 #endif // LIBCWD_LOCKABLE_AUTO_PTR_H