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
Cleaner.h
1 /*
2  * $Id$
3  * cleaner module interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2008-10, 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_CLEANER_H
23 #define ELM_UTIL_CLEANER_H
24 
25 #include <elm/util/AutoPtr.h>
26 #include <elm/genstruct/SLList.h>
27 
28 namespace elm {
29 
30 // Cleaner class
31 class Cleaner {
32 public:
33  virtual void clean(void) { }
34  virtual ~Cleaner(void) { }
35 };
36 
37 
38 // Deletor class
39 template <class T>
40 class Deletor: public Cleaner {
41 public:
42  inline Deletor(T *object): obj(object) { }
43  virtual ~Deletor(void) { }
44  virtual void clean(void) { delete obj; }
45 private:
46  T *obj;
47 };
48 
49 
50 // AutoCleaner class
51 template <class T>
52 class AutoCleaner: public AutoPtr<T>, public Cleaner {
53 public:
54  inline AutoCleaner(T *p = 0): AutoPtr<T>(p) { }
55  inline AutoCleaner(const AutoPtr<T>& locked): AutoPtr<T>(locked) { }
56 };
57 
58 
59 // CleanList class
60 class CleanList {
61 public:
62  inline ~CleanList(void) { clean(); }
63  void add(Cleaner *cleaner);
64  void clean(void);
65 
66  inline Cleaner *operator()(Cleaner *cleaner) { add(cleaner); return cleaner; }
67  template <class T> inline const AutoPtr<T>& operator()(const AutoPtr<T>& object)
68  { add(new AutoCleaner<T>(object)); return object; }
69  template <class T> inline T *operator()(T *object)
70  { add(new Deletor<T>(object)); return object; }
71 
72 private:
73  typedef genstruct::SLList<Cleaner *> list_t;
74  list_t list;
75 };
76 
77 } // elm
78 
79 #endif /* ELM_UTIL_CLEANER_H */
void clean(void)
Definition: util_Cleaner.cpp:109
AutoCleaner(const AutoPtr< T > &locked)
Definition: Cleaner.h:55
Cleaner * operator()(Cleaner *cleaner)
Definition: Cleaner.h:66
const AutoPtr< T > & operator()(const AutoPtr< T > &object)
Definition: Cleaner.h:67
Definition: Cleaner.h:31
virtual void clean(void)
Definition: Cleaner.h:33
AutoCleaner(T *p=0)
Definition: Cleaner.h:54
virtual ~Deletor(void)
Definition: Cleaner.h:43
Definition: Cleaner.h:52
virtual ~Cleaner(void)
Definition: Cleaner.h:34
Definition: AutoPtr.h:26
Definition: Cleaner.h:60
virtual void clean(void)
Definition: Cleaner.h:44
T * operator()(T *object)
Definition: Cleaner.h:69
~CleanList(void)
Definition: Cleaner.h:62
Deletor(T *object)
Definition: Cleaner.h:42
void add(Cleaner *cleaner)
Definition: util_Cleaner.cpp:100
Definition: Cleaner.h:40