Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
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/LockPtr.h>
26 #include <elm/data/List.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 LockPtr<T>, public Cleaner {
53 public:
54  inline AutoCleaner(T *p = 0): LockPtr<T>(p) { }
55  inline AutoCleaner(const LockPtr<T>& locked): LockPtr<T>(locked) { }
56 };
57 
58 
59 // CleanList class
60 class CleanList {
61 public:
62  inline ~CleanList(void) { clean(); }
63  void add(Cleaner *cleaner);
64  template <class T> void add(T *p) { if(p != nullptr) add(static_cast<Cleaner *>(new Deletor<T>(p))); }
65  void clean(void);
66 
67  inline Cleaner *operator()(Cleaner *cleaner) { add(cleaner); return cleaner; }
68  template <class T> inline const LockPtr<T>& operator()(const LockPtr<T>& object)
69  { add(new AutoCleaner<T>(object)); return object; }
70  template <class T> inline T *operator()(T *object)
71  { add(object); return object; }
72 
73 private:
74  typedef List<Cleaner *> list_t;
75  list_t list;
76 };
77 
78 } // elm
79 
80 #endif /* ELM_UTIL_CLEANER_H */
elm::AutoCleaner::AutoCleaner
AutoCleaner(T *p=0)
Definition: Cleaner.h:54
elm::Deletor::clean
virtual void clean(void)
Definition: Cleaner.h:44
elm::CleanList::operator()
T * operator()(T *object)
Definition: Cleaner.h:70
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::CleanList
Definition: Cleaner.h:60
elm::CleanList::operator()
Cleaner * operator()(Cleaner *cleaner)
Definition: Cleaner.h:67
elm::Deletor::~Deletor
virtual ~Deletor(void)
Definition: Cleaner.h:43
elm::CleanList::clean
void clean(void)
Definition: util_Cleaner.cpp:109
elm::Cleaner::clean
virtual void clean(void)
Definition: Cleaner.h:33
elm::Deletor
Definition: Cleaner.h:40
elm::Deletor::Deletor
Deletor(T *object)
Definition: Cleaner.h:42
elm
Definition: adapter.h:26
elm::CleanList::add
void add(T *p)
Definition: Cleaner.h:64
elm::Cleaner::~Cleaner
virtual ~Cleaner(void)
Definition: Cleaner.h:34
elm::Cleaner
Definition: Cleaner.h:31
elm::LockPtr
Definition: LockPtr.h:40
elm::AutoCleaner
Definition: Cleaner.h:52
elm::AutoCleaner::AutoCleaner
AutoCleaner(const LockPtr< T > &locked)
Definition: Cleaner.h:55
elm::CleanList::add
void add(Cleaner *cleaner)
Definition: util_Cleaner.cpp:100
elm::CleanList::operator()
const LockPtr< T > & operator()(const LockPtr< T > &object)
Definition: Cleaner.h:68
elm::CleanList::~CleanList
~CleanList(void)
Definition: Cleaner.h:62
elm::List< Cleaner * >