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#include <string>
42
43namespace rttr
44{
45
46class variant_associative_view;
47class variant_sequential_view;
48class type;
49class variant;
50class argument;
51class instance;
52
53namespace detail
54{
55 template<class T>
56 RTTR_INLINE T* unsafe_variant_cast(variant* operand) RTTR_NOEXCEPT;
57 template<class T>
58 RTTR_INLINE const T* unsafe_variant_cast(const variant* operand) RTTR_NOEXCEPT;
59
60 struct data_address_container;
61 template<typename T>
62 struct empty_type_converter;
63
64 template<typename T, typename Tp, typename Converter = empty_type_converter<T>>
65 struct variant_data_base_policy;
66 struct variant_data_policy_nullptr_t;
67
68 enum class variant_policy_operation : uint8_t;
69
70 template<typename T, typename Decayed = decay_except_array_t<T>>
71 using decay_variant_t = enable_if_t<!std::is_same<Decayed, variant>::value, Decayed>;
72
73 using variant_policy_func = bool (*)(variant_policy_operation, const variant_data&, argument_wrapper);
74}
75
199{
200 public:
207
212 template<typename T, typename Tp = detail::decay_variant_t<T>>
214
219
224
229
235 template<typename T, typename Tp = detail::decay_variant_t<T>>
237
244
251
269
284
301 RTTR_INLINE bool operator<(const variant& other) const;
302
303
318
333
346 RTTR_INLINE bool operator>(const variant& other) const;
347
353 void clear();
354
359
365 template<typename T>
366 bool is_type() const;
367
375 type get_type() const;
376
388 bool is_valid() const;
389
397 explicit operator bool() const;
398
406
414
438 template<typename T>
440
464 template<typename T>
465 const T& get_value() const;
466
486 template<typename T>
487 const T& get_wrapped_value() const;
488
513
522 template<typename T>
523 bool can_convert() const;
524
538 bool can_convert(const type& target_type) const;
539
570
597 template<typename T>
598 T convert(bool* ok = nullptr) const;
599
628 template<typename T>
629 bool convert(T& value) const;
630
663
694
709 bool to_bool() const;
710
728 int to_int(bool *ok = nullptr) const;
729
747 float to_float(bool* ok = nullptr) const;
748
766 double to_double(bool* ok = nullptr) const;
767
781 std::string to_string(bool *ok = nullptr) const;
782
800 int8_t to_int8(bool *ok = nullptr) const;
801
819 int16_t to_int16(bool *ok = nullptr) const;
820
838 int32_t to_int32(bool *ok = nullptr) const;
839
857 int64_t to_int64(bool *ok = nullptr) const;
858
877 uint8_t to_uint8(bool *ok = nullptr) const;
878
897 uint16_t to_uint16(bool *ok = nullptr) const;
898
917 uint32_t to_uint32(bool *ok = nullptr) const;
918
937 uint64_t to_uint64(bool *ok = nullptr) const;
938
939 private:
941
942
950 RTTR_INLINE void* get_ptr() const;
951
959 RTTR_INLINE type get_raw_type() const;
960
969 RTTR_INLINE void* get_raw_ptr() const;
970
972 template<typename T>
973 detail::enable_if_t<std::is_arithmetic<T>::value, T> convert_impl(bool* ok = nullptr) const;
974
975 template<typename T>
976 detail::enable_if_t<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, T> convert_impl(bool* ok = nullptr) const;
977
978 template<typename T>
979 detail::enable_if_t<std::is_enum<T>::value, T> convert_impl(bool* ok = nullptr) const;
980
988 RTTR_INLINE detail::data_address_container get_data_address_container() const;
989
990 bool convert(const type& target_type, variant& var) const;
991
997 template<typename T>
998 bool try_basic_type_conversion(T& to) const;
999
1005 template<typename T>
1006 typename std::enable_if<detail::pointer_count<T>::value == 1, bool>::type
1007 try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
1008
1014 template<typename T>
1015 typename std::enable_if<detail::pointer_count<T>::value != 1, bool>::type
1016 try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
1017
1025 bool compare_equal(const variant& other, bool& ok) const;
1026
1034 bool compare_less(const variant& other, bool& ok) const;
1035
1041 RTTR_INLINE bool is_nullptr() const;
1042
1043
1044 variant create_wrapped_value(const type& wrapped_type) const;
1045
1046 private:
1047 friend class argument;
1048 friend class instance;
1049
1050 template<typename T, typename Tp, typename Converter>
1051 friend struct detail::variant_data_base_policy;
1052 friend struct detail::variant_data_policy_nullptr_t;
1053 friend RTTR_API bool detail::variant_compare_less(const variant&, const type&, const variant&, const type&, bool& ok);
1054 template<class T>
1055 friend RTTR_INLINE T* detail::unsafe_variant_cast(variant* operand) RTTR_NOEXCEPT;
1056
1057
1058 detail::variant_data m_data;
1060};
1061
1063
1078template<class T>
1080
1095template<class T>
1097
1114template<class T>
1116
1133template<class T>
1135
1152template<class T>
1154
1155} // end namespace rttr
1156
1157#include "rttr/detail/variant/variant_impl.h"
1158
1159#endif // RTTR_VARIANT_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
array_range()
Default constructor.
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:178
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:199
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.
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.
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...