variant.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_VARIANT_H_
29#define RTTR_VARIANT_H_
30
31#include "rttr/detail/base/core_prerequisites.h"
32#include "rttr/detail/misc/misc_type_traits.h"
33#include "rttr/detail/variant/variant_data.h"
34#include "rttr/detail/misc/argument_wrapper.h"
35#include "rttr/detail/variant/variant_compare.h"
36
37#include <type_traits>
38#include <cstddef>
39#include <cstdint>
40#include <algorithm>
41
42namespace rttr
43{
44
47class type;
48class variant;
49class argument;
50class instance;
51
52namespace detail
53{
54 template<class T>
55 RTTR_INLINE T* unsafe_variant_cast(variant* operand) RTTR_NOEXCEPT;
56 template<class T>
57 RTTR_INLINE const T* unsafe_variant_cast(const variant* operand) RTTR_NOEXCEPT;
58
59 struct data_address_container;
60 template<typename T>
61 struct empty_type_converter;
62
63 template<typename T, typename Tp, typename Converter = empty_type_converter<T>>
64 struct variant_data_base_policy;
65 struct variant_data_policy_nullptr_t;
66
67 enum class variant_policy_operation : uint8_t;
68
69 template<typename T, typename Decayed = decay_except_array_t<T>>
70 using decay_variant_t = enable_if_t<!std::is_same<Decayed, variant>::value, Decayed>;
71
72 using variant_policy_func = bool (*)(variant_policy_operation, const variant_data&, argument_wrapper);
73}
74
197class RTTR_API variant
198{
199 public:
205 RTTR_INLINE variant();
206
211 template<typename T, typename Tp = detail::decay_variant_t<T>>
212 variant(T&& val);
213
217 variant(const variant& other);
218
222 variant(variant&& other);
223
227 RTTR_INLINE ~variant();
228
234 template<typename T, typename Tp = detail::decay_variant_t<T>>
235 variant& operator=(T&& other);
236
243
249 variant& operator=(const variant& other);
250
267 RTTR_INLINE bool operator==(const variant& other) const;
268
282 RTTR_INLINE bool operator!=(const variant& other) const;
283
300 RTTR_INLINE bool operator<(const variant& other) const;
301
302
316 RTTR_INLINE bool operator<=(const variant& other) const;
317
331 RTTR_INLINE bool operator>=(const variant& other) const;
332
345 RTTR_INLINE bool operator>(const variant& other) const;
346
352 void clear();
353
357 void swap(variant& other);
358
364 template<typename T>
365 bool is_type() const;
366
374 type get_type() const;
375
387 bool is_valid() const;
388
396 explicit operator bool() const;
397
405
413
437 template<typename T>
439
463 template<typename T>
464 const T& get_value() const;
465
485 template<typename T>
486 const T& get_wrapped_value() const;
487
512
521 template<typename T>
522 bool can_convert() const;
523
537 bool can_convert(const type& target_type) const;
538
568 bool convert(const type& target_type);
569
596 template<typename T>
597 T convert(bool* ok = nullptr) const;
598
627 template<typename T>
628 bool convert(T& value) const;
629
662
693
708 bool to_bool() const;
709
727 int to_int(bool *ok = nullptr) const;
728
746 float to_float(bool* ok = nullptr) const;
747
765 double to_double(bool* ok = nullptr) const;
766
780 std::string to_string(bool *ok = nullptr) const;
781
799 int8_t to_int8(bool *ok = nullptr) const;
800
818 int16_t to_int16(bool *ok = nullptr) const;
819
837 int32_t to_int32(bool *ok = nullptr) const;
838
856 int64_t to_int64(bool *ok = nullptr) const;
857
876 uint8_t to_uint8(bool *ok = nullptr) const;
877
896 uint16_t to_uint16(bool *ok = nullptr) const;
897
916 uint32_t to_uint32(bool *ok = nullptr) const;
917
936 uint64_t to_uint64(bool *ok = nullptr) const;
937
938 private:
940
941
949 RTTR_INLINE void* get_ptr() const;
950
958 RTTR_INLINE type get_raw_type() const;
959
968 RTTR_INLINE void* get_raw_ptr() const;
969
971 template<typename T>
972 detail::enable_if_t<std::is_arithmetic<T>::value, T> convert_impl(bool* ok = nullptr) const;
973
974 template<typename T>
975 detail::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, T> convert_impl(bool* ok = nullptr) const;
976
977 template<typename T>
978 detail::enable_if_t<std::is_enum<T>::value, T> convert_impl(bool* ok = nullptr) const;
979
987 RTTR_INLINE detail::data_address_container get_data_address_container() const;
988
989 bool convert(const type& target_type, variant& var) const;
990
996 template<typename T>
997 bool try_basic_type_conversion(T& to) const;
998
1004 template<typename T>
1005 typename std::enable_if<detail::pointer_count<T>::value == 1, bool>::type
1006 try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
1007
1013 template<typename T>
1014 typename std::enable_if<detail::pointer_count<T>::value != 1, bool>::type
1015 try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
1016
1024 bool compare_equal(const variant& other, bool& ok) const;
1025
1033 bool compare_less(const variant& other, bool& ok) const;
1034
1040 RTTR_INLINE bool is_nullptr() const;
1041
1042
1043 variant create_wrapped_value(const type& wrapped_type) const;
1044
1045 private:
1046 friend class argument;
1047 friend class instance;
1048
1049 template<typename T, typename Tp, typename Converter>
1050 friend struct detail::variant_data_base_policy;
1051 friend struct detail::variant_data_policy_nullptr_t;
1052 friend RTTR_API bool detail::variant_compare_less(const variant&, const type&, const variant&, const type&, bool& ok);
1053 template<class T>
1054 friend RTTR_INLINE T* detail::unsafe_variant_cast(variant* operand) RTTR_NOEXCEPT;
1055
1056
1057 detail::variant_data m_data;
1058 detail::variant_policy_func m_policy;
1059};
1060
1062
1077template<class T>
1078T variant_cast(const variant& operand);
1079
1094template<class T>
1096
1113template<class T>
1115
1132template<class T>
1133const T* variant_cast(const variant* operand) RTTR_NOEXCEPT;
1134
1151template<class T>
1152T* variant_cast(variant* operand) RTTR_NOEXCEPT;
1153
1154} // end namespace rttr
1155
1156#include "rttr/detail/variant/variant_impl.h"
1157
1158#endif // RTTR_VARIANT_H_
The argument class is used for forwarding arguments to properties or methods.
Definition argument.h:52
The instance class is used for forwarding the instance of an object to invoke a property or method.
Definition instance.h:48
The type class holds the type information for any arbitrary object.
Definition type.h:171
The variant_associative_view describes a class that refers to an associative container (e....
Definition variant_associative_view.h:93
The variant_sequential_view describes a class that refers to an sequence container (e....
Definition variant_sequential_view.h:96
The variant class allows to store data of any type and convert between these types transparently.
Definition variant.h:198
bool operator>(const variant &other) const
Compares this variant with other and returns true if this is greater than other, otherwise returns fa...
bool is_associative_container() const
Returns true, when for the underlying or the wrapped type an associative_mapper exists.
variant_sequential_view create_sequential_view() const
Creates a variant_sequential_view from the containing value, when the type or its raw type or the wra...
bool operator<(const variant &other) const
Compares this variant with other and returns true if this is less than other, otherwise returns false...
int32_t to_int32(bool *ok=nullptr) const
Returns the containing variant as an int32_t when the type is an int32_t.
variant()
Constructs an invalid variant.
std::string to_string(bool *ok=nullptr) const
Returns the containing variant as a std::string when the type is a std::string.
double to_double(bool *ok=nullptr) const
Returns the containing variant as a double when the type is a double.
~variant()
Destroys the variant and the contained object.
variant(const variant &other)
Constructs a new variant object from the given variant other.
bool operator==(const variant &other) const
Compares this variant with other and returns true if they are equal; otherwise returns false.
bool operator!=(const variant &other) const
Compares this variant with other and returns true if they are not equal; otherwise returns false.
bool convert(const type &target_type)
Converts the containing variant internally to the given type target_type.
float to_float(bool *ok=nullptr) const
Returns the containing variant as a float when the type is a float.
friend class instance
Definition variant.h:1047
T & get_value()
Returns a reference to the containing value as type T.
bool convert(T &value) const
Converts the containing data to the given value value and returns a bool flag that indicated whether ...
variant & operator=(T &&other)
Assigns the value of the other object to this variant.
const T & get_wrapped_value() const
Returns a reference to the contained wrapped value as type T.
variant & operator=(const variant &other)
Assigns the value of the other variant to this variant.
variant & operator=(variant &&other)
Assigns the value of the other variant to this variant.
variant_associative_view create_associative_view() const
Creates a variant_associative_view from the containing value, when the type or its raw type or the wr...
bool operator>=(const variant &other) const
Compares this variant with other and returns true if this is greater or equal then other,...
type get_type() const
Returns the type object of underlying data.
int64_t to_int64(bool *ok=nullptr) const
Returns the containing variant as an int64_t when the type is an int64_t.
bool is_type() const
Returns true if the containing variant data is of the given template type T.
variant(T &&val)
Constructs a new variant with the new value val.
void clear()
When the variant contains a value, then this function will clear the content.
uint64_t to_uint64(bool *ok=nullptr) const
Returns the containing variant as an uint64_t when the type is an uint64_t.
uint16_t to_uint16(bool *ok=nullptr) const
Returns the containing variant as an uint16_t when the type is an uint16_t.
uint32_t to_uint32(bool *ok=nullptr) const
Returns the containing variant as an uint32_t when the type is an uint32_t.
int8_t to_int8(bool *ok=nullptr) const
Returns the containing variant as an int8_t when the type is an int8_t.
int16_t to_int16(bool *ok=nullptr) const
Returns the containing variant as an int16_t when the type is an int16_t.
T convert(bool *ok=nullptr) const
Converts the containing data to a new value of type T and return this value.
uint8_t to_uint8(bool *ok=nullptr) const
Returns the containing variant as an uint8_t when the type is an uint8_t.
bool to_bool() const
Returns the variant as a bool if this variant is of type bool.
bool can_convert(const type &target_type) const
Returns true if the contained value can be converted to the given type target_type; otherwise false.
const T & get_value() const
Returns a reference to the containing value as type T.
bool can_convert() const
Returns true if the contained value can be converted to the given type T.
variant extract_wrapped_value() const
Extracts the wrapped value and copies its content into a new variant.
friend class argument
Definition variant.h:1046
int to_int(bool *ok=nullptr) const
Returns the containing variant as an int when the type is an integer.
bool is_valid() const
Returns true if this variant is valid, that means the variant is holding some data.
bool is_sequential_container() const
Returns true, when for the underlying or the wrapped type an sequential_mapper exists.
void swap(variant &other)
Swaps the content of this variant with other variant.
bool operator<=(const variant &other) const
Compares this variant with other and returns true if this is less or equal than other,...
variant(variant &&other)
Constructs a new variant via move constructor.
Definition access_levels.h:34
T variant_cast(const variant &operand)
Returns a reference to the containing value as type T.
detail::enum_data< Enum_Type > value(string_view, Enum_Type value)
The value function should be used to add a mapping from enum name to value during the registration pr...