register_function.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // This file is distributed under the BSD License.
  2. // See "license.txt" for details.
  3. // Copyright 2009-2012, Jonathan Turner ([email protected])
  4. // Copyright 2009-2017, Jason Turner ([email protected])
  5. // http://www.chaiscript.com
  6. // This is an open source non-commercial project. Dear PVS-Studio, please check it.
  7. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  8. #ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_
  9. #define CHAISCRIPT_REGISTER_FUNCTION_HPP_
  10. #include <type_traits>
  11. #include "bind_first.hpp"
  12. #include "proxy_functions.hpp"
  13. namespace chaiscript
  14. {
  15. /// \brief Creates a new Proxy_Function object from a free function, member function or data member
  16. /// \param[in] t Function / member to expose
  17. ///
  18. /// \b Example:
  19. /// \code
  20. /// int myfunction(const std::string &);
  21. /// class MyClass
  22. /// {
  23. /// public:
  24. /// void memberfunction();
  25. /// int memberdata;
  26. /// };
  27. ///
  28. /// chaiscript::ChaiScript chai;
  29. /// chai.add(fun(&myfunction), "myfunction");
  30. /// chai.add(fun(&MyClass::memberfunction), "memberfunction");
  31. /// chai.add(fun(&MyClass::memberdata), "memberdata");
  32. /// \endcode
  33. ///
  34. /// \sa \ref adding_functions
  35. template<typename T>
  36. Proxy_Function fun(const T &t)
  37. {
  38. typedef typename dispatch::detail::Callable_Traits<T>::Signature Signature;
  39. return Proxy_Function(
  40. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Signature, T>>(t));
  41. }
  42. template<typename Ret, typename ... Param>
  43. Proxy_Function fun(Ret (*func)(Param...))
  44. {
  45. auto fun_call = dispatch::detail::Fun_Caller<Ret, Param...>(func);
  46. return Proxy_Function(
  47. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Param...), decltype(fun_call)>>(fun_call));
  48. }
  49. template<typename Ret, typename Class, typename ... Param>
  50. Proxy_Function fun(Ret (Class::*t_func)(Param...) const)
  51. {
  52. auto call = dispatch::detail::Const_Caller<Ret, Class, Param...>(t_func);
  53. return Proxy_Function(
  54. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (const Class &, Param...), decltype(call)>>(call));
  55. }
  56. template<typename Ret, typename Class, typename ... Param>
  57. Proxy_Function fun(Ret (Class::*t_func)(Param...))
  58. {
  59. auto call = dispatch::detail::Caller<Ret, Class, Param...>(t_func);
  60. return Proxy_Function(
  61. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Class &, Param...), decltype(call)>>(call));
  62. }
  63. template<typename T, typename Class /*, typename = typename std::enable_if<std::is_member_object_pointer<T>::value>::type*/>
  64. Proxy_Function fun(T Class::* m /*, typename std::enable_if<std::is_member_object_pointer<T>::value>::type* = 0*/ )
  65. {
  66. return Proxy_Function(chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Attribute_Access<T, Class>>(m));
  67. }
  68. // only compile this bit if noexcept is part of the type system
  69. //
  70. #if __cpp_noexcept_function_type >= 201510
  71. template<typename Ret, typename ... Param>
  72. Proxy_Function fun(Ret (*func)(Param...) noexcept)
  73. {
  74. auto fun_call = dispatch::detail::Fun_Caller<Ret, Param...>(func);
  75. return Proxy_Function(
  76. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Param...), decltype(fun_call)>>(fun_call));
  77. }
  78. template<typename Ret, typename Class, typename ... Param>
  79. Proxy_Function fun(Ret (Class::*t_func)(Param...) const noexcept)
  80. {
  81. auto call = dispatch::detail::Const_Caller<Ret, Class, Param...>(t_func);
  82. return Proxy_Function(
  83. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (const Class &, Param...), decltype(call)>>(call));
  84. }
  85. template<typename Ret, typename Class, typename ... Param>
  86. Proxy_Function fun(Ret (Class::*t_func)(Param...) noexcept)
  87. {
  88. auto call = dispatch::detail::Caller<Ret, Class, Param...>(t_func);
  89. return Proxy_Function(
  90. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Class &, Param...), decltype(call)>>(call));
  91. }
  92. #endif
  93. /// \brief Creates a new Proxy_Function object from a free function, member function or data member and binds the first parameter of it
  94. /// \param[in] t Function / member to expose
  95. /// \param[in] q Value to bind to first parameter
  96. ///
  97. /// \b Example:
  98. /// \code
  99. /// struct MyClass
  100. /// {
  101. /// void memberfunction(int);
  102. /// };
  103. ///
  104. /// MyClass obj;
  105. /// chaiscript::ChaiScript chai;
  106. /// // Add function taking only one argument, an int, and permanently bound to "obj"
  107. /// chai.add(fun(&MyClass::memberfunction, std::ref(obj)), "memberfunction");
  108. /// \endcode
  109. ///
  110. /// \sa \ref adding_functions
  111. template<typename T, typename Q>
  112. Proxy_Function fun(T &&t, const Q &q)
  113. {
  114. return fun(detail::bind_first(std::forward<T>(t), q));
  115. }
  116. }
  117. #endif