ALBA
albaSmartPointer.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaSmartPointer
5 Authors: based on vtkSmartPointer (www.vtk.org), rewritten by Marco Petrone
6
7 Copyright (c) BIC
8 All rights reserved. See Copyright.txt or
9
10
11 This software is distributed WITHOUT ANY WARRANTY; without even
12 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the above copyright notice for more information.
14
15=========================================================================*/
16#ifndef __albaSmartPointer_h
17#define __albaSmartPointer_h
18
19#include "albaConfigure.h"
20#include "albaBase.h"
21
22//----------------------------------------------------------------------------
23// forward declarations
24//----------------------------------------------------------------------------
26
31template <class T>
33{
34public:
36 albaAutoPointer(T* r=NULL,void *owner=NULL){
37 m_Object=r;
38 Register(owner);
39 }
40
41
46
49
54 // make use of a temp auto-ptr to safely unregister, this
55 // to avoid deallocation in case of self assignment
56 albaAutoPointer(r).Swap(*this);
57 return *this;
58 }
59
61 // make use of a temp auto-ptr to safely unregister, this
62 // to avoid deallocation in case of self assignment
63 albaAutoPointer<T> tmp(r);
64 tmp.Swap(*this); // swaps the two pointers
65 return *this;
66 }
67
68 bool operator==(const albaAutoPointer<T>& r) const {return r.m_Object==m_Object;}
69 bool operator==(const T *r) const {return r==m_Object;}
70
72 T* GetPointer() const {return m_Object;}
73
76 operator T *() const {return (T *)m_Object;}
77
79 T* operator->() const { return static_cast<T*>(this->m_Object); }
80
82 void UnRegister(void *owner){
83 if(m_Object)
84 {
85 m_Object->UnRegister(owner);
86 m_Object = NULL;
87 }
88 }
89
90protected:
91 // Internal utility methods.
93 T* temp = r.m_Object;
95 m_Object = temp;
96 }
97 void Register(void *owner){
98 if(m_Object)
99 {
100 m_Object->Register(owner);
101 }
102 }
103
105};
106
110template <class T>
112{
113public:
116 albaSmartPointer() {this->m_Object=T::New();this->Register(NULL);}
117
121
126};
127
128
129//----------------------------------------------------------------------------
130template <class T>
131inline bool operator == (const albaAutoPointer<T>& l, const albaAutoPointer<T>& r) \
132//----------------------------------------------------------------------------
133{ return (static_cast<void*>(l.GetPointer()) == static_cast<void*>(r.GetPointer())); }
134
135//----------------------------------------------------------------------------
136template <class T>
138//----------------------------------------------------------------------------
139{ return (static_cast<void*>(l) == static_cast<void*>(r.GetPointer())); }
140
141//----------------------------------------------------------------------------
142template <class T>
144//----------------------------------------------------------------------------
145{ return (static_cast<void*>(l.GetPointer()) == static_cast<void*>(r)); }
146
147
148#endif
149
bool operator==(const albaAutoPointer< T > &l, const albaAutoPointer< T > &r)
Hold a reference to a T instance.
T * m_Object
Pointer to the actual object.
void UnRegister(void *owner)
used to force releasing of internal object, specifying the owner
bool operator==(const T *r) const
T * GetPointer() const
Get the contained pointer.
albaAutoPointer(T *r=NULL, void *owner=NULL)
Initialize smart pointer to (optional) given object.
~albaAutoPointer()
Destroy smart pointer and remove the reference to its object.
bool operator==(const albaAutoPointer< T > &r) const
albaAutoPointer & operator=(const albaAutoPointer< T > &r)
albaAutoPointer(const albaAutoPointer &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
T * operator->() const
Provides normal pointer target member access using operator ->.
albaAutoPointer & operator=(T *r)
Assign object to reference.
void Swap(albaAutoPointer &r)
void Register(void *owner)
albaBase - the pourpose of this class is just to groups utilities classes
Definition: albaBase.h:27
albaReferenceCounted - abstract base class for ALBA objects with reference counting.
AutoPointer which self allocates the internal object.
albaSmartPointer()
Initialize smart pointer to a new instance of class T.
albaSmartPointer(const albaAutoPointer< T > &r)
Initialize smart pointer with a new reference to the same object referenced by given smart pointer.
albaSmartPointer(T *r)
Initialize smart pointer to given object pointer and reference the given object.