Lumiera  0.pre.03
»edit your freedom«
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/symbol.hpp"
49 #include "lib/nocopy.hpp"
50 
51 #include <algorithm>
52 #include <vector>
53 
54 
55 namespace lib {
56 
57 
65  class DelStash
67  {
68 
69  typedef void KillFun(void*);
70 
75  class Killer
76  {
77  void* target_;
78  KillFun* killIt_;
79 
80  public:
81  Killer(KillFun* f, void* t)
82  : target_{t}
83  , killIt_{f}
84  {
85  REQUIRE(f);
86  }
87 
88  void
89  trigger()
90  {
91  if (target_)
92  killIt_(target_);
93  target_ = NULL; // remember kill
94  }
95 
96  bool operator== (const void* target) const { return target_ == target; }
97  bool operator!= (const void* target) const { return target_ != target; }
98 
99  bool
100  isActive() const
101  {
102  return bool(target_);
103  }
104  };
105 
106 
107  typedef std::vector<Killer> Killers;
108 
109  Killers killers_;
110 
111 
112  public:
113  DelStash (size_t elms_to_reserve =0)
114  : killers_()
115  {
116  if (elms_to_reserve)
117  {
118  killers_.reserve (elms_to_reserve);
119  }
120  }
121 
122  ~DelStash ()
123  try { killAll(); }
124 
125  catch(...)
126  {
127  Symbol errID = lumiera_error();
128  WARN (memory, "Problems on de-allocation: %s", errID.c());
129  }
130 
131 
132  size_t
133  size () const
134  {
135  size_t activeEntries = 0;
136  size_t i = killers_.size();
137  while (i)
138  if (killers_[--i].isActive())
139  ++activeEntries;
140  return activeEntries;
141  }
142 
143 
144 
145  template<typename TY>
146  void
147  manage (TY* obj)//, __disable_if_voidPtr<TY> =0)
148  {
149  if (!obj) return;
150  REQUIRE (!isRegistered (obj));
151  killers_.push_back (Killer (how_to_kill<TY>, obj));
152  }
153 
154  template<typename TY>
155  void
156  manage (TY& obj)//, __disable_if_voidPtr<TY> =0)
157  {
158  REQUIRE (!isRegistered (&obj));
159  killers_.push_back (Killer (how_to_kill<TY>, &obj));
160  }
161 
162  void
163  manage (void* obj, KillFun* customDeleter)
164  {
165  if (!obj) return;
166  REQUIRE (!isRegistered (obj));
167  killers_.push_back (Killer (customDeleter, obj));
168  }
169 
170 
171  template<typename TY>
172  void
173  kill (TY* obj)
174  {
175  triggerKill (obj);
176  } // note: entry remains in the killer vector,
177  // but is disabled and can't be found anymore
178 
179  template<typename TY>
180  void
181  kill (TY& obj)
182  {
183  triggerKill (&obj);
184  }
185 
186  void
187  killAll ()
188  {
189  size_t i = killers_.size();
190  while (i)
191  killers_[--i].trigger();
192  }
193 
194 
195  private:
198  template<typename X>
199  static void
200  how_to_kill (void* subject)
201  {
202  X* victim = static_cast<X*> (subject);
203  ENSURE (victim);
204  delete victim;
205  };
206 
207  bool
208  isRegistered (const void* objAddress)
209  {
210  REQUIRE (objAddress);
211  return killers_.end() != findEntry (objAddress);
212  }
213 
214  Killers::iterator
215  findEntry (const void* obj)
216  {
217  REQUIRE (obj);
218  return std::find (killers_.begin(),killers_.end(), obj);
219  }
220 
221  void
222  triggerKill (void* objAddress)
223  {
224  Killers::iterator pos = findEntry (objAddress);
225  if (killers_.end() != pos)
226  pos->trigger();
227  ENSURE (!isRegistered (objAddress), "duplicate deleter registration");
228  }
229  };
230 
231 
232 
233 
234 } // namespace lib
235 #endif
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Implementation namespace for support and library code.
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Marker types to indicate a literal string and a Symbol.
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
Manage a collection of deleter functions.
Definition: del-stash.hpp:65
Lumiera error handling (C++ interface).
static void how_to_kill(void *subject)
trampoline function to invoke destructor of the specific target type
Definition: del-stash.hpp:200