#include <cstddef>
#include "functional.h"
-#include "callbackfwd.h"
-template<typename Type>
-inline void* convertToOpaque( Type* t ){
- return t;
-}
-template<typename Type>
-inline void* convertToOpaque( const Type* t ){
- return const_cast<Type*>( t );
-}
-template<typename Type>
-inline void* convertToOpaque( Type& t ){
- return &t;
-}
-template<typename Type>
-inline void* convertToOpaque( const Type& t ){
- return const_cast<Type*>( &t );
-}
+namespace detail {
+ template<typename Thunk_>
+ class CallbackBase {
+ void *m_environment;
+ Thunk_ m_thunk;
+ public:
+ typedef Thunk_ Thunk;
-template<typename Type>
-class ConvertFromOpaque
-{
-};
+ CallbackBase(void *environment, Thunk function) : m_environment(environment), m_thunk(function) {
+ }
-template<typename Type>
-class ConvertFromOpaque<Type&>
-{
-public:
-static Type& apply( void* p ){
- return *static_cast<Type*>( p );
-}
-};
+ void *getEnvironment() const {
+ return m_environment;
+ }
-template<typename Type>
-class ConvertFromOpaque<const Type&>
-{
-public:
-static const Type& apply( void* p ){
- return *static_cast<Type*>( p );
-}
-};
+ Thunk getThunk() const {
+ return m_thunk;
+ }
+ };
+ template<typename Thunk>
+ inline bool operator==(const CallbackBase<Thunk> &self, const CallbackBase<Thunk> &other) {
+ return self.getEnvironment() == other.getEnvironment() && self.getThunk() == other.getThunk();
+ }
-template<typename Type>
-class ConvertFromOpaque<Type*>
-{
-public:
-static Type* apply( void* p ){
- return static_cast<Type*>( p );
-}
-};
+ template<typename Thunk>
+ inline bool operator!=(const CallbackBase<Thunk> &self, const CallbackBase<Thunk> &other) {
+ return !(self == other);
+ }
-template<typename Type>
-class ConvertFromOpaque<const Type*>
-{
-public:
-static const Type* apply( void* p ){
- return static_cast<Type*>( p );
-}
-};
+ template<typename Thunk>
+ inline bool operator<(const CallbackBase<Thunk> &self, const CallbackBase<Thunk> &other) {
+ return self.getEnvironment() < other.getEnvironment() ||
+ (!(other.getEnvironment() < self.getEnvironment()) && self.getThunk() < other.getThunk());
+ }
-template<typename Thunk_>
-class CallbackBase
-{
-void* m_environment;
-Thunk_ m_thunk;
-public:
-typedef Thunk_ Thunk;
-CallbackBase( void* environment, Thunk function ) : m_environment( environment ), m_thunk( function ){
-}
-void* getEnvironment() const {
- return m_environment;
}
-Thunk getThunk() const {
- return m_thunk;
-}
-};
-template<typename Thunk>
-inline bool operator==( const CallbackBase<Thunk>& self, const CallbackBase<Thunk>& other ){
- return self.getEnvironment() == other.getEnvironment() && self.getThunk() == other.getThunk();
-}
-template<typename Thunk>
-inline bool operator!=( const CallbackBase<Thunk>& self, const CallbackBase<Thunk>& other ){
- return !( self == other );
-}
-template<typename Thunk>
-inline bool operator<( const CallbackBase<Thunk>& self, const CallbackBase<Thunk>& other ){
- return self.getEnvironment() < other.getEnvironment() ||
- ( !( other.getEnvironment() < self.getEnvironment() ) && self.getThunk() < other.getThunk() );
-}
+namespace detail {
-template<class Caller, class F>
-class BindFirstOpaqueN;
+ template<class Type>
+ struct ConvertFromOpaque {
+ };
-template<class Caller, class R, class FirstBound, class... Ts>
-class BindFirstOpaqueN<Caller, R(FirstBound, Ts...)> {
- FirstBound firstBound;
-public:
- explicit BindFirstOpaqueN(FirstBound firstBound) : firstBound(firstBound) {
- }
+ // reference
- R operator()(Ts... args) const {
- return Caller::call(firstBound, args...);
+ template<class T>
+ inline const void *convertToOpaque(const T &t) {
+ return &t;
}
- FirstBound getBound() const {
- return firstBound;
- }
+ template<class T>
+ struct ConvertFromOpaque<const T &> {
+ static T const &apply(void *p) {
+ return *static_cast<const T *>(p);
+ }
+ };
- static R thunk(void *environment, Ts... args) {
- return Caller::call(ConvertFromOpaque<FirstBound>::apply(environment), args...);
+ template<class T>
+ inline void *convertToOpaque(T &t) {
+ return &t;
}
- void *getEnvironment() const {
- return convertToOpaque(firstBound);
- }
-};
+ template<class T>
+ struct ConvertFromOpaque<T &> {
+ static T &apply(void *p) {
+ return *static_cast<T *>( p );
+ }
+ };
-template<class Caller>
-using BindFirstOpaque = BindFirstOpaqueN<Caller, get_func<Caller>>;
+ // pointer
-template<class F>
-class CallbackN;
+ template<class T>
+ inline const void *convertToOpaque(const T *t) {
+ return t;
+ }
-template<class R, class... Ts>
-class CallbackN<R(Ts...)> : public CallbackBase<R(*)(void *, Ts...)> {
- using Base = CallbackBase<R (*)(void *, Ts...)>;
+ template<class T>
+ struct ConvertFromOpaque<const T *> {
+ static const T *apply(void *p) {
+ return static_cast<const T *>(p);
+ }
+ };
- static R nullThunk(void *, Ts...) {
+ template<class T>
+ inline void *convertToOpaque(T *t) {
+ return t;
}
-public:
- using func = R(Ts...);
+ template<class T>
+ struct ConvertFromOpaque<T *> {
+ static T *apply(void *p) {
+ return static_cast<T *>(p);
+ }
+ };
- CallbackN() : Base(0, nullThunk) {
- }
+ // function pointer
- template<typename Caller>
- CallbackN(const BindFirstOpaque<Caller> &caller) : Base(caller.getEnvironment(), BindFirstOpaque<Caller>::thunk) {
+ template<class R, class... Ts>
+ inline const void *convertToOpaque(R(*const &t)(Ts...)) {
+ return &t;
}
- CallbackN(void *environment, typename Base::Thunk function) : Base(environment, function) {
- }
+ template<class R, class... Ts>
+ struct ConvertFromOpaque<R(*const &)(Ts...)> {
+ using Type = R(*)(Ts...);
- R operator()(Ts... args) const {
- return Base::getThunk()(Base::getEnvironment(), args...);
- }
-};
+ static Type const &apply(void *p) {
+ return *static_cast<Type *>(p);
+ }
+ };
-/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer.
-///
-/// Use with the callback constructors MemberCaller, ConstMemberCaller, ReferenceCaller, ConstReferenceCaller, PointerCaller, ConstPointerCaller and FreeCaller.
-template<class Result>
-class Callback0 : public CallbackN<Result()> {
-public:
- using CallbackN<Result()>::CallbackN;
-};
+ template<class R, class... Ts>
+ inline void *convertToOpaque(R(*&t)(Ts...)) {
+ return &t;
+ }
-template<typename Caller>
-inline Callback0<get_result_type<Caller>> makeCallback0(const Caller &caller, get_argument<Caller, 0> callee) {
- return Callback0<get_result_type<Caller>>(BindFirstOpaque<Caller>(callee));
-}
-template<typename Caller>
-inline Callback0<get_result_type<Caller>> makeStatelessCallback0(const Caller &caller) {
- return makeCallback0( Caller0To1<Caller>(), 0 );
-}
+ template<class R, class... Ts>
+ struct ConvertFromOpaque<R(*&)(Ts...)> {
+ using Type = R(*)(Ts...);
-typedef Callback0<void> Callback;
+ static Type &apply(void *p) {
+ return *static_cast<Type *>(p);
+ }
+ };
+ template<class Caller, class F>
+ class BindFirstOpaqueN;
+ template<class Caller, class R, class FirstBound, class... Ts>
+ class BindFirstOpaqueN<Caller, R(FirstBound, Ts...)> {
+ FirstBound firstBound;
+ public:
+ explicit BindFirstOpaqueN(FirstBound firstBound) : firstBound(firstBound) {
+ }
-/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer and one other argument.
-///
-/// Use with the callback constructors MemberCaller1, ConstMemberCaller1, ReferenceCaller1, ConstReferenceCaller1, PointerCaller1, ConstPointerCaller1 and FreeCaller1.
-template<class FirstArgument, class Result>
-class Callback1 : public CallbackN<Result(FirstArgument)> {
-public:
- using CallbackN<Result(FirstArgument)>::CallbackN;
-};
+ R operator()(Ts... args) const {
+ return Caller::call(firstBound, args...);
+ }
-template<typename Caller>
-inline Callback1<get_argument<Caller, 1>, get_result_type<Caller>>
-makeCallback1(const Caller &caller, get_argument<Caller, 0> callee) {
- return Callback1<get_argument<Caller, 1>, get_result_type<Caller>>(BindFirstOpaque<Caller>(callee));
-}
-template<typename Caller>
-inline Callback1<get_argument<Caller, 1>, get_result_type<Caller>> makeStatelessCallback1(const Caller &caller) {
- return makeCallback1( Caller1To2<Caller>(), 0 );
-}
+ FirstBound getBound() const {
+ return firstBound;
+ }
+ static R thunk(void *environment, Ts... args) {
+ return thunk_(detail::ConvertFromOpaque<FirstBound>::apply(environment), args...);
+ }
-/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer and two other arguments.
-///
-template<typename FirstArgument, typename SecondArgument, typename Result>
-class Callback2 : public CallbackN<Result(FirstArgument, SecondArgument)> {
-public:
- using CallbackN<Result(FirstArgument, SecondArgument)>::CallbackN;
-};
+ static R thunk_(FirstBound environment, Ts... args) {
+ return Caller::call(environment, args...);
+ }
+
+ void *getEnvironment() const {
+ return const_cast<void *>(detail::convertToOpaque(firstBound));
+ }
+ };
-template<typename Caller>
-inline Callback2<
- get_argument<Caller, 1>,
- get_argument<Caller, 2>,
- get_result_type<Caller>
-> makeCallback2(const Caller &caller, get_argument<Caller, 0> callee) {
- return Callback2<
- get_argument<Caller, 1>,
- get_argument<Caller, 2>,
- get_result_type<Caller>
- >(BindFirstOpaque<Caller>(callee));
-}
-template<typename Caller>
-inline Callback2<
- get_argument<Caller, 0>,
- get_argument<Caller, 1>,
- get_result_type<Caller>
-> makeStatelessCallback2(const Caller &caller) {
- return makeCallback2( Caller2To3<Caller>(), 0 );
}
+template<class Caller>
+using BindFirstOpaque = detail::BindFirstOpaqueN<Caller, get_func<Caller>>;
-/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer and three other arguments.
+/// \brief Combines a void pointer with a pointer to a function which operates on a void pointer.
///
-template<typename FirstArgument, typename SecondArgument, typename ThirdArgument, typename Result>
-class Callback3 : public CallbackN<Result(FirstArgument, SecondArgument, ThirdArgument)> {
+/// Use with the callback constructors MemberCaller0, ConstMemberCaller0, ReferenceCaller0, ConstReferenceCaller0, PointerCaller0, ConstPointerCaller0 and FreeCaller0.
+template<class F>
+class Callback;
+
+template<class R, class... Ts>
+class Callback<R(Ts...)> : public detail::CallbackBase<R(*)(void *, Ts...)> {
+ using Base = detail::CallbackBase<R (*)(void *, Ts...)>;
+
+ static R nullThunk(void *, Ts...) {
+ }
+
public:
- using CallbackN<Result(FirstArgument, SecondArgument, ThirdArgument)>::CallbackN;
-};
+ using func = R(Ts...);
-template<typename Caller>
-inline Callback3<
- get_argument<Caller, 1>,
- get_argument<Caller, 2>,
- get_argument<Caller, 3>,
- get_result_type<Caller>
-> makeCallback3(const Caller &caller, get_argument<Caller, 0> callee) {
- return Callback3<
- get_argument<Caller, 1>,
- get_argument<Caller, 2>,
- get_argument<Caller, 3>,
- get_result_type<Caller>
- >(BindFirstOpaque<Caller>(callee));
-}
-template<typename Caller>
-inline Callback3<
- get_argument<Caller, 0>,
- get_argument<Caller, 1>,
- get_argument<Caller, 2>,
- get_result_type<Caller>
-> makeStatelessCallback3(const Caller &caller) {
- return makeCallback3( Caller3To4<Caller>(), 0 );
-}
+ Callback() : Base(0, nullThunk) {
+ }
+ template<typename Caller>
+ Callback(const BindFirstOpaque<Caller> &caller) : Base(caller.getEnvironment(), BindFirstOpaque<Caller>::thunk) {
+ }
-/// \brief Forms a Callback from a non-const Environment reference and a non-const Environment member-function.
-///
-/// \dontinclude generic/callback.cpp
-/// \skipline MemberCaller example
-/// \until end example
-template<class Environment, void(Environment::*member)()>
-using MemberCaller = BindFirstOpaque<Member<Environment, void, member>>;
+ Callback(void *environment, typename Base::Thunk function) : Base(environment, function) {
+ }
-/// \brief Forms a Callback from a const Environment reference and a const Environment member-function.
-///
-/// \dontinclude generic/callback.cpp
-/// \skipline MemberCaller example
-/// \until end example
-template<class Environment, void(Environment::*member)() const>
-using ConstMemberCaller = BindFirstOpaque<ConstMember<Environment, void, member>>;
+ R operator()(Ts... args) const {
+ return Base::getThunk()(Base::getEnvironment(), args...);
+ }
+};
-/// \brief Forms a Callback from a non-const Environment reference and a const Environment member-function which takes one argument.
-template<class Environment, class FirstArgument, void(Environment::*member)(FirstArgument)>
-using MemberCaller1 = BindFirstOpaque<Member1<Environment, FirstArgument, void, member>>;
+namespace detail {
+ template<class F>
+ struct Arglist;
-/// \brief Forms a Callback from a const Environment reference and a const Environment member-function which takes one argument.
-template<class Environment, class FirstArgument, void(Environment::*member)(FirstArgument) const>
-using ConstMemberCaller1 = BindFirstOpaque<ConstMember1<Environment, FirstArgument, void, member>>;
+ template<class R, class Head, class... Ts>
+ struct Arglist<R(Head, Ts...)> {
+ using type = R(Head, Ts...);
-/// \brief Forms a Callback from a non-const Environment reference and a free function which operates on a non-const Environment reference.
-///
-/// \dontinclude generic/callback.cpp
-/// \skipline ReferenceCaller example
-/// \until end example
-template<class Environment, void(*func)(Environment &)>
-using ReferenceCaller = BindFirstOpaque<Function1<Environment &, void, func>>;
+ template <class Unshift>
+ using unshift = Arglist<R(Unshift, Head, Ts...)>;
-/// \brief Forms a Callback from a const Environment reference and a free function which operates on a const Environment reference.
-///
-/// \dontinclude generic/callback.cpp
-/// \skipline ReferenceCaller example
-/// \until end example
-template<class Environment, void(*func)(const Environment &)>
-using ConstReferenceCaller = BindFirstOpaque<Function1<const Environment &, void, func>>;
+ using shift = Arglist<R(Ts...)>;
+ };
-/// \brief Forms a Callback from a non-const Environment reference and a free function which operates on a non-const Environment reference and one other argument.
-template<class Environment, class FirstArgument, void(*func)(Environment &, FirstArgument)>
-using ReferenceCaller1 = BindFirstOpaque<Function2<Environment &, FirstArgument, void, func>>;
+ template<class R, class... Ts>
+ struct Arglist<R(Ts...)> {
+ using type = R(Ts...);
-/// \brief Forms a Callback from a const Environment reference and a free function which operates on a const Environment reference and one other argument.
-template<class Environment, class FirstArgument, void(*func)(const Environment &, FirstArgument)>
-using ConstReferenceCaller1 = BindFirstOpaque<Function2<const Environment &, FirstArgument, void, func>>;
+ template <class Unshift>
+ using unshift = Arglist<R(Unshift, Ts...)>;
+ };
-/// \brief Forms a Callback from a non-const Environment pointer and a free function which operates on a non-const Environment pointer.
-template<class Environment, void(*func)(Environment *)>
-using PointerCaller = BindFirstOpaque<Function1<Environment *, void, func>>;
+ template<class F>
+ using ArgShift = typename detail::Arglist<F>::shift::type;
-/// \brief Forms a Callback from a const Environment pointer and a free function which operates on a const Environment pointer.
-template<class Environment, void(*func)(const Environment *)>
-using ConstPointerCaller = BindFirstOpaque<Function1<const Environment *, void, func>>;
+ template<class F, class T>
+ using ArgUnshift = typename detail::Arglist<F>::template unshift<T>::type;
+}
-/// \brief Forms a Callback from a non-const Environment pointer and a free function which operates on a non-const Environment pointer and one other argument.
-template<class Environment, class FirstArgument, void(*func)(Environment *, FirstArgument)>
-using PointerCaller1 = BindFirstOpaque<Function2<Environment *, FirstArgument, void, func>>;
+template<typename Caller>
+inline Callback<detail::ArgShift<get_func<Caller>>> makeCallback(const Caller &caller, get_argument<Caller, 0> callee) {
+ return BindFirstOpaque<Caller>(callee);
+}
-/// \brief Forms a Callback from a const Environment pointer and a free function which operates on a const Environment pointer and one other argument.
-template<class Environment, class FirstArgument, void(*func)(const Environment *, FirstArgument)>
-using ConstPointerCaller1 = BindFirstOpaque<Function2<const Environment *, FirstArgument, void, func>>;
+template<class Caller, class F>
+class CallerShiftFirst;
-/// \brief Forms a Callback from a free function which takes no arguments.
-template<void(*func)()>
-class FreeCaller : public BindFirstOpaque<Caller0To1<Function0<void, func>>> {
+template<class Caller, class R, class FirstArgument, class... Ts>
+class CallerShiftFirst<Caller, R(FirstArgument, Ts...)> {
public:
- FreeCaller() : BindFirstOpaque<Caller0To1<Function0<void, func>>>(0) {
- }
-};
+ using func = R(FirstArgument, Ts...);
-/// \brief Forms a Callback from a free function which takes a single argument.
-template<class FirstArgument, void(*func)(FirstArgument)>
-class FreeCaller1 : public BindFirstOpaque<Caller1To2<Function1<FirstArgument, void, func>>> {
-public:
- FreeCaller1() : BindFirstOpaque<Caller1To2<Function1<FirstArgument, void, func>>>(0) {
+ static R call(FirstArgument, Ts... args) {
+ return Caller::call(args...);
}
};
-
-/// \brief Constructs a Callback from a non-const \p functor with zero arguments.
-///
-/// \param Functor Must define \c operator()().
-template<typename Functor>
-inline Callback makeCallback( Functor& functor ){
- return Callback( MemberCaller<Functor, &Functor::operator()>( functor ) );
+template<typename Caller>
+inline Callback<get_func<Caller>> makeStatelessCallback(const Caller &caller) {
+ return makeCallback(CallerShiftFirst<Caller, detail::ArgUnshift<get_func<Caller>, void *>>(), nullptr);
}
-/// \brief Constructs a Callback from a const \p functor with zero arguments.
-///
-/// \param Functor Must define const \c operator()().
-template<typename Functor>
-inline Callback makeCallback( const Functor& functor ){
- return Callback( ConstMemberCaller<Functor, &Functor::operator()>( functor ) );
-}
+/// \brief Forms a Callback from a non-const Environment reference and a non-const Environment member-function.
+template<class Environment, class F, MemberFunction<Environment, F> member>
+using MemberCaller = BindFirstOpaque<Member<Environment, F, member>>;
-/// \brief Constructs a Callback1 from a non-const \p functor with one argument.
+/// \brief Constructs a Callback1 from a non-const \p functor
///
/// \param Functor Must define \c first_argument_type and \c operator()(first_argument_type).
template<typename Functor>
-inline Callback1<get_argument<Functor, 0>> makeCallback1(Functor &functor) {
- typedef get_argument<Functor, 0> FirstArgument;
- return Callback1<FirstArgument>( MemberCaller1<Functor, FirstArgument, &Functor::operator()>( functor ) );
+inline Callback<get_func<Functor>> makeCallback(Functor &functor) {
+ return MemberCaller<Functor, get_func<Functor>, &Functor::operator()>(functor);
}
-/// \brief Constructs a Callback1 from a const \p functor with one argument.
+/// \brief Forms a Callback from a const Environment reference and a const Environment member-function.
+template<class Environment, class F, ConstMemberFunction<Environment, F> member>
+using ConstMemberCaller = BindFirstOpaque<ConstMember<Environment, F, member>>;
+
+/// \brief Constructs a Callback1 from a const \p functor
///
/// \param Functor Must define \c first_argument_type and const \c operator()(first_argument_type).
template<typename Functor>
-inline Callback1<get_argument<Functor, 0>> makeCallback1(const Functor &functor) {
- typedef get_argument<Functor, 0> FirstArgument;
- return Callback1<FirstArgument>( ConstMemberCaller1<Functor, FirstArgument, &Functor::operator()>( functor ) );
+inline Callback<get_func<Functor>> makeCallback(const Functor &functor) {
+ return ConstMemberCaller<Functor, get_func<Functor>, &Functor::operator()>(functor);
}
+/// \brief Forms a Callback from a non-const Environment reference and a free function which operates on a non-const Environment reference.
+template<class Environment, class F, detail::ArgUnshift<F, Environment &> *func>
+using ReferenceCaller = BindFirstOpaque<Function<detail::ArgUnshift<F, Environment &>, func>>;
-typedef Callback1<bool> BoolImportCallback;
-typedef Callback1<const BoolImportCallback&> BoolExportCallback;
-
-typedef Callback1<int> IntImportCallback;
-typedef Callback1<const IntImportCallback&> IntExportCallback;
+/// \brief Forms a Callback from a const Environment reference and a free function which operates on a const Environment reference.
+template<class Environment, class F, detail::ArgUnshift<F, const Environment &> *func>
+using ConstReferenceCaller = BindFirstOpaque<Function<detail::ArgUnshift<F, const Environment &>, func>>;
-typedef Callback1<float> FloatImportCallback;
-typedef Callback1<const FloatImportCallback&> FloatExportCallback;
+/// \brief Forms a Callback from a non-const Environment pointer and a free function which operates on a non-const Environment pointer.
+template<class Environment, class F, detail::ArgUnshift<F, Environment *> *func>
+using PointerCaller = BindFirstOpaque<Function<detail::ArgUnshift<F, Environment *>, func>>;
-typedef Callback1<const char*> StringImportCallback;
-typedef Callback1<const StringImportCallback&> StringExportCallback;
+/// \brief Forms a Callback from a const Environment pointer and a free function which operates on a const Environment pointer.
+template<class Environment, class F, detail::ArgUnshift<F, const Environment *> *func>
+using ConstPointerCaller = BindFirstOpaque<Function<detail::ArgUnshift<F, const Environment *>, func>>;
+
+namespace detail {
+ template<class Caller, class F>
+ class FreeCaller : public BindFirstOpaque<CallerShiftFirst<Caller, detail::ArgUnshift<F, void *>>> {
+ public:
+ FreeCaller() : BindFirstOpaque<CallerShiftFirst<Caller, detail::ArgUnshift<F, void *>>>(nullptr) {
+ }
+ };
+
+ template<class F>
+ struct FreeCallerWrapper;
+
+ template<class R, class... Ts>
+ struct FreeCallerWrapper<R(Ts...)> {
+ using func = R(void *, Ts...);
+
+ static R call(void *f, Ts... args) {
+ // ideally, we'd get the implementation of the function type directly. Instead, it's passed in
+ return reinterpret_cast<R(*)(Ts...)>(f)(args...);
+ }
+ };
+}
-typedef Callback1<std::size_t> SizeImportCallback;
-typedef Callback1<const SizeImportCallback&> SizeExportCallback;
+/// \brief Forms a Callback from a free function
+template<class F, F *func>
+using FreeCaller = detail::FreeCaller<Function<F, func>, F>;
+template<class R, class... Ts>
+inline Callback<R(Ts...)> makeCallbackF(R(*func)(Ts...)) {
+ void *pVoid = reinterpret_cast<void *>(func);
+ return BindFirstOpaque<detail::FreeCallerWrapper<R(Ts...)>>(pVoid);
+}
#endif