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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
46 #ifndef LIB_META_FUNCTION_ERASURE_H
47 #define LIB_META_FUNCTION_ERASURE_H
48 
49 #include "lib/util.hpp"
50 #include "lib/error.hpp"
51 #include "lib/opaque-holder.hpp"
52 
53 #include <functional>
54 
55 
56 namespace lib {
57 namespace meta{
58 
59  using std::function;
60  using util::unConst;
61 
62 
63 
64  /**************************************************/
79  template<class FH>
80  struct FunErasure
81  : FH
82  {
83  template<typename FUN>
84  FunErasure (FUN const& functor)
85  : FH(functor)
86  { }
87 
88  friend bool
89  operator!= (FunErasure const& fer1, FunErasure const& fer2)
90  {
91  return not (fer1==fer2); // use equality defined by FH
92  }
93  };
94 
95 
96  /* ====== Policy classes ====== */
97 
98  typedef function<void(void)> FunVoid;
99  typedef lib::InPlaceAnyHolder< sizeof(FunVoid) // same size for all function objects
100  , lib::InPlaceAnyHolder_unrelatedTypes // no common base class!
101  > FunHolder;
102  typedef lib::InPlaceAnyHolder< sizeof(void*)
104  > FunPtrHolder;
105 
106 
114  : public FunHolder
115  {
116  public:
117  template<typename SIG>
118  StoreFunction (SIG& fun)
119  : FunHolder(function<SIG>(fun))
120  { }
121 
122  template<typename SIG>
123  StoreFunction (function<SIG> const& fun)
124  : FunHolder(fun)
125  { }
126 
127  template<typename SIG>
128  function<SIG>&
129  getFun () const
130  {
131  return get<function<SIG>>();
132  }
133  };
134 
135 
143  : public FunPtrHolder
144  {
145  public:
146  template<typename SIG>
147  StoreFunPtr (SIG& fun)
148  : FunPtrHolder(&fun)
149  { }
150 
151  template<typename SIG>
152  StoreFunPtr (SIG *fun)
153  : FunPtrHolder(fun)
154  { }
155 
156  template<typename SIG>
157  SIG&
158  getFun () const
159  {
160  SIG *fun = get<SIG*>();
161  REQUIRE (fun);
162  return *fun;
163  }
164  };
165 
166 
173  {
174  void *funP_;
175 
176  public:
177  template<typename SIG>
178  StoreUncheckedFunPtr (SIG& fun)
179  {
180  funP_ = reinterpret_cast<void*> (&fun);
181  }
182  template<typename SIG>
183  StoreUncheckedFunPtr (SIG *fun)
184  {
185  funP_ = reinterpret_cast<void*> (fun);
186  }
187 
188  template<typename SIG>
189  SIG&
190  getFun ()
191  {
192  return *reinterpret_cast<SIG*> (funP_);
193  }
194 
195 
196  explicit operator bool() const { return funP_; }
197  bool isValid() const { return funP_; }
198 
199  };
200 
201 
202 
203 
204 }} // namespace lib::meta
205 #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).