// file : odb/details/function-wrapper.hxx // copyright : Copyright (c) 2009-2019 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&); // Cleanly cast to an incompatible function type. // template R cast () const; // Conversion to bool. // public: typedef void (function_wrapper::*bool_convertible) (); void true_value () {} operator bool_convertible () const { return function != 0 ? &function_wrapper::true_value : 0; } public: F* function; void (*deleter) (const void*); const void* std_function; }; } } #include #include #include #endif // ODB_DETAILS_FUNCTION_WRAPPER_HXX