diff --git a/include/chaiscript/dispatchkit/bind_first.hpp b/include/chaiscript/dispatchkit/bind_first.hpp index 2792724..a00d7ee 100644 --- a/include/chaiscript/dispatchkit/bind_first.hpp +++ b/include/chaiscript/dispatchkit/bind_first.hpp @@ -4,87 +4,130 @@ // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com -#include -#include - -#define param(z,n,text) BOOST_PP_CAT(text, BOOST_PP_INC(n)) - -#ifndef BOOST_PP_IS_ITERATING #ifndef CHAISCRIPT_BIND_FIRST_HPP_ #define CHAISCRIPT_BIND_FIRST_HPP_ - -#define BOOST_PP_ITERATION_LIMITS ( 0, 8 ) -#define BOOST_PP_FILENAME_1 - -#include BOOST_PP_ITERATE() - - -# endif -#else -# define n BOOST_PP_ITERATION() -# define m BOOST_PP_INC(n) +#include namespace chaiscript { namespace detail - { - /// \brief Helper function for binding the first parameter of a class method pointer. Used in chaiscript::fun overloads - /// that take 1 or 2 parameters to pre-bind to the function. - /// - /// \param[in] f method pointer to bind - /// \param[in] o object to bind as first parameter - /// \returns a new std::function object with one fewer parameters than the function passed in. - template - std::function - bind_first(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const O &o) + { + + template + struct Placeholder { - return std::bind(std::mem_fn(f), o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, std::placeholders::_)); + }; + + template<> + struct Placeholder<1> + { + static decltype(std::placeholders::_1) value() { return std::placeholders::_1; } + }; + + template<> + struct Placeholder<2> + { + static decltype(std::placeholders::_2) value() { return std::placeholders::_2; } + }; + + template<> + struct Placeholder<3> + { + static decltype(std::placeholders::_3) value() { return std::placeholders::_3; } + }; + + template<> + struct Placeholder<4> + { + static decltype(std::placeholders::_4) value() { return std::placeholders::_4; } + }; + + template<> + struct Placeholder<5> + { + static decltype(std::placeholders::_5) value() { return std::placeholders::_5; } + }; + + template<> + struct Placeholder<6> + { + static decltype(std::placeholders::_6) value() { return std::placeholders::_6; } + }; + + template<> + struct Placeholder<7> + { + static decltype(std::placeholders::_7) value() { return std::placeholders::_7; } + }; + + template<> + struct Placeholder<8> + { + static decltype(std::placeholders::_8) value() { return std::placeholders::_8; } + }; + + template<> + struct Placeholder<9> + { + static decltype(std::placeholders::_9) value() { return std::placeholders::_9; } + }; + + template<> + struct Placeholder<10> + { + static decltype(std::placeholders::_10) value() { return std::placeholders::_10; } + }; + + + template + struct Bind_First + { + template + static std::function bind(F f, InnerParams ... innerparams) + { + return Bind_First::bind(f, innerparams..., Placeholder::value()); + } + }; + + template + struct Bind_First<0, maxcount, Sig> + { + template + static std::function bind(F f, InnerParams ... innerparams) + { + return std::bind(f, innerparams...); + } + }; + + + template + std::function bind_first(Ret (*f)(P1, Param...), O o) + { + return Bind_First::bind(f, o); } - /// \brief Helper function for binding the first parameter of a const class method pointer. Used in chaiscript::fun overloads - /// that take 1 or 2 parameters to pre-bind to the function. - /// - /// \param[in] f method pointer to bind - /// \param[in] o object to bind as first parameter - /// \returns a new std::function object with one fewer parameters than the function passed in. - template - std::function - bind_first(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)) const, const O &o) + template + std::function bind_first(Ret (Class::*f)(Param...), O o) { - return std::bind(std::mem_fn(f), o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, std::placeholders::_)); + return Bind_First::bind(f, o); } - /// \brief Helper function for binding the first parameter of a function pointer. Used in chaiscript::fun overloads - /// that take 1 or 2 parameters to pre-bind to the function. - /// - /// \param[in] f method pointer to bind - /// \param[in] o object to bind as first parameter - /// \returns a new std::function object with one fewer parameters than the function passed in. - template - std::function - bind_first(Ret (*f)(BOOST_PP_ENUM_PARAMS(m, Param)), const O &o) + template + std::function bind_first(Ret (Class::*f)(Param...) const, O o) { - return std::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, std::placeholders::_)); + return Bind_First::bind(f, o); } - /// \brief Helper function for binding the first parameter of a std::function object. Used in chaiscript::fun overloads - /// that take 1 or 2 parameters to pre-bind to the function. - /// - /// \param[in] f method pointer to bind - /// \param[in] o object to bind as first parameter - /// \returns a new std::function object with one fewer parameters than the function passed in. - template - std::function - bind_first(const std::function &f, const O &o) + template + std::function bind_first(const std::function &f, O o) { - return std::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, std::placeholders::_)); + return Bind_First::bind(f, o); } + } } -#undef n -#undef m #endif