28 #define tpl_assert(bed) arb_assert(bed)
44 #define SmartArrayPtr(type) SmartPtr<type, Counted<type, auto_delete_array_ptr<type> > >
45 #define SmartMallocPtr(type) SmartPtr<type, Counted<type, auto_free_ptr<type> > >
46 #define SmartCustomPtr(type, deallocator) SmartPtr<type, Counted<type, custom_dealloc_ptr<type, deallocator> > >
65 #ifdef DUMP_SMART_PTRS
66 #error Please do not define DUMP_SMART_PTRS in NDEBUG mode!
70 #ifdef DUMP_SMART_PTRS
71 #define DUMP_SMART_PTRS_DO(cmd) do { (cmd); } while (0)
73 #define DUMP_SMART_PTRS_DO(cmd)
82 template<
class T,
void (*DEALLOC)(T*)>
87 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p now controlled by custom_dealloc_ptr\n", thePointer));
90 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p gets destroyed by custom_dealloc_ptr (using fun %p)\n", thePointer, DEALLOC));
103 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p now controlled by auto_free_ptr\n", thePointer));
106 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p gets destroyed by auto_free_ptr\n", thePointer));
119 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p now controlled by auto_delete_ptr\n", thePointer));
122 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p gets destroyed by auto_delete_ptr\n", thePointer));
135 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p now controlled by auto_delete_array_ptr\n", thePointer));
138 DUMP_SMART_PTRS_DO(fprintf(stderr,
"pointer %p gets destroyed by auto_delete_array_ptr\n", thePointer));
139 delete [] thePointer;
151 template <
class T,
class AP>
190 template <
class T,
class C = Counted<T, auto_delete_ptr<T> > >
197 if (
object && object->free_reference()==0) {
198 DUMP_SMART_PTRS_DO(fprintf(stderr,
"Unbind() deletes Counted object %p (which hold pointer %p)\n",
object, object->getPointer()));
217 object->new_reference();
228 object = other.object;
229 if (
object)
object->new_reference();
232 if (other.object) other.object->new_reference();
234 object = other.object;
245 bool isSet()
const {
return object; }
258 object->new_reference();
264 return object ?
object->getPointer() :
NULp;
289 return object==other.object;
293 return isSet() ?
object->counter : 0;
310 #define RETURN_LOCAL_ALLOC(mallocation) \
311 static SmartMallocPtr(typeof(*mallocation)) static_ptr; \
312 static_ptr.assign(mallocation); \
313 return static_ptr.content();
315 #define RETURN_ONETIME_ALLOC(allocated) \
316 static SmartMallocPtr(typeof(*allocated)) static_ptr; \
317 if (static_ptr.isNull()) { static_ptr = allocated; } \
318 return static_ptr.content();
321 #error smartptr.h included twice
auto_delete_array_ptr(T *p)
unsigned references() const
const T * getPointer() const
const T * getPointer() const
const T * getPointer() const
bool operator==(const SmartPtr< T, C > &s1, const SmartPtr< T, C > &s2)
bool isNull() const
test if SmartPtr is NULp
const T * getPointer() const
void setNull()
set SmartPtr to NULp
SmartPtr< T, C > & operator=(const SmartPtr< T, C > &other)
const T * getPointer() const
bool isSet() const
test if SmartPtr is not NULp
#define SmartMallocPtr(type)
const T * content() const
convert SmartPtr to plain old pointer (also works if isNull())
SmartPtr()
build Smart-NULp-Ptr
bool operator!=(const SmartPtr< T, C > &s1, const SmartPtr< T, C > &s2)
unsigned free_reference()
const T & operator*() const
SmartPtr< T, C > deep_copy() const
bool sameObject(const SmartPtr< T, C > &other) const
void assign(T *p)
set SmartPtr to new content or NULp
SmartPtr(const SmartPtr< T, C > &other)
#define DUMP_SMART_PTRS_DO(cmd)
const T * operator->() const