Lumiera  0.pre.03
»edit your freedom«
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 
62 #include <functional>
63 
64 
65 namespace lib {
66 namespace meta{
67 
68  using std::function;
69  using util::unConst;
70 
71 
72 
73  /**************************************************/
88  template<class FH>
89  struct FunErasure
90  : FH
91  {
92  template<typename FUN>
93  FunErasure (FUN const& functor)
94  : FH(functor)
95  { }
96 
97  friend bool
98  operator!= (FunErasure const& fer1, FunErasure const& fer2)
99  {
100  return not (fer1==fer2); // use equality defined by FH
101  }
102  };
103 
104 
105  /* ====== Policy classes ====== */
106 
107  typedef function<void(void)> FunVoid;
108  typedef lib::InPlaceAnyHolder< sizeof(FunVoid) // same size for all function objects
109  , lib::InPlaceAnyHolder_unrelatedTypes // no common base class!
110  > FunHolder;
111  typedef lib::InPlaceAnyHolder< sizeof(void*)
113  > FunPtrHolder;
114 
115 
123  : public FunHolder
124  {
125  public:
126  template<typename SIG>
127  StoreFunction (SIG& fun)
128  : FunHolder(function<SIG>(fun))
129  { }
130 
131  template<typename SIG>
132  StoreFunction (function<SIG> const& fun)
133  : FunHolder(fun)
134  { }
135 
136  template<typename SIG>
137  function<SIG>&
138  getFun () const
139  {
140  return get<function<SIG>>();
141  }
142  };
143 
144 
152  : public FunPtrHolder
153  {
154  public:
155  template<typename SIG>
156  StoreFunPtr (SIG& fun)
157  : FunPtrHolder(&fun)
158  { }
159 
160  template<typename SIG>
161  StoreFunPtr (SIG *fun)
162  : FunPtrHolder(fun)
163  { }
164 
165  template<typename SIG>
166  SIG&
167  getFun () const
168  {
169  SIG *fun = get<SIG*>();
170  REQUIRE (fun);
171  return *fun;
172  }
173  };
174 
175 
182  {
183  void *funP_;
184 
185  public:
186  template<typename SIG>
187  StoreUncheckedFunPtr (SIG& fun)
188  {
189  funP_ = reinterpret_cast<void*> (&fun);
190  }
191  template<typename SIG>
192  StoreUncheckedFunPtr (SIG *fun)
193  {
194  funP_ = reinterpret_cast<void*> (fun);
195  }
196 
197  template<typename SIG>
198  SIG&
199  getFun ()
200  {
201  return *reinterpret_cast<SIG*> (funP_);
202  }
203 
204 
205  explicit operator bool() const { return funP_; }
206  bool isValid() const { return funP_; }
207 
208  };
209 
210 
211 
212 
213 }} // namespace lib::meta
214 #endif
Policy for FunErasure: store an unchecked bare function pointer.
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.
Policy for FunErasure: store an embedded std::function Using this policy allows to store arbitrary co...
Lumiera error handling (C++ interface).