1 // ========================================================================== 2 // $Source: /var/lib/cvs/Givaro/src/kernel/memory/givpointer.h,v $ 3 // Copyright(c)'1994-2009 by The Givaro group 4 // This file is part of Givaro. 5 // Givaro is governed by the CeCILL-B license under French law 6 // and abiding by the rules of distribution of free software. 7 // see the COPYRIGHT file for more details. 8 // Author: T. Gautier 9 // $Id: givpointer.h,v 1.3 2011-02-02 16:23:56 briceboyer Exp $ 10 // ========================================================================== 11 /*! @file givpointer.h 12 * @ingroup memory 13 * @brief auto ptr management 14 */ 15 #ifndef __GIVARO_pointer_H 16 #define __GIVARO_pointer_H 17 18 #include "givaro/givaromm.h" 19 20 namespace Givaro { 21 22 // ==================================================================== // 23 24 //! Refcount Pointer 25 template<class T> 26 class RefCountPtr { 27 T* _data; 28 mutable int* _count; 29 public: RefCountPtr(T * data)30 explicit RefCountPtr ( T* data ) 31 : _data( data ), _count(0) 32 { 33 _count = GivaroMM<int>::allocate(1); 34 *_count = 1; 35 } RefCountPtr(const RefCountPtr<T> & ptr)36 RefCountPtr ( const RefCountPtr<T>& ptr ) 37 : _data( ptr._data), _count(ptr._count) 38 { 39 if (_count !=0) *_count += 1; 40 } ~RefCountPtr()41 ~RefCountPtr() 42 { 43 if (--*_count ==0) { 44 delete data; 45 GivaroMM<int>::desallocate(_count); 46 } 47 } 48 49 RefCountPtr<T>& operator=( const RefCountPtr<T>& ptr ) 50 { 51 if (--*_count ==0) { 52 delete data; 53 GivaroMM<int>::desallocate(_count); 54 } 55 _data = ptr._data; _count = ptr._count; 56 if (_count !=0) *_count += 1; 57 } 58 59 T& operator* () const { return *_data; } 60 T* operator-> () const { return _data; } 61 }; 62 63 } // namespace Givaro 64 65 #endif // __GIVARO_pointer_H 66 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 67 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s 68