rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
instance.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014 Axel Menzel <info@axelmenzel.de> *
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_INSTANCE_H__
29 #define __RTTR_INSTANCE_H__
30 
33 #include "rttr/type.h"
34 #include "rttr/variant.h"
35 
36 namespace rttr
37 {
38 namespace detail
39 {
40 
41 class argument;
47 class instance
48 {
49 public:
50  instance() : _data(nullptr), _type(impl::get_invalid_type()) {}
51 
52  instance(variant& var)
53  : _data(var.get_raw_ptr()),
54  _type(var.get_raw_type())
55  {
56  }
57 
58  instance(const instance& other)
59  : _data(other._data),
60  _type(other._type)
61  {
62  }
63 
64  instance(instance&& other)
65  : _data(other._data),
66  _type(other._type)
67  {
68  }
69 
70  template<typename T>
71  instance(const T& data, typename std::enable_if<!std::is_same<instance, T>::value >::type* = 0)
72  : _data(detail::get_void_ptr(data)),
73  _type(rttr::type::get<typename raw_type<T>::type>())
74  {
75  static_assert(!std::is_same<argument, T>::value, "Don't use the instance class for forwarding an argument!");
76  }
77 
78  template<typename T>
79  instance(T& data, typename std::enable_if<!std::is_same<instance, T>::value >::type* = 0)
80  : _data(detail::get_void_ptr(data)),
81  _type(rttr::type::get<typename raw_type<T>::type>())
82  {
83  static_assert(!std::is_same<argument, T>::value, "Don't use the instance class for forwarding an argument!");
84  }
85 
86  template<typename TargetType>
87  TargetType* try_convert() const
88  {
89  return (static_cast<TargetType*>(type::apply_offset(const_cast<instance*>(this)->_data, _type, type::get<TargetType>())));
90  }
91 
92  bool is_valid() const { return (_data != nullptr); }
93  operator bool() const { return (_data != nullptr); }
94 
95  type get_type() const { return _type; }
96 
97 private:
98  instance& operator=(const instance& other);
99 
100 private:
101  void* _data;
102  const rttr::type _type;
103 };
104 
105 } // end namespace detail
106 
113 RTTR_INLINE static detail::instance empty_instance() { return detail::instance(); }
114 } // end namespace rttr
115 
116 #endif // __RTTR_INSTANCE_H__
This class holds the type information for any arbitrary object.
Definition: type.h:165
#define RTTR_INLINE
Definition: core_prerequisites.h:90