variant.h Source File - 0.9.7 |RTTR
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 
42 namespace rttr
43 {
44 
45 class variant_associative_view;
46 class variant_sequential_view;
47 class type;
48 class variant;
49 class argument;
50 class instance;
51 
52 namespace 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 
197 class 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 
242  variant& operator=(variant&& other);
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 
404  bool is_associative_container() const;
405 
412  bool is_sequential_container() const;
413 
437  template<typename T>
438  T& get_value();
439 
463  template<typename T>
464  const T& get_value() const;
465 
485  template<typename T>
486  const T& get_wrapped_value() const;
487 
511  variant extract_wrapped_value() const;
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 
661  variant_associative_view create_associative_view() const;
662 
692  variant_sequential_view create_sequential_view() const;
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>
1006  try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
1007 
1013  template<typename T>
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 
1077 template<class T>
1078 T variant_cast(const variant& operand);
1079 
1094 template<class T>
1095 T variant_cast(variant& operand);
1096 
1113 template<class T>
1114 T variant_cast(variant&& operand);
1115 
1132 template<class T>
1133 const T* variant_cast(const variant* operand) RTTR_NOEXCEPT;
1134 
1151 template<class T>
1152 T* 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_
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
The type class holds the type information for any arbitrary object.
Definition: type.h:177
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.
The variant_associative_view describes a class that refers to an associative container (e...
Definition: variant_associative_view.h:92
The instance class is used for forwarding the instance of an object to invoke a property or method...
Definition: instance.h:47
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...
The variant_sequential_view describes a class that refers to an sequence container (e...
Definition: variant_sequential_view.h:95
constexpr bool operator!=(basic_string_view< CharT, Traits > lhs, basic_string_view< CharT, Traits > rhs) noexcept
Compares the two views lhs and rhs.
The argument class is used for forwarding arguments to properties or methods.
Definition: argument.h:51
The variant class allows to store data of any type and convert between these types transparently...
Definition: variant.h:197
T variant_cast(const variant &operand)
Returns a reference to the containing value as type T.
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.