Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
del-stash.hpp
Go to the documentation of this file.
1 /*
2  DEL-STASH.hpp - collect and execute deleter functions
3 
4  Copyright (C) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program 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 this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
43 #ifndef LIB_DEL_STASH_H
44 #define LIB_DEL_STASH_H
45 
46 
47 #include "lib/error.hpp"
48 #include "lib/nocopy.hpp"
49 
50 #include <algorithm>
51 #include <vector>
52 
53 
54 namespace lib {
55 
56 
64  class DelStash
66  {
67 
68  typedef void KillFun(void*);
69 
74  class Killer
75  {
76  void* target_;
78 
79  public:
80  Killer(KillFun* f, void* t)
81  : target_{t}
82  , killIt_{f}
83  {
84  REQUIRE(f);
85  }
86 
87  void
89  {
90  if (target_)
91  killIt_(target_);
92  target_ = NULL; // remember kill
93  }
94 
95  bool operator== (const void* target) const { return target_ == target; }
96  bool operator!= (const void* target) const { return target_ != target; }
97 
98  bool
99  isActive() const
100  {
101  return bool(target_);
102  }
103  };
104 
105 
106  typedef std::vector<Killer> Killers;
107 
108  Killers killers_;
109 
110 
111  public:
112  DelStash (size_t elms_to_reserve =0)
113  : killers_()
114  {
115  if (elms_to_reserve)
116  {
117  killers_.reserve (elms_to_reserve);
118  }
119  }
120 
122  try { killAll(); }
123 
124  catch(...)
125  {
126  Symbol errID = lumiera_error();
127  WARN (memory, "Problems on de-allocation: %s", errID.c());
128  }
129 
130 
131  size_t
132  size () const
133  {
134  size_t activeEntries = 0;
135  size_t i = killers_.size();
136  while (i)
137  if (killers_[--i].isActive())
138  ++activeEntries;
139  return activeEntries;
140  }
141 
142 
143 
144  template<typename TY>
145  void
146  manage (TY* obj)//, __disable_if_voidPtr<TY> =0)
147  {
148  if (!obj) return;
149  REQUIRE (!isRegistered (obj));
150  killers_.push_back (Killer (how_to_kill<TY>, obj));
151  }
152 
153  template<typename TY>
154  void
155  manage (TY& obj)//, __disable_if_voidPtr<TY> =0)
156  {
157  REQUIRE (!isRegistered (&obj));
158  killers_.push_back (Killer (how_to_kill<TY>, &obj));
159  }
160 
161  void
162  manage (void* obj, KillFun* customDeleter)
163  {
164  if (!obj) return;
165  REQUIRE (!isRegistered (obj));
166  killers_.push_back (Killer (customDeleter, obj));
167  }
168 
169 
170  template<typename TY>
171  void
172  kill (TY* obj)
173  {
174  triggerKill (obj);
175  } // note: entry remains in the killer vector,
176  // but is disabled and can't be found anymore
177 
178  template<typename TY>
179  void
180  kill (TY& obj)
181  {
182  triggerKill (&obj);
183  }
184 
185  void
187  {
188  size_t i = killers_.size();
189  while (i)
190  killers_[--i].trigger();
191  }
192 
193 
194  private:
197  template<typename X>
198  static void
199  how_to_kill (void* subject)
200  {
201  X* victim = static_cast<X*> (subject);
202  ENSURE (victim);
203  delete victim;
204  };
205 
206  bool
207  isRegistered (const void* objAddress)
208  {
209  REQUIRE (objAddress);
210  return killers_.end() != findEntry (objAddress);
211  }
212 
214  findEntry (const void* obj)
215  {
216  REQUIRE (obj);
217  return std::find (killers_.begin(),killers_.end(), obj);
218  }
219 
220  void
221  triggerKill (void* objAddress)
222  {
223  Killers::iterator pos = findEntry (objAddress);
224  if (killers_.end() != pos)
225  pos->trigger();
226  ENSURE (!isRegistered (objAddress), "duplicate deleter registration");
227  }
228  };
229 
230 
231 
232 
233 } // namespace lib
234 #endif
bool isRegistered(const void *objAddress)
Definition: del-stash.hpp:207
void manage(TY *obj)
Definition: del-stash.hpp:146
size_t size() const
Definition: del-stash.hpp:132
void manage(TY &obj)
Definition: del-stash.hpp:155
void manage(void *obj, KillFun *customDeleter)
Definition: del-stash.hpp:162
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
bool operator==(const void *target) const
Definition: del-stash.hpp:95
typename meta::Strip< CON >::TypeReferred::iterator iterator
Implementation namespace for support and library code.
bool find(Query< Pipe > &q)
shortcut: run just a query without creating new instances
DelStash(size_t elms_to_reserve=0)
Definition: del-stash.hpp:112
Token or Atom with distinct identity.
Definition: symbol.hpp:116
Mix-Ins to allow or prohibit various degrees of copying and cloning.
return NULL
Definition: llist.h:596
bool operator!=(const void *target) const
Definition: del-stash.hpp:96
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
bool isActive() const
Definition: del-stash.hpp:99
std::vector< Killer > Killers
Definition: del-stash.hpp:106
Manage a collection of deleter functions.
Definition: del-stash.hpp:64
Lumiera error handling (C++ interface).
void KillFun(void *)
Definition: del-stash.hpp:68
void kill(TY *obj)
Definition: del-stash.hpp:172
Killers::iterator findEntry(const void *obj)
Definition: del-stash.hpp:214
Killers killers_
Definition: del-stash.hpp:108
void kill(TY &obj)
Definition: del-stash.hpp:180
Killer(KillFun *f, void *t)
Definition: del-stash.hpp:80
const char * c() const
Definition: symbol.hpp:92
void triggerKill(void *objAddress)
Definition: del-stash.hpp:221
void killAll()
Definition: del-stash.hpp:186
ENSURE(r==&pq)
static void how_to_kill(void *subject)
trampoline function to invoke destructor of the specific target type
Definition: del-stash.hpp:199