1 // This file is part of The New Aspell 2 // Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license 3 // version 2.0 or 2.1. You should have received a copy of the LGPL 4 // license along with this library if you did not you can find 5 // it at http://www.gnu.org/. 6 7 #ifndef stack_ptr 8 #define stack_ptr 9 10 #include <assert.h> 11 12 namespace acommon { 13 14 template <typename T> 15 class StackPtr { 16 T * ptr; 17 18 // to avoid operator* being used unexpectedly. for example 19 // without this the following will compile 20 // PosibErr<T> fun(); 21 // PosibErr<StackPtr<T > > pe = fun(); 22 // and operator* and StackPtr(T *) will be used. The explicit 23 // doesn't protect us here due to PosibErr 24 StackPtr(const StackPtr & other); 25 // because I am paranoid 26 StackPtr & operator=(const StackPtr & other); 27 28 public: 29 StackPtr(T * p=0)30 explicit StackPtr(T * p = 0) : ptr(p) {} 31 StackPtr(StackPtr & other)32 StackPtr(StackPtr & other) : ptr (other.release()) {} 33 ~StackPtr()34 ~StackPtr() {del();} 35 operator =(StackPtr & other)36 StackPtr & operator=(StackPtr & other) 37 {reset(other.release()); return *this;} 38 operator *() const39 T & operator* () const {return *ptr;} operator ->() const40 T * operator-> () const {return ptr;} get() const41 T * get() const {return ptr;} operator T*() const42 operator T * () const {return ptr;} 43 release()44 T * release () {T * p = ptr; ptr = 0; return p;} 45 del()46 void del() {delete ptr; ptr = 0;} reset(T * p)47 void reset(T * p) {assert(ptr==0); ptr = p;} operator =(T * p)48 StackPtr & operator=(T * p) {reset(p); return *this;} 49 50 }; 51 } 52 53 #endif 54 55