Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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
56namespace lib {
57namespace meta{
58
59 using std::function;
60 using util::unConst;
61
62
63
64 /**************************************************/
79 template<class FH>
81 : FH
82 {
83 template<typename FUN>
85 : FH(functor)
86 { }
87
88 friend bool
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!
102 typedef lib::InPlaceAnyHolder< sizeof(void*)
105
106
114 : public FunHolder
115 {
116 public:
117 template<typename SIG>
119 : FunHolder(function<SIG>(fun))
120 { }
121
122 template<typename SIG>
124 : FunHolder(fun)
125 { }
126
127 template<typename SIG>
129 getFun () const
130 {
131 return get<function<SIG>>();
132 }
133 };
134
135
143 : public FunPtrHolder
144 {
145 public:
146 template<typename SIG>
148 : FunPtrHolder(&fun)
149 { }
150
151 template<typename SIG>
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>
179 {
180 funP_ = reinterpret_cast<void*> (&fun);
181 }
182 template<typename SIG>
184 {
185 funP_ = reinterpret_cast<void*> (fun);
186 }
187
188 template<typename SIG>
189 SIG&
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
Inline buffer to hold and own an object while concealing the concrete type.
Policy for FunErasure: store a bare function pointer.
Policy for FunErasure: store an embedded std::function Using this policy allows to store arbitrary co...
StoreFunction(function< SIG > const &fun)
function< SIG > & getFun() const
Policy for FunErasure: store an unchecked bare function pointer.
Lumiera error handling (C++ interface).
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
function< void(void)> FunVoid
Implementation namespace for support and library code.
OBJ * unConst(const OBJ *)
shortcut to save some typing when having to define const and non-const variants of member functions
Definition util.hpp:358
Helper allowing type erasure while holding the actual object inline.
Alternative policy for accessing the contents without a common interface; use this policy if the inte...
Generic wrapper carrying a function object while hiding the actual function signature.
friend bool operator!=(FunErasure const &fer1, FunErasure const &fer2)
FunErasure(FUN const &functor)
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...