From 1f217e38f7507758da1d33d46e675e801621aa38 Mon Sep 17 00:00:00 2001 From: Michael Shepanski Date: Thu, 30 Oct 2014 15:21:32 +1100 Subject: Allow lambdas & std::functions as query factories with C++-98 builds of libodb --- odb/details/function-wrapper.hxx | 83 ++++++++++++++++++++++++++++++++++++++ odb/details/function-wrapper.ixx | 47 ++++++++++++++++++++++ odb/details/function-wrapper.txx | 87 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 odb/details/function-wrapper.hxx create mode 100644 odb/details/function-wrapper.ixx create mode 100644 odb/details/function-wrapper.txx (limited to 'odb/details') diff --git a/odb/details/function-wrapper.hxx b/odb/details/function-wrapper.hxx new file mode 100644 index 0000000..5615fca --- /dev/null +++ b/odb/details/function-wrapper.hxx @@ -0,0 +1,83 @@ +// file : odb/details/function-wrapper.hxx +// copyright : Copyright (c) 2009-2014 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_FUNCTION_WRAPPER_HXX +#define ODB_DETAILS_FUNCTION_WRAPPER_HXX + +#include + +#include // ODB_CXX11 + +#ifdef ODB_CXX11 +# include // std::function +# include // std::enable_if, std::is_convertible +#endif + +namespace odb +{ + namespace details + { + // Low-level 'callable object' wrapper similar to std::function but + // that works in both C++98 and 11. In particular, the call site can + // be compiled in C++98 and the registration site in C++11 and it + // will work. + // + template + struct function_wrapper + { + ~function_wrapper (); + + explicit + function_wrapper (F* = 0); + +#ifdef ODB_CXX11 + typedef typename std::function std_function_type; + + // This overload accepts lambdas and std::functions, but when the + // argument is convertible to F*, then we disable it in favor of the + // other overload (above), which is more efficient. + // + // Subtlety alert: if you're thinking of changing this to accept a + // std::function argument, stop. That creates an overload ambiguity + // when the actual parameter is a lambda, which is convertible to either + // std::function or F*. + // + template + function_wrapper(F1, + typename std::enable_if< + !std::is_convertible::value>::type* = 0); +#endif + + // Destructive copy construction and assignment (aka move). These + // should really only be called by containers when they need to + // reallocate the underlying buffer and move the elements. + // + function_wrapper (const function_wrapper&); + function_wrapper& + operator= (const function_wrapper&); + + void swap (function_wrapper&); + + // Conversion to bool. + // + public: + typedef void (function_wrapper::*bool_convertible) (); + void true_value () {} + + operator bool_convertible () const; + + public: + F* function; + void (*deleter) (const void*); + const void* std_function; + }; + } +} + +#include +#include + +#include + +#endif // ODB_DETAILS_FUNCTION_WRAPPER_HXX diff --git a/odb/details/function-wrapper.ixx b/odb/details/function-wrapper.ixx new file mode 100644 index 0000000..58ede04 --- /dev/null +++ b/odb/details/function-wrapper.ixx @@ -0,0 +1,47 @@ +// file : odb/details/function-wrapper.ixx +// copyright : Copyright (c) 2009-2014 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace details + { + template + inline function_wrapper:: + ~function_wrapper () + { + if (deleter != 0) + deleter (std_function); + } + + template + inline function_wrapper:: + function_wrapper (F* f) + : function (f), deleter (0), std_function (0) + { + } + + template + inline function_wrapper:: + function_wrapper (const function_wrapper& x) + : function (0), deleter (0), std_function (0) + { + swap (const_cast&> (x)); + } + + template + inline function_wrapper& function_wrapper:: + operator= (const function_wrapper& x) + { + swap (const_cast&> (x)); + return *this; + } + + template + inline function_wrapper:: + operator bool_convertible () const + { + return function != 0 ? &function_wrapper::true_value : 0; + } + } +} 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 // std::swap, std::move + +namespace odb +{ + namespace details + { +#ifdef ODB_CXX11 + template + struct caller_impl; + +#ifdef ODB_CXX11_VARIADIC_TEMPLATE + template + struct caller_impl + { + static R + function (const void* f, A... a) + { + return (*static_cast*> (f)) (a...); + } + }; +#else + template + struct caller_impl + { + static R + function (const void* f, A1 a1) + { + return (*static_cast*> (f)) (a1); + } + }; + + template + struct caller_impl + { + static R + function (const void* f, A1 a1, A2 a2) + { + return (*static_cast*> (f)) (a1, a2); + } + }; +#endif + + template + void + deleter_impl (const void* f) + { + delete static_cast*> (f); + } + + template + template + function_wrapper:: + function_wrapper ( + F1 f, + typename std::enable_if::value>::type*) + { + std_function_type sf (std::move (f)); + + if (F* const* const f = sf.template target ()) + { + function = *f; + deleter = 0; + std_function = 0; + } + else + { + function = reinterpret_cast (&caller_impl::function); + deleter = &deleter_impl; + std_function = new std_function_type (std::move (sf)); + } + } +#endif + + template + void function_wrapper:: + swap (function_wrapper& x) + { + std::swap (function, x.function); + std::swap (deleter, x.deleter); + std::swap (std_function, x.std_function); + } + } +} -- cgit v1.1