Lumiera  0.pre.03
»edityourfreedom«
function-erasure.hpp
Go to the documentation of this file.
1 /*
2  FUNCTION-ERASURE.hpp - wrapping a functor object for inline storage while hiding the signature
3 
4  Copyright (C) Lumiera.org
5  2009, 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 
55 #ifndef LIB_META_FUNCTION_ERASURE_H
56 #define LIB_META_FUNCTION_ERASURE_H
57 
58 #include "lib/util.hpp"
59 #include "lib/error.hpp"
60 #include "lib/opaque-holder.hpp"
61 #include "lib/functor-util.hpp"
62 
63 #include <functional>
64 
65 
66 namespace lib {
67 namespace meta{
68 
69  using std::function;
70  using util::unConst;
71 
72 
73 
74  /**************************************************/
89  template<class FH>
90  struct FunErasure
91  : FH
92  {
93  template<typename FUN>
94  FunErasure (FUN const& functor)
95  : FH(functor)
96  { }
97 
98  friend bool
99  operator!= (FunErasure const& fer1, FunErasure const& fer2)
100  {
101  return not (fer1==fer2); // use equality defined by FH
102  }
103  };
104 
105 
106  /* ====== Policy classes ====== */
107 
108  typedef function<void(void)> FunVoid;
109  typedef lib::InPlaceAnyHolder< sizeof(FunVoid) // same size for all function objects
110  , lib::InPlaceAnyHolder_unrelatedTypes // no common base class!
112  typedef lib::InPlaceAnyHolder< sizeof(void*)
115 
116 
124  : public FunHolder
125  {
126  public:
127  template<typename SIG>
128  StoreFunction (SIG& fun)
129  : FunHolder(function<SIG>(fun))
130  { }
131 
132  template<typename SIG>
133  StoreFunction (function<SIG> const& fun)
134  : FunHolder(fun)
135  { }
136 
137  template<typename SIG>
138  function<SIG>&
139  getFun () const
140  {
141  return get<function<SIG>>();
142  }
143 
144 
145  friend bool
147  StoreFunction const& o2)
148  {
149  return util::rawComparison (o1.asBase(),o2.asBase());
150  }
151  };
152 
153 
161  : public FunPtrHolder
162  {
163  public:
164  template<typename SIG>
165  StoreFunPtr (SIG& fun)
166  : FunPtrHolder(&fun)
167  { }
168 
169  template<typename SIG>
170  StoreFunPtr (SIG *fun)
171  : FunPtrHolder(fun)
172  { }
173 
174  template<typename SIG>
175  SIG&
176  getFun () const
177  {
178  SIG *fun = get<SIG*>();
179  REQUIRE (fun);
180  return *fun;
181  }
182 
183 
184  friend bool
186  StoreFunPtr const& o2)
187  {
188  void * *fun1 = reinterpret_cast<void**> (o1.asBase());
189  void * *fun2 = reinterpret_cast<void**> (o2.asBase());
190  return *fun1 == *fun2;
191  }
192  };
193 
194 
201  {
202  void *funP_;
203 
204  public:
205  template<typename SIG>
207  {
208  funP_ = reinterpret_cast<void*> (&fun);
209  }
210  template<typename SIG>
212  {
213  funP_ = reinterpret_cast<void*> (fun);
214  }
215 
216  template<typename SIG>
217  SIG&
219  {
220  return *reinterpret_cast<SIG*> (funP_);
221  }
222 
223 
224  explicit operator bool() const { return funP_; }
225  bool isValid() const { return funP_; }
226 
227  friend bool
229  StoreUncheckedFunPtr const& o2)
230  {
231  return unConst(o1).funP_ == unConst(o2).funP_;
232  }
233  };
234 
235 
236 
237 
238 }} // namespace lib::meta
239 #endif
Policy for FunErasure: store an unchecked bare function pointer.
StoreFunction(function< SIG > const &fun)
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
Implementation namespace for support and library code.
Alternative policy for accessing the contents without a common interface; use this policy if the inte...
Inline buffer to hold and own an object while concealing the concrete type.
Generic wrapper carrying a function object while hiding the actual function signature.
Policy for FunErasure: store a bare function pointer.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Helper allowing type erasure while holding the actual object inline.
bool rawComparison(function< SIG > const &f1, function< SIG > const &f2)
temporary workaround: tr1/functional should define public comparison operators for functor objects...
Policy for FunErasure: store an embedded std::function Using this policy allows to store arbitrary co...
FunErasure(FUN const &functor)
function< void(void)> FunVoid
Collection of small helpers and utilities related to function objects.
friend bool operator!=(FunErasure const &fer1, FunErasure const &fer2)
Lumiera error handling (C++ interface).
OBJ * unConst(const OBJ *o)
shortcut to save some typing when having to define const and non-const variants of member functions ...
Definition: util.hpp:319
BaseP asBase() const
<
function< SIG > & getFun() const