OSVR-Core
Classes | Namespaces | Macros | Functions | Variables
GenericCaller.h File Reference

Header providing templated functions to automate the process of calling a C++ member function (specifically the function call operator) from a facility that accepts a C function pointer and an opaque "userdata" pointer. More...

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/if.hpp>
#include <boost/preprocessor/enum.hpp>
#include <boost/preprocessor/enum_params.hpp>
#include <boost/preprocessor/repeat.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/function_types/function_arity.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <boost/type_traits/is_void.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/is_function.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/static_assert.hpp>

Go to the source code of this file.

Classes

struct  osvr::util::functor_trampolines::this_last_t
 Tag type indicating the last parameter of the function contains the "this" pointer. More...
 
struct  osvr::util::functor_trampolines::this_first_t
 Tag type indicating the first parameter of the function contains the "this" pointer. More...
 
struct  osvr::util::functor_trampolines::detail::param_at< F, C >
 Convenience metafunction to simplify computing the type of a particular argument to a function F. More...
 
struct  osvr::util::functor_trampolines::detail::Caller< ThisLocation, Arity, Return >
 Template that will be specialized to contain callers for functors with the "this" pointer as a void * argument. More...
 
struct  osvr::util::functor_trampolines::detail::ComputeGenericCaller< FunctionPtr, FunctionObjectType, ThisLocation >
 Internal metafunction to compute which caller you need based on the pointer types and this location. More...
 
struct  osvr::util::functor_trampolines::GenericCaller< FunctionPtr, FunctionObjectType, ThisLocation >
 Struct containing a single static function member named "call" that serves as a converter from a function call with an opaque userdata pointer to a functor call using the userdata pointer as "this". More...
 

Namespaces

 osvr
 The main namespace for all C++ elements of the framework, internal and external.
 
 osvr::util
 The Util library: Functionality not necessarily coupled to any particular core library, serving more as a common base layer behind all systems.
 

Macros

#define OSVR_UTIL_CALLER_MAX_ARITY   3
 
#define OSVR_PARAM_STEM   p
 what do we call our pass-through parameters
 
#define OSVR_MAKE_PARAMLIST(Z, N, unused)   typename param_at<FPtr, N>::type BOOST_PP_CAT(OSVR_PARAM_STEM, N)
 Macro for use with BOOST_PP_ENUM to generate pass-through argument list.
 
#define OSVR_RETURNTYPE(R)
 Expands to the result type computation if R==1, void if R==0. More...
 
#define OSVR_RETURNSTATEMENT(R)   BOOST_PP_IF(R, return, BOOST_PP_EMPTY())
 Expands to "return" if R==1, nothing if R==0. More...
 
#define OSVR_MAKE_CALLERS(Z, ARITY, RETURNS)
 Generates specializations of CallerThisLast and CallerThisFirst for the given values of ARITY and RETURNS (matching the template parameters). More...
 

Functions

template<typename FunctionPtr , typename FunctionObjectType , typename ThisLocation >
FunctionPtr osvr::util::functor_trampolines::getCaller ()
 Get a generic functor caller: a pointer to a function that will call an object of your specific function object type, expecting the function object address passed as a void * as a parameter. More...
 
template<typename FunctionPtr , typename FunctionObjectType , typename ThisLocation >
FunctionPtr osvr::util::functor_trampolines::getCaller (ThisLocation const &)
 Get a generic functor caller. More...
 
template<typename FunctionPtr , typename FunctionObjectType , typename ThisLocation >
FunctionPtr osvr::util::functor_trampolines::getCaller (FunctionObjectType const *, ThisLocation const &)
 Get a generic functor caller. More...
 

Variables

BOOST_CONSTEXPR_OR_CONST this_last_t this_last = {}
 Pass as an argument to a getCaller() overload to indicate the last parameter of the function contains the "this" pointer. More...
 
BOOST_CONSTEXPR_OR_CONST this_first_t this_first = {}
 Pass as an argument to a getCaller() overload to indicate the first parameter of the function contains the "this" pointer. More...
 

Detailed Description

Header providing templated functions to automate the process of calling a C++ member function (specifically the function call operator) from a facility that accepts a C function pointer and an opaque "userdata" pointer.

Date
2014
Author
Sensics, Inc. http://sensics.com/osvr

Macro Definition Documentation

§ OSVR_MAKE_CALLERS

#define OSVR_MAKE_CALLERS (   Z,
  ARITY,
  RETURNS 
)
Value:
template <> struct Caller<this_last_t, ARITY, RETURNS> { \
template <typename FPtr, typename F> struct Specialized { \
static OSVR_RETURNTYPE(RETURNS) \
call(BOOST_PP_ENUM(ARITY, OSVR_MAKE_PARAMLIST, ~) \
BOOST_PP_COMMA_IF(ARITY) void *functor) { \
F *o = static_cast<F *>(functor); \
OSVR_RETURNSTATEMENT(RETURNS) (*o)( \
BOOST_PP_ENUM_PARAMS(ARITY, OSVR_PARAM_STEM)); \
} \
}; \
}; \
template <> struct Caller<this_first_t, ARITY, RETURNS> { \
template <typename FPtr, typename F> \
static OSVR_RETURNTYPE(RETURNS) \
call(void *functor BOOST_PP_COMMA_IF(ARITY) \
BOOST_PP_ENUM(ARITY, OSVR_MAKE_PARAMLIST, ~)) { \
F *o = static_cast<F *>(functor); \
OSVR_RETURNSTATEMENT(RETURNS) (*o)( \
BOOST_PP_ENUM_PARAMS(ARITY, OSVR_PARAM_STEM)); \
} \
};
#define OSVR_MAKE_PARAMLIST(Z, N, unused)
Macro for use with BOOST_PP_ENUM to generate pass-through argument list.
Definition: GenericCaller.h:125
#define OSVR_PARAM_STEM
what do we call our pass-through parameters
Definition: GenericCaller.h:121
#define OSVR_RETURNTYPE(R)
Expands to the result type computation if R==1, void if R==0.
Definition: GenericCaller.h:131

Generates specializations of CallerThisLast and CallerThisFirst for the given values of ARITY and RETURNS (matching the template parameters).

Intended for use with BOOST_PP_REPEAT, passing a value (0 or 1) for RETURNS in the third, "data" parameter.

§ OSVR_RETURNSTATEMENT

#define OSVR_RETURNSTATEMENT (   R)    BOOST_PP_IF(R, return, BOOST_PP_EMPTY())

Expands to "return" if R==1, nothing if R==0.

Parameters
Rvalue of RETURNS

§ OSVR_RETURNTYPE

#define OSVR_RETURNTYPE (   R)
Value:
BOOST_PP_IF(R, typename boost::function_types::result_type<FPtr>::type, \
void)

Expands to the result type computation if R==1, void if R==0.

Parameters
Rvalue of RETURNS

Function Documentation

§ getCaller() [1/3]

template<typename FunctionPtr , typename FunctionObjectType , typename ThisLocation >
FunctionPtr osvr::util::functor_trampolines::getCaller ( )
inline

Get a generic functor caller: a pointer to a function that will call an object of your specific function object type, expecting the function object address passed as a void * as a parameter.

Template Parameters
FunctionPtrDesired function pointer type
FunctionObjectTypeType of your function object
ThisLocationone of this_first_t or this_last_t indicating which parameter is the "userdata" this pointer.

Function wrapper around GenericCaller.

§ getCaller() [2/3]

template<typename FunctionPtr , typename FunctionObjectType , typename ThisLocation >
FunctionPtr osvr::util::functor_trampolines::getCaller ( ThisLocation const &  )
inline

Get a generic functor caller.

Specify the location of "this" through the argument: results in automatic type deduction for ThisLocation.

Template Parameters
FunctionPtrDesired function pointer type
FunctionObjectTypeType of your function object

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

§ getCaller() [3/3]

template<typename FunctionPtr , typename FunctionObjectType , typename ThisLocation >
FunctionPtr osvr::util::functor_trampolines::getCaller ( FunctionObjectType const *  ,
ThisLocation const &   
)
inline

Get a generic functor caller.

Pass a pointer to a function object and specify the location of "this" through the argument: results in automatic type deduction for FunctionObjectType and ThisLocation.

Note that nothing is actually done with your function object pointer: you still need to pass it along with this function pointer as userdata.

Template Parameters
FunctionPtrDesired function pointer type

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Variable Documentation

§ this_first

BOOST_CONSTEXPR_OR_CONST this_first_t this_first = {}

Pass as an argument to a getCaller() overload to indicate the first parameter of the function contains the "this" pointer.

()

§ this_last

BOOST_CONSTEXPR_OR_CONST this_last_t this_last = {}

Pass as an argument to a getCaller() overload to indicate the last parameter of the function contains the "this" pointer.

()