variant.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014, 2015 - 2016 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_less.h"
36 
37 #include <type_traits>
38 #include <cstddef>
39 #include <cstdint>
40 #include <algorithm>
41 
42 namespace rttr
43 {
44 
45 class variant_array_view;
46 class type;
47 class variant;
48 class argument;
49 class instance;
50 
51 namespace detail
52 {
53  struct data_address_container;
54  template<typename T>
55  struct empty_type_converter;
56 
57  template<typename T, typename Tp, typename Converter = empty_type_converter<T>>
58  struct variant_data_base_policy;
59  struct variant_data_policy_nullptr_t;
60 
61  enum class variant_policy_operation : uint8_t;
62 
63  template<typename T, typename Decayed = decay_except_array_t<T>>
64  using decay_variant_t = enable_if_t<!std::is_same<Decayed, variant>::value &&
65  !std::is_same<Decayed, variant_array_view>::value, Decayed>;
66 
67  using variant_policy_func = bool (*)(variant_policy_operation, const variant_data&, argument_wrapper);
68 }
69 
192 class RTTR_API variant
193 {
194  public:
200  RTTR_INLINE variant();
201 
206  template<typename T, typename Tp = detail::decay_variant_t<T>>
207  variant(T&& val);
208 
212  variant(const variant& other);
213 
217  variant(variant&& other);
218 
222  RTTR_INLINE ~variant();
223 
229  template<typename T, typename Tp = detail::decay_variant_t<T>>
230  variant& operator=(T&& other);
231 
237  variant& operator=(variant&& other);
238 
244  variant& operator=(const variant& other);
245 
260  RTTR_INLINE bool operator==(const variant& other) const;
261 
273  RTTR_INLINE bool operator!=(const variant& other) const;
274 
289  RTTR_INLINE bool operator<(const variant& other) const;
290 
296  void clear();
297 
301  void swap(variant& other);
302 
308  template<typename T>
309  bool is_type() const;
310 
318  type get_type() const;
319 
331  bool is_valid() const;
332 
340  explicit operator bool() const;
341 
349  bool is_array() const;
350 
374  template<typename T>
375  const T& get_value() const;
376 
383  template<typename T>
384  bool can_convert() const;
385 
397  bool can_convert(const type& target_type) const;
398 
421  bool convert(const type& target_type);
422 
446  template<typename T>
447  T convert(bool* ok = nullptr) const;
448 
474  template<typename T>
475  bool convert(T& value) const;
476 
500  variant_array_view create_array_view() const;
501 
516  bool to_bool() const;
517 
535  int to_int(bool *ok = nullptr) const;
536 
554  float to_float(bool* ok = nullptr) const;
555 
573  double to_double(bool* ok = nullptr) const;
574 
588  std::string to_string(bool *ok = nullptr) const;
589 
607  int8_t to_int8(bool *ok = nullptr) const;
608 
626  int16_t to_int16(bool *ok = nullptr) const;
627 
645  int32_t to_int32(bool *ok = nullptr) const;
646 
664  int64_t to_int64(bool *ok = nullptr) const;
665 
684  uint8_t to_uint8(bool *ok = nullptr) const;
685 
704  uint16_t to_uint16(bool *ok = nullptr) const;
705 
724  uint32_t to_uint32(bool *ok = nullptr) const;
725 
744  uint64_t to_uint64(bool *ok = nullptr) const;
745 
746  private:
748 
749 
757  RTTR_INLINE void* get_ptr() const;
758 
766  RTTR_INLINE type get_raw_type() const;
767 
776  RTTR_INLINE void* get_raw_ptr() const;
777 
779  template<typename T>
780  detail::enable_if_t<std::is_arithmetic<T>::value, T> convert_impl(bool* ok = nullptr) const;
781 
782  template<typename T>
783  detail::enable_if_t<!std::is_arithmetic<T>::value, T> convert_impl(bool* ok = nullptr) const;
784 
792  RTTR_INLINE detail::data_address_container get_data_address_container() const;
793 
794  bool convert(const type& target_type, variant& var) const;
795 
801  template<typename T>
802  bool try_basic_type_conversion(T& to) const;
803 
809  template<typename T>
811  try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
812 
818  template<typename T>
820  try_pointer_conversion(T& to, const type& source_type, const type& target_type) const;
821 
827  bool compare_equal(const variant& other) const;
828 
834  bool compare_less(const variant& other) const;
835 
841  RTTR_INLINE bool is_nullptr() const;
842 
843  private:
844  friend class argument;
845  friend class instance;
846 
847  template<typename T, typename Tp, typename Converter>
848  friend struct detail::variant_data_base_policy;
849  friend struct detail::variant_data_policy_nullptr_t;
850  friend RTTR_API bool detail::variant_compare_less(const variant&, const type&, const variant&, const type&);
851 
852  detail::variant_data m_data;
853  detail::variant_policy_func m_policy;
854 };
855 
857 
858 } // end namespace rttr
859 
860 #include "rttr/detail/variant/variant_impl.h"
861 
862 #endif // RTTR_VARIANT_H_
Definition: access_levels.h:33
The type class holds the type information for any arbitrary object.
Definition: type.h:153
The instance class is used for forwarding the instance of an object to invoke a property or method...
Definition: instance.h:46
detail::enum_data< Enum_Type > value(const char *name, Enum_Type value)
The value function should be used to add a mapping from enum name to value during the registration pr...
The variant_array_view describes a class that refers to an array inside a variant.
Definition: variant_array_view.h:137
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:192