/* ========================================================================
** Extended Template and Library
** Template Smart Pointer Implementation
-** $Id: _smart_ptr.h,v 1.1.1.1 2005/01/04 01:31:48 darco Exp $
+** $Id$
**
** Copyright (c) 2002 Robert B. Quattlebaum Jr.
**
typedef D destructor_type;
#ifdef DOXYGEN_SHOULD_SKIP_THIS // #ifdef is not a typo
-private:
+private:
#endif
value_type *obj; //!< \internal Pointer to object
reference_counter refcount;
obj=(pointer)x.get();
refcount=x.refcount;
}
-
+
return *this;
}
{
if(x.get()==obj)
return *this;
-
+
reset();
if(x.obj)
{
-
+
obj=(pointer)x.get();
refcount=x.refcount;
}
-
+
return *this;
}
//! smart_ptr reset procedure
void
reset()
- {
+ {
if(obj)
- {
+ {
if(refcount.unique()) destructor_type()(obj);
refcount.detach();
obj=0;
}
}
-
+
void spawn() { operator=(smart_ptr(new T)); }
//! Returns number of instances
pointer operator->()const { assert(obj); return obj; }
- //! Overloaded cast operator -- useful for implicit casts
- template <class U>
- operator smart_ptr<U>()
- {
- // This next line should provide a syntax check
- // to make sure that this cast makes sense.
- // If it doesn't, this should have a compiler error.
- // Otherwise, it should get optimized right out
- // of the code.
- //(U*)obj;
-
- return *reinterpret_cast<smart_ptr<U>*>(this);
- }
operator smart_ptr<const value_type>()const
{ return smart_ptr<const value_type>(static_cast<const_pointer>(obj)); }
bool operator!()const { return !obj; }
+ //! Overloaded cast operator -- useful for implicit casts
+ template <class U>
+ operator smart_ptr<U>()
+ {
+ // This next line should provide a syntax check
+ // to make sure that this cast makes sense.
+ // If it doesn't, this should have a compiler error.
+ // Otherwise, it should get optimized right out
+ // of the code.
+ //(U*)obj;
+
+ return *reinterpret_cast<smart_ptr<U>*>(this);
+ }
+
}; // END of template class smart_ptr
// ========================================================================
const count_type& count()const { return refcount; }
bool unique()const { return refcount.unique(); }
-
+
reference operator*()const { assert(obj); return *obj; }
pointer operator->()const { assert(obj); return obj; }