rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
constructor_container.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014 Axel Menzel <info@axelmenzel.de> *
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 
28 #ifndef __RTTR_CONSTRUCTOR_CONTAINER_H__
29 #define __RTTR_CONSTRUCTOR_CONTAINER_H__
30 
33 #include "rttr/detail/argument.h"
34 #include "rttr/detail/utility.h"
35 #include "rttr/variant.h"
36 
37 #include <vector>
38 #include <utility>
39 #include <type_traits>
40 
41 namespace rttr
42 {
43 namespace detail
44 {
45 
49 
50 template<typename ClassType, typename... Args>
51 class constructor_container : public constructor_container_base
52 {
53  public:
54  constructor_container() : constructor_container_base(type::get<typename raw_type<ClassType>::type>()) {}
55  RTTR_INLINE std::vector<type> get_parameter_types_impl(std::false_type) const { return {}; }
56  RTTR_INLINE std::vector<type> get_parameter_types_impl(std::true_type) const { return { type::get<Args>()...}; }
57  std::vector<type> get_parameter_types() const { return get_parameter_types_impl(std::integral_constant<bool, sizeof...(Args) != 0>()); }
58 
59  type get_instanciated_type() const { return type::get<ClassType*>(); }
60 
61  RTTR_INLINE std::vector<bool> get_is_reference_impl(std::true_type) const { return {std::is_reference<Args>::value...}; }
62  RTTR_INLINE std::vector<bool> get_is_reference_impl(std::false_type) const { return {}; }
63 
64  RTTR_INLINE std::vector<bool> get_is_const_impl(std::true_type) const { return {std::is_const<typename std::remove_reference<Args>::type>::value...}; }
65  RTTR_INLINE std::vector<bool> get_is_const_impl(std::false_type) const { return {}; }
66 
67  std::vector<bool> get_is_reference() const { return get_is_reference_impl(std::integral_constant<bool, sizeof...(Args) != 0>()); }
68  std::vector<bool> get_is_const() const { return get_is_const_impl(std::integral_constant<bool, sizeof...(Args) != 0>()); }
69 
70  template<typename... TArgs>
71  RTTR_INLINE variant invoke_variadic_extracted(TArgs&... args) const
72  {
73  if (check_all_true(args. template is_type<Args>()...))
74  return (new ClassType(args. template get_value<Args>()...));
75  else
76  return variant();
77  }
78 
79  template<typename... TArgs>
80  RTTR_INLINE variant invoke_impl(std::true_type, TArgs&... args) const
81  {
82  return invoke_variadic_extracted(args...);
83  }
84 
85  template<typename... TArgs>
86  RTTR_INLINE variant invoke_impl(std::false_type, TArgs&... args) const
87  {
88  return variant();
89  }
90 
91  variant invoke() const
92  {
93  return invoke_impl(std::integral_constant<bool, 0 == sizeof...(Args)>());
94  }
95 
96  variant invoke(detail::argument& arg1) const
97  {
98  return invoke_impl(std::integral_constant<bool, 1 == sizeof...(Args)>(), arg1);
99  }
100  variant invoke(detail::argument& arg1, detail::argument& arg2) const
101  {
102  return invoke_impl(std::integral_constant<bool, 2 == sizeof...(Args)>(), arg1, arg2);
103  }
104  variant invoke(detail::argument& arg1, detail::argument& arg2, detail::argument& arg3) const
105  {
106  return invoke_impl(std::integral_constant<bool, 3 == sizeof...(Args)>(), arg1, arg2, arg3);
107  }
108  variant invoke(detail::argument& arg1, detail::argument& arg2, detail::argument& arg3, detail::argument& arg4) const
109  {
110  return invoke_impl(std::integral_constant<bool, 4 == sizeof...(Args)>(), arg1, arg2, arg3, arg4);
111  }
112  variant invoke(detail::argument& arg1, detail::argument& arg2, detail::argument& arg3, detail::argument& arg4, detail::argument& arg5) const
113  {
114  return invoke_impl(std::integral_constant<bool, 5 == sizeof...(Args)>(), arg1, arg2, arg3, arg4, arg5);
115  }
116  variant invoke(detail::argument& arg1, detail::argument& arg2, detail::argument& arg3, detail::argument& arg4, detail::argument& arg5, detail::argument& arg6) const
117  {
118  return invoke_impl(std::integral_constant<bool, 6 == sizeof...(Args)>(), arg1, arg2, arg3, arg4, arg5, arg6);
119  }
120 
121  template<std::size_t ...I>
122  RTTR_INLINE variant invoke_variadic_impl(std::vector<detail::argument>& args, index_sequence<I...>) const
123  {
124  if (args.size() == sizeof...(I))
125  return invoke_variadic_extracted(args[I]...);
126  else
127  return variant();
128  }
129 
130  variant invoke_variadic(std::vector<detail::argument>& args) const
131  {
132  return invoke_variadic_impl(args, make_index_sequence<sizeof...(Args)>());
133  }
134 };
135 
137 
138 } // end namespace detail
139 } // end namespace rttr
140 
141 #endif // __RTTR_CONSTRUCTOR_CONTAINER_H__
#define RTTR_INLINE
Definition: core_prerequisites.h:90