type.h
Go to the documentation of this file.
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 
28 #ifndef RTTR_TYPE_H_
29 #define RTTR_TYPE_H_
30 
31 #include "rttr/detail/base/core_prerequisites.h"
32 #include "rttr/string_view.h"
33 #include "rttr/array_range.h"
34 #include "rttr/filter_item.h"
35 
36 #include <type_traits>
37 #include <vector>
38 #include <string>
39 #include <memory>
40 #include <cstdint>
41 
42 namespace rttr
43 {
44 
45 class variant;
46 class constructor;
47 class destructor;
48 class method;
49 class property;
50 class enumeration;
51 class type;
52 class instance;
53 class argument;
54 class visitor;
55 
56 template<typename Target_Type, typename Source_Type>
57 Target_Type rttr_cast(Source_Type object) RTTR_NOEXCEPT;
58 
59 namespace detail
60 {
61 struct derived_info;
62 struct base_class_info;
63 struct type_converter_base;
64 class type_register;
65 class type_register_private;
66 
67 static type get_invalid_type() RTTR_NOEXCEPT;
68 struct invalid_type{};
69 struct type_data;
70 struct class_data;
71 class destructor_wrapper_base;
72 class property_wrapper_base;
73 RTTR_LOCAL RTTR_INLINE type create_type(type_data*) RTTR_NOEXCEPT;
74 
75 template<typename T>
76 RTTR_LOCAL std::unique_ptr<type_data> make_type_data();
77 
78 template<typename T, typename Tp, typename Converter>
79 struct variant_data_base_policy;
80 
81 struct type_comparator_base;
82 
83 enum class type_of_visit : bool;
84 
85 RTTR_API bool compare_types_less_than(const void*, const void*, const type&, int&);
86 RTTR_API bool compare_types_equal(const void*, const void*, const type&, bool&);
87 
88 template<typename T>
89 RTTR_LOCAL RTTR_INLINE type get_type_from_instance(const T*) RTTR_NOEXCEPT;
90 } // end namespace detail
91 
177 class RTTR_API type
178 {
179  public:
180  typedef uintptr_t type_id;
181 
186  RTTR_INLINE type(const type& other) RTTR_NOEXCEPT;
187 
193  RTTR_INLINE type& operator=(const type& other) RTTR_NOEXCEPT;
194 
200  RTTR_INLINE bool operator<(const type& other) const RTTR_NOEXCEPT;
201 
207  RTTR_INLINE bool operator>(const type& other) const RTTR_NOEXCEPT;
208 
214  RTTR_INLINE bool operator>=(const type& other) const RTTR_NOEXCEPT;
215 
221  RTTR_INLINE bool operator<=(const type& other) const RTTR_NOEXCEPT;
222 
229  RTTR_INLINE bool operator==(const type& other) const RTTR_NOEXCEPT;
230 
237  RTTR_INLINE bool operator!=(const type& other) const RTTR_NOEXCEPT;
238 
247  RTTR_INLINE type_id get_id() const RTTR_NOEXCEPT;
248 
256  RTTR_INLINE string_view get_name() const RTTR_NOEXCEPT;
257 
263  RTTR_INLINE bool is_valid() const RTTR_NOEXCEPT;
264 
270  RTTR_INLINE explicit operator bool() const RTTR_NOEXCEPT;
271 
280  RTTR_INLINE type get_raw_type() const RTTR_NOEXCEPT;
281 
302  RTTR_INLINE type get_wrapped_type() const RTTR_NOEXCEPT;
303 
309  template<typename T>
310  RTTR_LOCAL static type get() RTTR_NOEXCEPT;
311 
322  template<typename T>
323  RTTR_LOCAL static type get(T&& object) RTTR_NOEXCEPT;
324 
335  static type get_by_name(string_view name) RTTR_NOEXCEPT;
336 
344  static array_range<type> get_types() RTTR_NOEXCEPT;
345 
351  RTTR_INLINE std::size_t get_sizeof() const RTTR_NOEXCEPT;
352 
358  RTTR_INLINE bool is_class() const RTTR_NOEXCEPT;
359 
378  RTTR_INLINE bool is_template_instantiation() const RTTR_NOEXCEPT;
379 
401  array_range<type> get_template_arguments() const RTTR_NOEXCEPT;
402 
408  RTTR_INLINE bool is_enumeration() const RTTR_NOEXCEPT;
409 
418  enumeration get_enumeration() const RTTR_NOEXCEPT;
419 
437  RTTR_INLINE bool is_wrapper() const RTTR_NOEXCEPT;
438 
454  RTTR_INLINE bool is_array() const RTTR_NOEXCEPT;
455 
464  RTTR_INLINE bool is_associative_container() const RTTR_NOEXCEPT;
465 
474  RTTR_INLINE bool is_sequential_container() const RTTR_NOEXCEPT;
475 
482  RTTR_INLINE bool is_pointer() const RTTR_NOEXCEPT;
483 
491  RTTR_INLINE bool is_arithmetic() const RTTR_NOEXCEPT;
492 
499  RTTR_INLINE bool is_function_pointer() const RTTR_NOEXCEPT;
500 
507  RTTR_INLINE bool is_member_object_pointer() const RTTR_NOEXCEPT;
508 
515  RTTR_INLINE bool is_member_function_pointer() const RTTR_NOEXCEPT;
516 
526  bool is_derived_from(const type& other) const RTTR_NOEXCEPT;
527 
537  template<typename T>
538  bool is_derived_from() const RTTR_NOEXCEPT;
539 
549  bool is_base_of(const type& other) const RTTR_NOEXCEPT;
550 
560  template<typename T>
561  bool is_base_of() const RTTR_NOEXCEPT;
562 
574  array_range<type> get_base_classes() const RTTR_NOEXCEPT;
575 
586  array_range<type> get_derived_classes() const RTTR_NOEXCEPT;
587 
591 
600  variant get_metadata(const variant& key) const;
601 
610  constructor get_constructor(const std::vector<type>& params = std::vector<type>() ) const RTTR_NOEXCEPT;
611 
621  array_range<constructor> get_constructors() const RTTR_NOEXCEPT;
622 
672  array_range<constructor> get_constructors(filter_items filter) const RTTR_NOEXCEPT;
673 
682  variant create(std::vector<argument> args = std::vector<argument>()) const;
683 
692  destructor get_destructor() const RTTR_NOEXCEPT;
693 
702  bool destroy(variant& obj) const RTTR_NOEXCEPT;
703 
704 
712  property get_property(string_view name) const RTTR_NOEXCEPT;
713 
724  array_range<property> get_properties() const RTTR_NOEXCEPT;
725 
779  array_range<property> get_properties(filter_items filter) const RTTR_NOEXCEPT;
780 
788  static property get_global_property(string_view name) RTTR_NOEXCEPT;
789 
799  static array_range<property> get_global_properties() RTTR_NOEXCEPT;
800 
801 
809  variant get_property_value(string_view name, instance obj) const;
810 
816  static variant get_property_value(string_view name);
817 
825  bool set_property_value(string_view name, instance obj, argument arg) const;
826 
832  static bool set_property_value(string_view name, argument arg);
833 
834 
842  method get_method(string_view name) const RTTR_NOEXCEPT;
843 
852  method get_method(string_view name, const std::vector<type>& type_list) const RTTR_NOEXCEPT;
853 
864  array_range<method> get_methods() const RTTR_NOEXCEPT;
865 
919  array_range<method> get_methods(filter_items filter) const RTTR_NOEXCEPT;
920 
928  static method get_global_method(string_view name) RTTR_NOEXCEPT;
929 
938  static method get_global_method(string_view name, const std::vector<type>& params) RTTR_NOEXCEPT;
939 
949  static array_range<method> get_global_methods() RTTR_NOEXCEPT;
950 
951 
963  variant invoke(string_view name, instance obj, std::vector<argument> args) const;
964 
972  static variant invoke(string_view name, std::vector<argument> args);
973 
995  template<typename F>
996  static void register_converter_func(F func);
997 
1022  template<typename T>
1023  static void register_wrapper_converter_for_base_classes();
1024 
1047  template<typename T>
1048  static void register_comparators();
1049 
1072  template<typename T>
1073  static void register_equal_comparator();
1074 
1104  template<typename T>
1105  static void register_less_than_comparator();
1106 
1107  private:
1108 
1112  type() RTTR_NOEXCEPT;
1113 
1119  RTTR_INLINE explicit type(detail::type_data* data) RTTR_NOEXCEPT;
1120 
1129  static void* apply_offset(void* ptr, const type& source_type, const type& target_type) RTTR_NOEXCEPT;
1130 
1136  static type get_derived_type(void* ptr, const type& source_type) RTTR_NOEXCEPT;
1137 
1145  const detail::type_converter_base* get_type_converter(const type& target_type) const RTTR_NOEXCEPT;
1146 
1154  const detail::type_comparator_base* get_equal_comparator() const RTTR_NOEXCEPT;
1155 
1163  const detail::type_comparator_base* get_less_than_comparator() const RTTR_NOEXCEPT;
1164 
1171  RTTR_INLINE std::size_t get_pointer_dimension() const RTTR_NOEXCEPT;
1172 
1180  RTTR_INLINE type get_raw_array_type() const RTTR_NOEXCEPT;
1181 
1187  RTTR_INLINE string_view get_full_name() const RTTR_NOEXCEPT;
1188 
1193  void create_wrapped_value(const argument& arg, variant& var) const;
1194 
1198  void visit(visitor& visitor, detail::type_of_visit visit_type) const RTTR_NOEXCEPT;
1199 
1203 
1205  RTTR_INLINE variant create_variant(const argument& data) const;
1206 
1207  friend class variant;
1208  template<typename Target_Type, typename Source_Type>
1209  friend Target_Type rttr_cast(Source_Type object) RTTR_NOEXCEPT;
1210 
1211  friend class instance;
1212  friend class detail::type_register;
1213  friend class detail::type_register_private;
1214  friend class visitor;
1215  friend struct detail::class_data;
1216 
1217  friend type detail::create_type(detail::type_data*) RTTR_NOEXCEPT;
1218 
1219  template<typename T>
1220  friend std::unique_ptr<detail::type_data> detail::make_type_data();
1221 
1222  template<typename T, typename Tp, typename Converter>
1223  friend struct detail::variant_data_base_policy;
1224 
1225  friend RTTR_API bool detail::compare_types_less_than(const void*, const void*, const type&, int&);
1226  friend RTTR_API bool detail::compare_types_equal(const void*, const void*, const type&, bool&);
1227 
1228  private:
1229  detail::type_data* m_type_data;
1230 };
1231 
1232 } // end namespace rttr
1233 
1234 #include "rttr/detail/type/type_impl.h"
1235 
1236 #endif // RTTR_TYPE_H_
constexpr bool operator>=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
Definition: access_levels.h:33
constexpr bool operator==(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
The array_range class provides a view into an underlying data structure with lower and upper limits...
Definition: array_range.h:63
constexpr bool operator<=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
The destructor class provides a destructor for registered types.
Definition: destructor.h:72
The instance class is used for forwarding the instance of an object to invoke a property or method...
Definition: instance.h:47
The enumeration class provides several meta information about an enum.
Definition: enumeration.h:112
The method class provides several meta information about a method and can be invoked.
Definition: method.h:121
uintptr_t type_id
Definition: type.h:180
The class visitor, is used for visiting your registered accessors of a type at compile time...
Definition: visitor.h:98
constexpr bool operator!=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
The class template basic_string_view describes an non-owning reference to a constant contiguous seque...
Definition: string_view.h:48
The argument class is used for forwarding arguments to properties or methods.
Definition: argument.h:51
The property class provides several meta information about a property and gives read/write access to ...
Definition: property.h:117
The variant class allows to store data of any type and convert between these types transparently...
Definition: variant.h:197
Target_Type rttr_cast(Source_Type object) noexcept
Casts the given object of type Source_Type to an object of type Target_Type.
Contains a list of template parameters.
Definition: type_list.h:40
The constructor class provides several meta information about a constructor and can be invoked...
Definition: constructor.h:89
constexpr bool operator>(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
constexpr bool operator<(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.