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