callable_traits.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef CHAISCRIPT_CALLABLE_TRAITS_HPP_
  7. #define CHAISCRIPT_CALLABLE_TRAITS_HPP_
  8. #include <memory>
  9. namespace chaiscript {
  10. namespace dispatch {
  11. namespace detail {
  12. template<typename Class, typename ... Param>
  13. struct Constructor
  14. {
  15. template<typename ... Inner>
  16. std::shared_ptr<Class> operator()(Inner&& ... inner) const {
  17. return std::make_shared<Class>(std::forward<Inner>(inner)...);
  18. }
  19. };
  20. template<typename Ret, typename Class, typename ... Param>
  21. struct Const_Caller
  22. {
  23. explicit Const_Caller(Ret (Class::*t_func)(Param...) const) : m_func(t_func) {}
  24. template<typename ... Inner>
  25. Ret operator()(const Class &o, Inner&& ... inner) const {
  26. return (o.*m_func)(std::forward<Inner>(inner)...);
  27. }
  28. Ret (Class::*m_func)(Param...) const;
  29. };
  30. template<typename Ret, typename ... Param>
  31. struct Fun_Caller
  32. {
  33. explicit Fun_Caller(Ret( * t_func)(Param...) ) : m_func(t_func) {}
  34. template<typename ... Inner>
  35. Ret operator()(Inner&& ... inner) const {
  36. return (m_func)(std::forward<Inner>(inner)...);
  37. }
  38. Ret(*m_func)(Param...);
  39. };
  40. template<typename Ret, typename Class, typename ... Param>
  41. struct Caller
  42. {
  43. explicit Caller(Ret (Class::*t_func)(Param...)) : m_func(t_func) {}
  44. template<typename ... Inner>
  45. Ret operator()(Class &o, Inner&& ... inner) const {
  46. return (o.*m_func)(std::forward<Inner>(inner)...);
  47. }
  48. Ret (Class::*m_func)(Param...);
  49. };
  50. template<typename T>
  51. struct Arity
  52. {
  53. };
  54. template<typename Ret, typename ... Params>
  55. struct Arity<Ret (Params...)>
  56. {
  57. static const size_t arity = sizeof...(Params);
  58. };
  59. template<typename T>
  60. struct Function_Signature
  61. {
  62. };
  63. template<typename Ret, typename ... Params>
  64. struct Function_Signature<Ret (Params...)>
  65. {
  66. typedef Ret Return_Type;
  67. typedef Ret (Signature)(Params...);
  68. };
  69. template<typename Ret, typename T, typename ... Params>
  70. struct Function_Signature<Ret (T::*)(Params...) const>
  71. {
  72. typedef Ret Return_Type;
  73. typedef Ret (Signature)(Params...);
  74. };
  75. template<typename T>
  76. struct Callable_Traits
  77. {
  78. typedef typename Function_Signature<decltype(&T::operator())>::Signature Signature;
  79. typedef typename Function_Signature<decltype(&T::operator())>::Return_Type Return_Type;
  80. };
  81. }
  82. }
  83. }
  84. #endif