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
42namespace rttr
43{
44
45class variant;
46class constructor;
47class destructor;
48class method;
49class property;
50class enumeration;
51class type;
52class instance;
53class argument;
54
55template<typename Target_Type, typename Source_Type>
56Target_Type rttr_cast(Source_Type object) RTTR_NOEXCEPT;
57
58namespace detail
59{
60struct derived_info;
61struct base_class_info;
62struct type_converter_base;
63class type_register;
64class type_register_private;
65
66static type get_invalid_type() RTTR_NOEXCEPT;
67struct invalid_type{};
68struct type_data;
69class destructor_wrapper_base;
70class property_wrapper_base;
71RTTR_LOCAL RTTR_INLINE type create_type(type_data*) RTTR_NOEXCEPT;
72
73template<typename T>
74RTTR_LOCAL std::unique_ptr<type_data> make_type_data();
75
76template<typename T, typename Tp, typename Converter>
77struct variant_data_base_policy;
78
79struct type_comparator_base;
80
81RTTR_API bool compare_types_less_than(const void*, const void*, const type&, int&);
82RTTR_API bool compare_types_equal(const void*, const void*, const type&, bool&);
83} // end namespace detail
84
170class RTTR_API type
171{
172 public:
173 typedef uintptr_t type_id;
174
179 RTTR_INLINE type(const type& other) RTTR_NOEXCEPT;
180
186 RTTR_INLINE type& operator=(const type& other) RTTR_NOEXCEPT;
187
193 RTTR_INLINE bool operator<(const type& other) const 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
222 RTTR_INLINE bool operator==(const type& other) const RTTR_NOEXCEPT;
223
230 RTTR_INLINE bool operator!=(const type& other) const RTTR_NOEXCEPT;
231
240 RTTR_INLINE type_id get_id() const RTTR_NOEXCEPT;
241
249 RTTR_INLINE string_view get_name() const RTTR_NOEXCEPT;
250
256 RTTR_INLINE bool is_valid() const RTTR_NOEXCEPT;
257
263 RTTR_INLINE explicit operator bool() const RTTR_NOEXCEPT;
264
273 RTTR_INLINE type get_raw_type() const RTTR_NOEXCEPT;
274
295 RTTR_INLINE type get_wrapped_type() const RTTR_NOEXCEPT;
296
302 template<typename T>
303 RTTR_LOCAL static type get() RTTR_NOEXCEPT;
304
315 template<typename T>
316 RTTR_LOCAL static type get(T&& object) RTTR_NOEXCEPT;
317
328 static type get_by_name(string_view name) RTTR_NOEXCEPT;
329
337 static array_range<type> get_types() RTTR_NOEXCEPT;
338
344 RTTR_INLINE std::size_t get_sizeof() const RTTR_NOEXCEPT;
345
351 RTTR_INLINE bool is_class() const RTTR_NOEXCEPT;
352
371 RTTR_INLINE bool is_template_instantiation() const RTTR_NOEXCEPT;
372
395
401 RTTR_INLINE bool is_enumeration() const RTTR_NOEXCEPT;
402
411 enumeration get_enumeration() const RTTR_NOEXCEPT;
412
430 RTTR_INLINE bool is_wrapper() const RTTR_NOEXCEPT;
431
447 RTTR_INLINE bool is_array() const RTTR_NOEXCEPT;
448
457 RTTR_INLINE bool is_associative_container() const RTTR_NOEXCEPT;
458
467 RTTR_INLINE bool is_sequential_container() const RTTR_NOEXCEPT;
468
475 RTTR_INLINE bool is_pointer() const RTTR_NOEXCEPT;
476
484 RTTR_INLINE bool is_arithmetic() const RTTR_NOEXCEPT;
485
492 RTTR_INLINE bool is_function_pointer() const RTTR_NOEXCEPT;
493
500 RTTR_INLINE bool is_member_object_pointer() const RTTR_NOEXCEPT;
501
508 RTTR_INLINE bool is_member_function_pointer() const RTTR_NOEXCEPT;
509
519 bool is_derived_from(const type& other) const RTTR_NOEXCEPT;
520
530 template<typename T>
531 bool is_derived_from() const RTTR_NOEXCEPT;
532
542 bool is_base_of(const type& other) const RTTR_NOEXCEPT;
543
553 template<typename T>
554 bool is_base_of() const RTTR_NOEXCEPT;
555
567 array_range<type> get_base_classes() const RTTR_NOEXCEPT;
568
579 array_range<type> get_derived_classes() const RTTR_NOEXCEPT;
580
584
593 variant get_metadata(const variant& key) const;
594
603 constructor get_constructor(const std::vector<type>& params = std::vector<type>() ) const RTTR_NOEXCEPT;
604
615
665 array_range<constructor> get_constructors(filter_items filter) const RTTR_NOEXCEPT;
666
675 variant create(std::vector<argument> args = std::vector<argument>()) const;
676
685 destructor get_destructor() const RTTR_NOEXCEPT;
686
695 bool destroy(variant& obj) const RTTR_NOEXCEPT;
696
697
705 property get_property(string_view name) const RTTR_NOEXCEPT;
706
717 array_range<property> get_properties() const RTTR_NOEXCEPT;
718
772 array_range<property> get_properties(filter_items filter) const RTTR_NOEXCEPT;
773
781 static property get_global_property(string_view name) RTTR_NOEXCEPT;
782
793
794
803
810
819
826
827
835 method get_method(string_view name) const RTTR_NOEXCEPT;
836
845 method get_method(string_view name, const std::vector<type>& type_list) const RTTR_NOEXCEPT;
846
857 array_range<method> get_methods() const RTTR_NOEXCEPT;
858
912 array_range<method> get_methods(filter_items filter) const RTTR_NOEXCEPT;
913
921 static method get_global_method(string_view name) RTTR_NOEXCEPT;
922
931 static method get_global_method(string_view name, const std::vector<type>& params) RTTR_NOEXCEPT;
932
942 static array_range<method> get_global_methods() RTTR_NOEXCEPT;
943
944
956 variant invoke(string_view name, instance obj, std::vector<argument> args) const;
957
965 static variant invoke(string_view name, std::vector<argument> args);
966
988 template<typename F>
989 static void register_converter_func(F func);
990
1015 template<typename T>
1017
1040 template<typename T>
1042
1065 template<typename T>
1067
1097 template<typename T>
1099
1100 private:
1101
1105 type() RTTR_NOEXCEPT;
1106
1112 RTTR_INLINE explicit type(detail::type_data* data) RTTR_NOEXCEPT;
1113
1122 static void* apply_offset(void* ptr, const type& source_type, const type& target_type) RTTR_NOEXCEPT;
1123
1129 static type get_derived_type(void* ptr, const type& source_type) RTTR_NOEXCEPT;
1130
1138 const detail::type_converter_base* get_type_converter(const type& target_type) const RTTR_NOEXCEPT;
1139
1147 const detail::type_comparator_base* get_equal_comparator() const RTTR_NOEXCEPT;
1148
1156 const detail::type_comparator_base* get_less_than_comparator() const RTTR_NOEXCEPT;
1157
1164 RTTR_INLINE std::size_t get_pointer_dimension() const RTTR_NOEXCEPT;
1165
1173 RTTR_INLINE type get_raw_array_type() const RTTR_NOEXCEPT;
1174
1180 RTTR_INLINE string_view get_full_name() const RTTR_NOEXCEPT;
1181
1186 void create_wrapped_value(const argument& arg, variant& var) const;
1187
1188
1192
1194 RTTR_INLINE variant create_variant(const argument& data) const;
1195
1196 friend class variant;
1197 template<typename Target_Type, typename Source_Type>
1198 friend Target_Type rttr_cast(Source_Type object) RTTR_NOEXCEPT;
1199
1200 friend class instance;
1201 friend class detail::type_register;
1202 friend class detail::type_register_private;
1203
1204 friend type detail::create_type(detail::type_data*) RTTR_NOEXCEPT;
1205
1206 template<typename T>
1207 friend std::unique_ptr<detail::type_data> detail::make_type_data();
1208
1209 template<typename T, typename Tp, typename Converter>
1210 friend struct detail::variant_data_base_policy;
1211
1212 friend RTTR_API bool detail::compare_types_less_than(const void*, const void*, const type&, int&);
1213 friend RTTR_API bool detail::compare_types_equal(const void*, const void*, const type&, bool&);
1214
1215 private:
1216 detail::type_data* m_type_data;
1217};
1218
1219} // end namespace rttr
1220
1221#include "rttr/detail/type/type_impl.h"
1222
1223#endif // RTTR_TYPE_H_
The argument class is used for forwarding arguments to properties or methods.
Definition argument.h:52
The array_range class provides a view into an underlying data structure with lower and upper limits.
Definition array_range.h:64
The constructor class provides several meta information about a constructor and can be invoked.
Definition constructor.h:89
The destructor class provides a destructor for registered types.
Definition destructor.h:73
The enumeration class provides several meta information about an enum.
Definition enumeration.h:113
The instance class is used for forwarding the instance of an object to invoke a property or method.
Definition instance.h:48
The method class provides several meta information about a method and can be invoked.
Definition method.h:121
The property class provides several meta information about a property and gives read/write access to ...
Definition property.h:117
The type class holds the type information for any arbitrary object.
Definition type.h:171
bool is_template_instantiation() const noexcept
Returns true whether the given type is an instantiation of a class template.
destructor get_destructor() const noexcept
Returns the corresponding destructor for this type.
static method get_global_method(string_view name) noexcept
Returns a global method with the name name.
string_view get_name() const noexcept
Returns the unique and human-readable name of the type.
bool is_member_function_pointer() const noexcept
Returns true whether the given type represents a pointer to a member function.
bool is_pointer() const noexcept
Returns true whether the given type represents a pointer.
static array_range< method > get_global_methods() noexcept
Returns a range of all registered global methods.
array_range< type > get_template_arguments() const noexcept
Returns a list of type objects that represents the template arguments.
type & operator=(const type &other) noexcept
Assigns a type to another one.
variant get_property_value(string_view name, instance obj) const
Returns the property value of property named name from the instance obj.
bool destroy(variant &obj) const noexcept
Destroys the contained object in the variant obj.
bool is_class() const noexcept
Returns true whether the given type is class; that is not an atomic type or a method.
static array_range< type > get_types() noexcept
Returns a range of all registered type objects.
static void register_comparators()
Register comparison operators for template type T.
friend class instance
Definition type.h:1200
method get_method(string_view name) const noexcept
Returns a method with the name name.
array_range< type > get_derived_classes() const noexcept
Returns a range of all derived classes of this type.
type get_raw_type() const noexcept
Returns a type object which represent the raw type.
static type get() noexcept
Returns a type object for the given template type T.
static void register_less_than_comparator()
Register the less than comparison operators for template type T.
bool is_valid() const noexcept
Returns true if this type is valid, that means the type holds valid data to a type.
bool is_enumeration() const noexcept
Returns true whether the given type represents an enumeration.
bool operator==(const type &other) const noexcept
Compares this type with the other type and returns true if both describe the same type,...
uintptr_t type_id
Definition type.h:173
friend Target_Type rttr_cast(Source_Type object) noexcept
Casts the given object of type Source_Type to an object of type Target_Type.
array_range< property > get_properties() const noexcept
Returns a range of all registered public properties for this type and all its base classes.
bool operator!=(const type &other) const noexcept
Compares this type with the other type and returns true if both describe different types,...
static type get_by_name(string_view name) noexcept
Returns the type object with the given name name.
array_range< method > get_methods() const noexcept
Returns a range of all registered public methods for this type and all its base classes.
static void register_wrapper_converter_for_base_classes()
Register for all base classes of the giving type T wrapper converter functions.
static void register_converter_func(F func)
Register a converter func F, which will be used internally by the variant class to convert between ty...
bool is_sequential_container() const noexcept
Returns true whether the given type represents an sequence container.
bool is_array() const noexcept
Returns true whether the given type represents an array.
array_range< type > get_base_classes() const noexcept
Returns a range of all base classes of this type.
bool operator>(const type &other) const noexcept
Comparison operator for sorting the type data according to some internal criterion.
bool is_associative_container() const noexcept
Returns true whether the given type represents an associative container.
bool is_function_pointer() const noexcept
Returns true whether the given type represents a pointer to a function e.g.
bool operator<=(const type &other) const noexcept
Comparison operator for sorting the type data according to some internal criterion.
type get_wrapped_type() const noexcept
Returns a type object which represent the wrapped type.
static property get_global_property(string_view name) noexcept
Returns a global property with the name name.
array_range< constructor > get_constructors() const noexcept
Returns a range of all registered public constructors for this type.
bool operator<(const type &other) const noexcept
Comparison operator for sorting the type data according to some internal criterion.
type_id get_id() const noexcept
Returns the id of this type.
bool is_base_of(const type &other) const noexcept
Returns true if this type is the base class from the given type other, otherwise false.
enumeration get_enumeration() const noexcept
Returns the enumerator if this type is an enum type; otherwise the returned value is not valid.
std::size_t get_sizeof() const noexcept
Returns the size in bytes of the object representation of the current type (i.e.
bool is_arithmetic() const noexcept
Returns true whether the given type represents an arithmetic type.
bool is_member_object_pointer() const noexcept
Returns true whether the given type represents a pointer to a member object.
bool is_derived_from(const type &other) const noexcept
Returns true if this type is derived from the given type other, otherwise false.
variant create(std::vector< argument > args=std::vector< argument >()) const
Creates an instance of the current type, with the given arguments args for the constructor.
friend class variant
Definition type.h:1196
bool operator>=(const type &other) const noexcept
Comparison operator for sorting the type data according to some internal criterion.
constructor get_constructor(const std::vector< type > &params=std::vector< type >()) const noexcept
Returns a public constructor whose parameters match the types in the specified list.
property get_property(string_view name) const noexcept
Returns a property with the name name.
static array_range< property > get_global_properties() noexcept
Returns a range of all registered global properties.
variant get_metadata(const variant &key) const
Returns the meta data for the given key key.
bool is_wrapper() const noexcept
Returns true whether the given type represents a wrapper type.
bool set_property_value(string_view name, instance obj, argument arg) const
This function will set the given value arg to a property named name to the instance obj.
static void register_equal_comparator()
Register the equal comparison operators for template type T.
variant invoke(string_view name, instance obj, std::vector< argument > args) const
Invokes the method represented by the current instance object.
type(const type &other) noexcept
Assigns a type to another one.
The variant class allows to store data of any type and convert between these types transparently.
Definition variant.h:198
Definition access_levels.h:34
Target_Type rttr_cast(Source_Type object) noexcept
Casts the given object of type Source_Type to an object of type Target_Type.
basic_string_view< char > string_view
A class to hold a reference to a continuous sequence of char objects.
Definition string_view.h:493