// This file is distributed under the BSD License. // See "license.txt" for details. // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2017, Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com #ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_ #define CHAISCRIPT_REGISTER_FUNCTION_HPP_ #include #include "bind_first.hpp" #include "proxy_functions.hpp" namespace chaiscript { /// \brief Creates a new Proxy_Function object from a free function, member function or data member /// \param[in] t Function / member to expose /// /// \b Example: /// \code /// int myfunction(const std::string &); /// class MyClass /// { /// public: /// void memberfunction(); /// int memberdata; /// }; /// /// chaiscript::ChaiScript chai; /// chai.add(fun(&myfunction), "myfunction"); /// chai.add(fun(&MyClass::memberfunction), "memberfunction"); /// chai.add(fun(&MyClass::memberdata), "memberdata"); /// \endcode /// /// \sa \ref adding_functions template Proxy_Function fun(const T &t) { typedef typename dispatch::detail::Callable_Traits::Signature Signature; return Proxy_Function( chaiscript::make_shared>(t)); } template Proxy_Function fun(Ret (*func)(Param...)) { auto fun_call = dispatch::detail::Fun_Caller(func); return Proxy_Function( chaiscript::make_shared>(fun_call)); } template Proxy_Function fun(Ret (Class::*t_func)(Param...) const) { auto call = dispatch::detail::Const_Caller(t_func); return Proxy_Function( chaiscript::make_shared>(call)); } template Proxy_Function fun(Ret (Class::*t_func)(Param...)) { auto call = dispatch::detail::Caller(t_func); return Proxy_Function( chaiscript::make_shared>(call)); } template::value>::type*/> Proxy_Function fun(T Class::* m /*, typename std::enable_if::value>::type* = 0*/ ) { return Proxy_Function(chaiscript::make_shared>(m)); } /// \brief Creates a new Proxy_Function object from a free function, member function or data member and binds the first parameter of it /// \param[in] t Function / member to expose /// \param[in] q Value to bind to first parameter /// /// \b Example: /// \code /// struct MyClass /// { /// void memberfunction(int); /// }; /// /// MyClass obj; /// chaiscript::ChaiScript chai; /// // Add function taking only one argument, an int, and permanently bound to "obj" /// chai.add(fun(&MyClass::memberfunction, std::ref(obj)), "memberfunction"); /// \endcode /// /// \sa \ref adding_functions template Proxy_Function fun(T &&t, const Q &q) { return fun(detail::bind_first(std::forward(t), q)); } } #endif