rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
argument.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_ARGUMENT_H__
29 #define __RTTR_ARGUMENT_H__
30 
33 #include "rttr/type.h"
34 #include "rttr/variant.h"
35 #include "rttr/variant_array.h"
36 
37 #include <type_traits>
38 #include <utility>
39 
40 namespace rttr
41 {
42 namespace detail
43 {
44 class instance;
45 
51 class argument
52 {
53 public:
54  argument() : _data(nullptr), _type(impl::get_invalid_type()) {}
55  argument(const argument& other) : _data(other._data), _type(other._type) {}
56  argument(variant& var) : _data(var.get_ptr()), _type(var.get_type()) {}
57  argument(const variant& var) : _data(var.get_ptr()), _type(var.get_type()) {}
58  argument(variant_array& var) : _data(var.get_ptr()), _type(var.get_type()) {}
59  argument(const variant_array& var) : _data(var.get_ptr()), _type(var.get_type()) {}
60 
61  template<typename T>
62  argument(const T& data, typename std::enable_if<!std::is_same<argument, T>::value >::type* = 0)
63  : _data(reinterpret_cast<const void*>(std::addressof(data))),
64  _type(rttr::type::get<T>())
65  {
66  static_assert(!std::is_same<instance, T>::value, "Don't use the argument class for forwarding an instance!");
67  }
68 
69  template<typename T>
70  argument(T& data, typename std::enable_if<!std::is_same<argument, T>::value >::type* = 0)
71  : _data(reinterpret_cast<const void*>(std::addressof(data))),
72  _type(rttr::type::get<T>())
73  {
74  static_assert(!std::is_same<instance, T>::value, "Don't use the argument class for forwarding an instance!");
75  }
76 
77  template<typename T>
78  bool is_type() const { return rttr::type::get<T>() == _type; }
79  type get_type() const { return _type; }
80  void* get_ptr() const { return const_cast<void *>(_data); }
81 
82  template<typename T>
83  T& get_value() const
84  {
85  using raw_type = typename std::remove_reference<T>::type;
86  return (*reinterpret_cast<raw_type*>(const_cast<void *>(_data)));
87  }
88 
89  argument& operator=(const argument& other)
90  {
91  _data = other._data;
92  const_cast<rttr::type&>(_type) = other._type;
93  return *this;
94  }
95 
96 private:
97  const void* _data;
98  const rttr::type _type;
99 };
100 
101 } // end namespace detail
102 } // end namespace rttr
103 
104 #endif // __RTTR_ARGUMENT_H__
This class holds the type information for any arbitrary object.
Definition: type.h:165