Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
UniquePtr.h
1 /*
2  * UniquePtr class
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2007, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_UNIQUEPTR_H
22 #define ELM_UTIL_UNIQUEPTR_H
23 
24 namespace elm {
25 
26 template <class T>
27 class UniquePtr {
28 public:
29  inline UniquePtr(void): p(0) { }
30  inline UniquePtr(T *ptr): p(ptr) { };
31  inline UniquePtr(UniquePtr<T>& ptr): p(ptr.p) { ptr.p = 0; }
32  inline ~UniquePtr(void) { clean(); }
33 
34  inline bool isNull(void) const { return !p; }
35  inline void clean(void) { if(p) delete p; p = 0; }
36  inline T *detach(void) { T *res = p; p = 0; return res; }
37  inline void set(T *ptr) { clean(); p = ptr; }
38  inline T *get(void) const { return p; }
39 
40  inline UniquePtr<T>& operator=(T *ptr) { set(ptr); return *this; }
41  inline UniquePtr<T>& operator=(UniquePtr<T>& ptr) { clean(); p = ptr.p; ptr.p = 0; return *this; }
42  inline operator T *(void) const { return get(); }
43  inline T *operator->(void) const { return get(); }
44  inline operator bool(void) const { return !isNull(); }
45  inline T& operator*(void) const { return *get(); }
46 
47 private:
48  T *p;
49 };
50 
51 template <class T> UniquePtr<T> unique(T *p) { return UniquePtr<T>(p); }
52 
53 };
54 
55 #endif // ELM_UTIL_UNIQUEPTR_H
56 
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::unique
UniquePtr< T > unique(T *p)
Definition: UniquePtr.h:51
elm::UniquePtr
Definition: UniquePtr.h:27
elm::UniquePtr::operator=
UniquePtr< T > & operator=(UniquePtr< T > &ptr)
Definition: UniquePtr.h:41
elm::UniquePtr::detach
T * detach(void)
Definition: UniquePtr.h:36
elm::UniquePtr::~UniquePtr
~UniquePtr(void)
Definition: UniquePtr.h:32
void
bool
elm::UniquePtr::operator->
T * operator->(void) const
Definition: UniquePtr.h:43
elm::UniquePtr::get
T * get(void) const
Definition: UniquePtr.h:38
elm::UniquePtr::clean
void clean(void)
Definition: UniquePtr.h:35
elm::UniquePtr::UniquePtr
UniquePtr(void)
Definition: UniquePtr.h:29
elm::UniquePtr::operator*
T & operator*(void) const
Definition: UniquePtr.h:45
elm
Definition: adapter.h:26
elm::UniquePtr::UniquePtr
UniquePtr(UniquePtr< T > &ptr)
Definition: UniquePtr.h:31
elm::UniquePtr::UniquePtr
UniquePtr(T *ptr)
Definition: UniquePtr.h:30
elm::UniquePtr::set
void set(T *ptr)
Definition: UniquePtr.h:37
elm::UniquePtr::isNull
bool isNull(void) const
Definition: UniquePtr.h:34
elm::UniquePtr::operator=
UniquePtr< T > & operator=(T *ptr)
Definition: UniquePtr.h:40