proxy_constructors.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_PROXY_CONSTRUCTORS_HPP_
  9. #define CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
  10. #include "proxy_functions.hpp"
  11. namespace chaiscript
  12. {
  13. namespace dispatch
  14. {
  15. namespace detail
  16. {
  17. template<typename Class, typename ... Params >
  18. Proxy_Function build_constructor_(Class (*)(Params...))
  19. {
  20. auto call = dispatch::detail::Constructor<Class, Params...>();
  21. return Proxy_Function(
  22. chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<std::shared_ptr<Class> (Params...), decltype(call)>>(call));
  23. }
  24. }
  25. }
  26. /// \brief Generates a constructor function for use with ChaiScript
  27. ///
  28. /// \tparam T The signature of the constructor to generate. In the form of: ClassType (ParamType1, ParamType2, ...)
  29. ///
  30. /// Example:
  31. /// \code
  32. /// chaiscript::ChaiScript chai;
  33. /// // Create a new function that creates a MyClass object using the (int, float) constructor
  34. /// // and call that function "MyClass" so that it appears as a normal constructor to the user.
  35. /// chai.add(constructor<MyClass (int, float)>(), "MyClass");
  36. /// \endcode
  37. template<typename T>
  38. Proxy_Function constructor()
  39. {
  40. T *f = nullptr;
  41. return (dispatch::detail::build_constructor_(f));
  42. }
  43. }
  44. #endif