33 #ifndef __SMART_POINTER__
34 #define __SMART_POINTER__
38 #include <boost/pointee.hpp>
53 static void free(
const T* data) {
if(data)
delete data;}
62 static void free(
const T* data) {
if(data)
delete[] data;}
71 static void free(
const T* data) {data->Release;}
106 template <
typename T,
template <
class TT>
class FreePolicy>
125 template <
class TPtr>
171 return (this->
get() == sp.
get());
186 template <
class TPtr>
188 return (this->
get() == sp.
get());
191 template <
class TPtr>
209 explicit operator bool() const noexcept {
return m_ptr != NULL; }
215 template <
class TDest>
217 TDest*
p =
dynamic_cast<TDest*
>(
m_ptr);
223 template <
class TDest>
225 TDest*
p =
static_cast<TDest*
>(
m_ptr);
231 template <
class TDest>
233 TDest*
p =
reinterpret_cast<TDest*
>(
m_ptr);
239 template <
class TDest>
242 return dynamic_cast<TDest*
>(
m_ptr) != NULL;
278 FreePolicy<T>::free(
m_ptr);
286 FreePolicy<T>::free(
reinterpret_cast<const T*
>(ptr));
294 template <
typename T,
template <
class TT>
class FreePolicy>
311 template <
class TPtr>
319 template <
class TPtr>
384 return (this->
get() == sp.
get());
387 template <
class TPtr>
389 return (this->
get() == sp.
get());
400 template <
class TPtr>
406 return m_ptr != NULL;
417 explicit operator bool() const noexcept {
return m_ptr != NULL; }
423 template <
class TDest>
425 const TDest*
p =
dynamic_cast<const TDest*
>(
m_ptr);
431 template <
class TDest>
433 const TDest*
p =
static_cast<const TDest*
>(
m_ptr);
439 template <
class TDest>
441 const TDest*
p =
reinterpret_cast<const TDest*
>(
m_ptr);
452 template <
class TDest>
455 return dynamic_cast<TDest*
>(
m_ptr) != NULL;
484 FreePolicy<T>::free(
m_ptr);
491 FreePolicy<T>::free(
reinterpret_cast<T*
>(ptr));
503 template <
typename T,
template <
class TT>
class FreePolicy>
535 m_refCountPtr(sp.m_refCountPtr),
536 m_freeFunc(sp.m_freeFunc)
538 if(m_refCountPtr) (*m_refCountPtr)++;
541 explicit SmartPtr(
void* ptr,
void (*freeFunc)(
const void*)) :
546 if(ptr) m_refCountPtr =
new int(1);
555 if(m_refCountPtr) (*m_refCountPtr)++;
598 template <
class T,
template <
class TPtr>
class TFreePolicy>
607 template <
class T,
template <
class TPtr>
class TFreePolicy>
618 explicit operator bool() const noexcept {
return m_ptr != NULL; }
628 int refcount()
const {
if(m_refCountPtr)
return *m_refCountPtr;
return 0;}
635 if((*m_refCountPtr) < 1)
637 delete m_refCountPtr;
645 void (*m_freeFunc)(
const void*);
659 if(ptr) m_refCountPtr =
new int(1);
666 m_refCountPtr(sp.m_refCountPtr),
667 m_freeFunc(sp.m_freeFunc)
669 if(m_refCountPtr) (*m_refCountPtr)++;
674 m_refCountPtr(sp.m_refCountPtr),
675 m_freeFunc(sp.m_freeFunc)
677 if(m_refCountPtr) (*m_refCountPtr)++;
680 template <
class T,
template <
class TPtr>
class TFreePolicy>
686 if(m_refCountPtr) (*m_refCountPtr)++;
689 template <
class T,
template <
class TPtr>
class TFreePolicy>
695 if(m_refCountPtr) (*m_refCountPtr)++;
724 template <
class T,
template <
class TPtr>
class TFreePolicy>
737 template <
class T,
template <
class TPtr>
class TFreePolicy>
762 template <
class T,
template <
class TPtr>
class TFreePolicy>
771 template <
class T,
template <
class TPtr>
class TFreePolicy>
782 explicit operator bool() const noexcept {
return m_ptr != NULL; }
791 int refcount()
const {
if(m_refCountPtr)
return *m_refCountPtr;
return 0;}
798 if((*m_refCountPtr) < 1)
800 delete m_refCountPtr;
801 m_freeFunc(
const_cast<void*
>(
m_ptr));
808 void (*m_freeFunc)(
const void*);
815 template <
class T,
template <
class TPtr>
class TFreePolicy>
817 #
if (__cplusplus < 201103L)
818 :
public binary_function<SmartPtr<T, TFreePolicy>, SmartPtr<T, TFreePolicy>, bool>
824 return less<T*>()(lhs.
get(), rhs.
get());
835 template <
typename T,
template <
class TT>
class FreePolicy>
842 template <
typename T>
Definition: smart_pointer.h:650
ConstSmartPtr(const SmartPtr< void > &sp)
Definition: smart_pointer.h:664
ConstSmartPtr()
Definition: smart_pointer.h:652
void release()
Definition: smart_pointer.h:794
const void * m_ptr
Definition: smart_pointer.h:806
ConstSmartPtr< void > & operator=(const ConstSmartPtr< T, TFreePolicy > &sp)
Definition: smart_pointer.h:738
ConstSmartPtr< T, TFreePolicy > cast_reinterpret() const
Returns a SmartPtr with the specified type and shared reference counting.
Definition: smart_pointer.h:763
int * m_refCountPtr
Definition: smart_pointer.h:807
ConstSmartPtr< void > & operator=(const ConstSmartPtr< void > &sp)
Definition: smart_pointer.h:712
ConstSmartPtr(const ConstSmartPtr< void > &sp)
Definition: smart_pointer.h:672
const void * get() const
Definition: smart_pointer.h:789
ConstSmartPtr(NullSmartPtr)
Definition: smart_pointer.h:662
ConstSmartPtr< void > & operator=(const SmartPtr< void > &sp)
Definition: smart_pointer.h:700
ConstSmartPtr(const SmartPtr< T, TFreePolicy > &sp)
Definition: smart_pointer.h:681
void invalidate()
Definition: smart_pointer.h:787
ConstSmartPtr(void *ptr, void(*freeFunc)(const void *))
Definition: smart_pointer.h:654
int refcount() const
Definition: smart_pointer.h:791
void(* m_freeFunc)(const void *)
Definition: smart_pointer.h:808
bool invalid() const
returns true if the pointer is invalid, false if not.
Definition: smart_pointer.h:785
~ConstSmartPtr()
Definition: smart_pointer.h:698
ConstSmartPtr(const ConstSmartPtr< T, TFreePolicy > &sp)
Definition: smart_pointer.h:690
ConstSmartPtr< void > & operator=(NullSmartPtr)
Definition: smart_pointer.h:750
bool valid() const
returns true if the pointer is valid, false if not.
Definition: smart_pointer.h:779
ConstSmartPtr< void > & operator=(const SmartPtr< T, TFreePolicy > &sp)
Definition: smart_pointer.h:725
void set_impl(const void *ptr)
sets the void* to a different location correspoding to a cast to a new type T
Definition: smart_pointer.h:772
Definition: smart_pointer.h:296
const T & operator*() const
Definition: smart_pointer.h:331
bool operator==(NullSmartPtr) const
Definition: smart_pointer.h:392
bool valid() const
returns true if the pointer is valid, false if not.
Definition: smart_pointer.h:414
ConstSmartPtr(const T *ptr)
Definition: smart_pointer.h:301
int * m_refCount
Definition: smart_pointer.h:496
ConstSmartPtr< TDest, FreePolicy > cast_reinterpret() const
performs a static cast
Definition: smart_pointer.h:440
bool invalid() const
returns true if the pointer is invalid, false if not.
Definition: smart_pointer.h:420
ConstSmartPtr & operator=(const ConstSmartPtr &sp)
Definition: smart_pointer.h:354
bool is_of_type() const
Definition: smart_pointer.h:453
ConstSmartPtr(const ConstSmartPtr< TPtr, FreePolicy > &sp)
Definition: smart_pointer.h:320
ConstSmartPtr< T, FreePolicy > & operator=(const SmartPtr< TIn, FreePolicy > &sp)
Definition: smart_pointer.h:344
ConstSmartPtr< T, FreePolicy > & operator=(const ConstSmartPtr< TIn, FreePolicy > &sp)
Definition: smart_pointer.h:365
ConstSmartPtr & operator=(NullSmartPtr)
Definition: smart_pointer.h:375
static void free_void_ptr(void *ptr)
Definition: smart_pointer.h:490
ConstSmartPtr()
Definition: smart_pointer.h:300
ConstSmartPtr(const SmartPtr< TPtr, FreePolicy > &sp)
Definition: smart_pointer.h:312
SmartPtr< T, FreePolicy > cast_const() const
performs a const cast
Definition: smart_pointer.h:447
ConstSmartPtr & operator=(const SmartPtr< T, FreePolicy > &sp)
Definition: smart_pointer.h:333
const T * m_ptr
Definition: smart_pointer.h:495
ConstSmartPtr(NullSmartPtr)
Definition: smart_pointer.h:302
int refcount() const
Definition: smart_pointer.h:411
ConstSmartPtr(const T *ptr, int *refCount)
WARNING: this method is DANGEROUS!
Definition: smart_pointer.h:461
const T * operator->() const
Definition: smart_pointer.h:329
bool operator!=(const ConstSmartPtr &sp) const
Definition: smart_pointer.h:396
bool operator!=(NullSmartPtr) const
Definition: smart_pointer.h:405
int * refcount_ptr() const
WARNING: this method is dangerous!
Definition: smart_pointer.h:472
bool operator==(const SmartPtr< TPtr, FreePolicy > &sp) const
Definition: smart_pointer.h:388
~ConstSmartPtr()
Definition: smart_pointer.h:327
ConstSmartPtr(const ConstSmartPtr &sp)
Definition: smart_pointer.h:303
ConstSmartPtr< TDest, FreePolicy > cast_dynamic() const
preforms a dynamic cast
Definition: smart_pointer.h:424
ConstSmartPtr< TDest, FreePolicy > cast_static() const
performs a static cast
Definition: smart_pointer.h:432
bool operator==(const ConstSmartPtr &sp) const
Definition: smart_pointer.h:383
const T * get() const
Definition: smart_pointer.h:409
void release()
decrements the refCount and frees the encapsulated pointer if required.
Definition: smart_pointer.h:476
bool operator!=(const SmartPtr< TPtr, FreePolicy > &sp) const
Definition: smart_pointer.h:401
Definition: smart_pointer.h:60
static void free(const T *data)
Definition: smart_pointer.h:62
Definition: smart_pointer.h:51
static void free(const T *data)
Definition: smart_pointer.h:53
Definition: smart_pointer.h:69
static void free(const T *data)
Definition: smart_pointer.h:71
Used to construct empty smart pointers.
Definition: smart_pointer.h:84
NullSmartPtr()
Definition: smart_pointer.h:86
Definition: smart_pointer.h:525
void release()
Definition: smart_pointer.h:631
void invalidate()
Definition: smart_pointer.h:623
~SmartPtr()
Definition: smart_pointer.h:558
SmartPtr(NullSmartPtr)
Definition: smart_pointer.h:531
SmartPtr< void > & operator=(const SmartPtr< void > &sp)
Definition: smart_pointer.h:560
int refcount() const
Definition: smart_pointer.h:628
bool invalid() const
returns true if the pointer is invalid, false if not.
Definition: smart_pointer.h:621
SmartPtr< void > & operator=(const SmartPtr< T > &sp)
Definition: smart_pointer.h:573
SmartPtr< void > & operator=(NullSmartPtr)
Definition: smart_pointer.h:586
void(* m_freeFunc)(const void *)
Definition: smart_pointer.h:645
void set_impl(void *ptr)
sets the void* to a different location correspoding to a cast to a new type T
Definition: smart_pointer.h:608
SmartPtr(const SmartPtr< void > &sp)
Definition: smart_pointer.h:533
SmartPtr(const SmartPtr< T > &sp)
Definition: smart_pointer.h:550
SmartPtr(void *ptr, void(*freeFunc)(const void *))
Definition: smart_pointer.h:541
SmartPtr()
Definition: smart_pointer.h:529
void * get()
Definition: smart_pointer.h:625
const void * get() const
Definition: smart_pointer.h:626
SmartPtr< T, TFreePolicy > cast_reinterpret() const
Returns a SmartPtr with the specified type and shared reference counting.
Definition: smart_pointer.h:599
void * m_ptr
Definition: smart_pointer.h:643
bool valid() const
returns true if the pointer is valid, false if not.
Definition: smart_pointer.h:615
int * m_refCountPtr
Definition: smart_pointer.h:644
Definition: smart_pointer.h:108
SmartPtr< TDest, FreePolicy > cast_static() const
performs a static cast
Definition: smart_pointer.h:224
bool operator!=(NullSmartPtr) const
Definition: smart_pointer.h:182
SmartPtr & operator=(const SmartPtr &sp)
Definition: smart_pointer.h:149
T * get_nonconst() const
WARNING: this method is DANGEROUS!
Definition: smart_pointer.h:265
const T * operator->() const
Definition: smart_pointer.h:136
SmartPtr< TDest, FreePolicy > cast_reinterpret() const
performs a reinterpret cast
Definition: smart_pointer.h:232
SmartPtr< T, FreePolicy > & operator=(const SmartPtr< TIn, FreePolicy > &sp)
Definition: smart_pointer.h:160
T * m_ptr
Definition: smart_pointer.h:290
SmartPtr(NullSmartPtr)
Definition: smart_pointer.h:116
static void free_void_ptr(const void *ptr)
this release method is required by SmartPtr<void>
Definition: smart_pointer.h:285
SmartPtr(const SmartPtr &sp)
Definition: smart_pointer.h:117
T * get()
returns encapsulated pointer
Definition: smart_pointer.h:197
bool operator==(const ConstSmartPtr< TPtr, FreePolicy > &sp) const
Definition: smart_pointer.h:187
int * refcount_ptr() const
WARNING: this method is DANGEROUS!
Definition: smart_pointer.h:263
int * m_refCount
Definition: smart_pointer.h:291
const T & operator*() const
Definition: smart_pointer.h:139
bool is_of_type() const
Definition: smart_pointer.h:240
SmartPtr< TDest, FreePolicy > cast_dynamic() const
preforms a dynamic cast
Definition: smart_pointer.h:216
bool operator==(NullSmartPtr) const
Definition: smart_pointer.h:178
bool operator!=(const ConstSmartPtr< TPtr, FreePolicy > &sp) const
Definition: smart_pointer.h:192
ConstSmartPtr< T, FreePolicy > cast_const() const
performs a const cast
Definition: smart_pointer.h:504
SmartPtr()
Definition: smart_pointer.h:114
bool valid() const
returns true if the pointer is valid, false if not.
Definition: smart_pointer.h:206
SmartPtr(T *ptr, int *refCount)
WARNING: this method is DANGEROUS!
Definition: smart_pointer.h:251
SmartPtr & operator=(NullSmartPtr)
Definition: smart_pointer.h:141
void release()
decrements the refCount and frees the encapsulated pointer if required.
Definition: smart_pointer.h:270
int refcount() const
returns refcount
Definition: smart_pointer.h:203
SmartPtr(const SmartPtr< TPtr, FreePolicy > &sp)
Definition: smart_pointer.h:126
~SmartPtr()
Definition: smart_pointer.h:133
T & operator*()
Definition: smart_pointer.h:138
const T * get() const
returns encapsulated pointer
Definition: smart_pointer.h:200
SmartPtr(T *ptr)
Definition: smart_pointer.h:115
bool operator==(const SmartPtr &sp) const
Definition: smart_pointer.h:170
bool invalid() const
returns true if the pointer is invalid, false if not.
Definition: smart_pointer.h:212
bool operator!=(const SmartPtr &sp) const
Definition: smart_pointer.h:174
T * operator->()
Definition: smart_pointer.h:135
const NullSmartPtr SPNULL
The equivalent to NULL for smart pointers.
Definition: smart_pointer.h:90
if(!(yy_init))
Definition: lexer.cpp:997
Definition: boost_serialization_routines.h:49
Definition: smart_pointer.h:814
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836
T type
Definition: smart_pointer.h:859
T type
Definition: smart_pointer.h:853
bool operator()(const SmartPtr< T, TFreePolicy > &lhs, const SmartPtr< T, TFreePolicy > &rhs) const
Definition: smart_pointer.h:821