diff options
author | Michael Shepanski <michael@codesynthesis.com> | 2014-10-30 15:21:32 +1100 |
---|---|---|
committer | Michael Shepanski <michael@codesynthesis.com> | 2014-10-31 08:39:49 +1100 |
commit | 1f217e38f7507758da1d33d46e675e801621aa38 (patch) | |
tree | bad382ee315a820ec08bd2a83eaece12ac8169e3 /odb/details/function-wrapper.txx | |
parent | 31af5a488f2be9f4059e159492d4fe62f25a895a (diff) |
Allow lambdas & std::functions as query factories with C++-98 builds of libodb
Diffstat (limited to 'odb/details/function-wrapper.txx')
-rw-r--r-- | odb/details/function-wrapper.txx | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/odb/details/function-wrapper.txx b/odb/details/function-wrapper.txx new file mode 100644 index 0000000..70dcb0f --- /dev/null +++ b/odb/details/function-wrapper.txx @@ -0,0 +1,87 @@ +// file : odb/details/function-wrapper.txx +// copyright : Copyright (c) 2009-2014 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <utility> // std::swap, std::move + +namespace odb +{ + namespace details + { +#ifdef ODB_CXX11 + template <typename F> + struct caller_impl; + +#ifdef ODB_CXX11_VARIADIC_TEMPLATE + template <typename R, typename... A> + struct caller_impl<R (A...)> + { + static R + function (const void* f, A... a) + { + return (*static_cast<const std::function<R (A...)>*> (f)) (a...); + } + }; +#else + template <typename R, typename A1> + struct caller_impl<R (A1)> + { + static R + function (const void* f, A1 a1) + { + return (*static_cast<const std::function<R (A1)>*> (f)) (a1); + } + }; + + template <typename R, typename A1, typename A2> + struct caller_impl<R (A1, A2)> + { + static R + function (const void* f, A1 a1, A2 a2) + { + return (*static_cast<const std::function<R (A1, A2)>*> (f)) (a1, a2); + } + }; +#endif + + template <typename F> + void + deleter_impl (const void* f) + { + delete static_cast<const std::function<F>*> (f); + } + + template <typename F> + template <typename F1> + function_wrapper<F>:: + function_wrapper ( + F1 f, + typename std::enable_if<!std::is_convertible<F1, F*>::value>::type*) + { + std_function_type sf (std::move (f)); + + if (F* const* const f = sf.template target<F*> ()) + { + function = *f; + deleter = 0; + std_function = 0; + } + else + { + function = reinterpret_cast<F*> (&caller_impl<F>::function); + deleter = &deleter_impl<F>; + std_function = new std_function_type (std::move (sf)); + } + } +#endif + + template <typename F> + void function_wrapper<F>:: + swap (function_wrapper<F>& x) + { + std::swap (function, x.function); + std::swap (deleter, x.deleter); + std::swap (std_function, x.std_function); + } + } +} |