Main Page   Modules   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

oscl_mem_auto_ptr.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00003 
00004 //           O S C L _ M E M _ A U T O _ P T R
00005 
00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00007 
00022 #ifndef OSCL_MEM_AUTO_PTR_H
00023 #define OSCL_MEM_AUTO_PTR_H
00024 
00025 #ifndef OSCLCONFIG_MEMORY_H_INCLUDED
00026 #include "osclconfig_memory.h"
00027 #endif
00028 
00029 #ifndef OSCL_MEM_H_INCLUDED
00030 #include "oscl_mem.h"
00031 #endif
00032 
00033 #define OSCL_DISABLE_WARNING_TRUNCATE_DEBUG_MESSAGE
00034 #define OSCL_DISABLE_WARNING_RETURN_TYPE_NOT_UDT
00035 #include "osclconfig_compiler_warnings.h"
00053 template < class T, class _Allocator = Oscl_TAlloc<T, OsclMemAllocator> > class OSCLMemAutoPtr
00054 {
00055     private:
00056         T* _Ptr;
00057 
00058 
00059     public:
00060         bool _Ownership;
00061 
00062 
00067         explicit OSCLMemAutoPtr(T* inPtr = 0) : _Ptr(inPtr), _Ownership(inPtr != 0) {};
00068 
00076         OSCLMemAutoPtr(const OSCLMemAutoPtr<T>& _Y): _Ownership(_Y._Ownership),
00077                 _Ptr(_Y.release()) {};
00078 
00079 
00093         OSCLMemAutoPtr<T, _Allocator>& operator=(const OSCLMemAutoPtr<T, _Allocator>& _Y)
00094         {
00095             if (this != &_Y)
00096             {
00097                 if (_Ptr != _Y.get())
00098                 {
00099                     if (_Ownership)
00100                     {
00101                         _Allocator alloc;
00102                         alloc.destroy(_Ptr);
00103                         alloc.deallocate(_Ptr, 1);
00104                     }
00105                     _Ownership = _Y._Ownership;
00106                 }
00107                 else if (_Y._Ownership)
00108                 {
00109                     _Ownership = true;
00110                 }
00111                 _Ptr = _Y.release();
00112             }
00113             return (*this);
00114         }
00115 
00121         ~OSCLMemAutoPtr()
00122         {
00123             if (_Ownership)
00124             {
00125                 _Allocator alloc;
00126                 alloc.destroy(_Ptr);
00127                 alloc.deallocate(_Ptr, 1);
00128             }
00129         }
00130 
00138         T& operator*() const
00139         {
00140             return (*get());
00141         }
00142 
00150         T *operator->() const
00151         {
00152             return (get());
00153         }
00154 
00155 
00161         void takeOwnership(T* ptr)
00162         {
00163             if (_Ptr != ptr)
00164             {
00165                 if (_Ownership)
00166                 {
00167                     _Allocator alloc;
00168                     alloc.destroy(_Ptr);
00169                     alloc.deallocate(_Ptr, 1);
00170                 }
00171                 _Ptr = ptr;
00172             }
00173 
00174             if (_Ptr)
00175             {
00176                 _Ownership = true;
00177             }
00178         }
00179 
00180         static void deallocate(T* ptr)
00181         {
00182             _Allocator alloc;
00183             alloc.destroy(ptr);
00184             alloc.deallocate(ptr, 1);
00185         }
00186 
00187         void allocate(oscl_memsize_t size)
00188         {
00189             _Allocator alloc;
00190             if (_Ptr && _Ownership)
00191             {
00192                 alloc.destroy(_Ptr);
00193                 alloc.deallocate(_Ptr, 1);
00194             }
00195             _Ptr = alloc.ALLOCATE(size);
00196             _Ownership = true;
00197         }
00198 
00204         void setWithoutOwnership(T* ptr)
00205         {
00206             if (_Ptr != ptr)
00207             {
00208                 if (_Ownership)
00209                 {
00210                     _Allocator alloc;
00211                     alloc.destroy(_Ptr);
00212                     alloc.deallocate(_Ptr, 1);
00213                 }
00214                 _Ptr = ptr;
00215             }
00216 
00217             _Ownership = false;
00218         }
00219 
00224         T *get() const
00225         {
00226             return (_Ptr);
00227         }
00228 
00234         T *release() const
00235         {
00236             ((OSCLMemAutoPtr<T> *)this)->_Ownership = false;
00237             return (_Ptr);
00238         }
00239 
00240 };
00241 
00242 #endif //OSCL_MEM_AUTO_PTR_H
00243 

OSCL API
Posting Version: OPENCORE_20090310