constructor_invoker.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_CONSTRUCTOR_INVOKER_H_
  28. #define RTTR_CONSTRUCTOR_INVOKER_H_
  29. #include "rttr/detail/misc/function_traits.h"
  30. #include "rttr/detail/misc/utility.h"
  31. #include "rttr/detail/policies/ctor_policies.h"
  32. namespace rttr
  33. {
  34. namespace detail
  35. {
  36. struct ctor_type { };
  37. struct ctor_func_type { };
  38. /////////////////////////////////////////////////////////////////////////////////////////
  39. template<typename Ctor_Type, typename Policy, typename Accessor, typename Arg_Indexer>
  40. struct constructor_invoker;
  41. /////////////////////////////////////////////////////////////////////////////////////////
  42. template<typename Class_Type, typename...Ctor_Args, std::size_t... Arg_Count>
  43. struct constructor_invoker<ctor_type, as_raw_pointer, type_list<Class_Type, Ctor_Args...>, index_sequence<Arg_Count...>>
  44. {
  45. using return_type = add_pointer_t<Class_Type>;
  46. template<typename... TArgs>
  47. static RTTR_INLINE variant invoke(TArgs&&...args)
  48. {
  49. if (check_all_true(args. template is_type<Ctor_Args>()...))
  50. return variant(new Class_Type(args. template get_value<Ctor_Args>()...));
  51. else
  52. return variant();
  53. }
  54. };
  55. /////////////////////////////////////////////////////////////////////////////////////////
  56. template<typename Class_Type, typename...Ctor_Args, std::size_t... Arg_Count>
  57. struct constructor_invoker<ctor_type, as_object, type_list<Class_Type, Ctor_Args...>, index_sequence<Arg_Count...>>
  58. {
  59. using return_type = Class_Type;
  60. template<typename... TArgs>
  61. static RTTR_INLINE variant invoke(TArgs&&...args)
  62. {
  63. if (check_all_true(args. template is_type<Ctor_Args>()...))
  64. return variant(Class_Type(args. template get_value<Ctor_Args>()...));
  65. else
  66. return variant();
  67. }
  68. };
  69. /////////////////////////////////////////////////////////////////////////////////////////
  70. template<typename Class_Type, typename...Ctor_Args, std::size_t... Arg_Count>
  71. struct constructor_invoker<ctor_type, as_std_shared_ptr, type_list<Class_Type, Ctor_Args...>, index_sequence<Arg_Count...>>
  72. {
  73. using return_type = std::shared_ptr<Class_Type>;
  74. template<typename... TArgs>
  75. static RTTR_INLINE variant invoke(TArgs&&...args)
  76. {
  77. // we cannot use std::make_shared<T> here because, otherwise we cannot instantiate, constructors which are declared as protected or private
  78. if (check_all_true(args. template is_type<Ctor_Args>()...))
  79. return variant(std::shared_ptr<Class_Type>(new Class_Type(args. template get_value<Ctor_Args>()...)));
  80. else
  81. return variant();
  82. }
  83. };
  84. /////////////////////////////////////////////////////////////////////////////////////////
  85. } // end namespace detail
  86. } // end namespace rttr
  87. #endif // RTTR_CONSTRUCTOR_INVOKER_H_