rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
property_container_member_object.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_PROPERTY_CONTAINER_MEMBER_OBJECT_H__
29 #define __RTTR_PROPERTY_CONTAINER_MEMBER_OBJECT_H__
30 
33 // pointer to member - read write
34 
35 template<typename C, typename A>
36 class property_container<member_object_ptr, A(C::*), void, return_as_copy, set_value> : public property_container_base
37 {
38  typedef A (C::*accessor);
39  public:
40  property_container(const std::string& name, const type declaring_type, accessor acc)
41  : property_container_base(name, declaring_type), _acc(acc)
42  {
43  }
44 
45  bool is_readonly() const { return false; }
46  bool is_static() const { return false; }
47  type get_type() const { return type::get<A>(); }
48  bool is_array() const { return detail::is_array<A>::value; }
49 
50  bool set_value(detail::instance& object, detail::argument& arg) const
51  {
52  C* ptr = object.try_convert<C>();
53  if (ptr && arg.is_type<A>())
54  return property_accessor<A>::set_value((ptr->*_acc), arg);
55  else
56  return false;
57  }
58 
59  variant get_value(detail::instance& object) const
60  {
61  if (C* ptr = object.try_convert<C>())
62  return variant((ptr->*_acc));
63  else
64  return variant();
65  }
66 
67  private:
68  accessor _acc;
69 };
70 
71 
74 // pointer to member - read only (because of std::false_type)
75 
76 template<typename C, typename A>
77 class property_container<member_object_ptr, A(C::*), void, return_as_copy, read_only> : public property_container_base
78 {
79  typedef A (C::*accessor);
80  public:
81  property_container(const std::string& name, const type declaring_type, accessor acc)
82  : property_container_base(name, declaring_type),
83  _acc(acc)
84  {
85  }
86 
87  bool is_readonly() const { return true; }
88  bool is_static() const { return false; }
89  type get_type() const { return type::get<A>(); }
90  bool is_array() const { return detail::is_array<A>::value; }
91 
92  bool set_value(detail::instance& object, detail::argument& arg) const
93  {
94  return false;
95  }
96 
97  variant get_value(detail::instance& object) const
98  {
99  if (C* ptr = object.try_convert<C>())
100  return variant((ptr->*_acc));
101  else
102  return variant();
103  }
104 
105  private:
106  accessor _acc;
107 };
108 
111 // pointer to member - read write
112 
113 template<typename C, typename A>
114 class property_container<member_object_ptr, A(C::*), void, return_as_ptr, set_as_ptr> : public property_container_base
115 {
116  typedef A (C::*accessor);
117  public:
118  property_container(const std::string& name, const type declaring_type, accessor acc)
119  : property_container_base(name, declaring_type), _acc(acc)
120  {
121  static_assert(!std::is_pointer<A>::value, "The given type is already a pointer type!");
122  }
123 
124  bool is_readonly() const { return false; }
125  bool is_static() const { return false; }
126  type get_type() const { return type::get<A*>(); }
127  bool is_array() const { return detail::is_array<A>::value; }
128 
129  bool set_value(detail::instance& object, detail::argument& arg) const
130  {
131  C* ptr = object.try_convert<C>();
132  if (ptr && arg.is_type<A*>())
133  {
134  return property_accessor<A*>::set_value(&(ptr->*_acc), arg);
135  }
136  else
137  {
138  return false;
139  }
140  }
141 
142  variant get_value(detail::instance& object) const
143  {
144  if (C* ptr = object.try_convert<C>())
145  return variant(&(ptr->*_acc));
146  else
147  return variant();
148  }
149 
150  private:
151  accessor _acc;
152 };
153 
156 // pointer to member - read only
157 
158 template<typename C, typename A>
159 class property_container<member_object_ptr, A(C::*), void, return_as_ptr, read_only> : public property_container_base
160 {
161  typedef A (C::*accessor);
162  public:
163  property_container(const std::string& name, const type declaring_type, accessor acc)
164  : property_container_base(name, declaring_type),
165  _acc(acc)
166  {
167  static_assert(!std::is_pointer<A>::value, "The given type is already a pointer type!");
168  }
169 
170  bool is_readonly() const { return true; }
171  bool is_static() const { return false; }
172  type get_type() const { return type::get<typename std::add_const<A>::type*>(); }
173  bool is_array() const { return detail::is_array<A>::value; }
174 
175  bool set_value(detail::instance& object, detail::argument& arg) const
176  {
177  return false;
178  }
179 
180  variant get_value(detail::instance& object) const
181  {
182  if (C* ptr = object.try_convert<C>())
183  return variant(const_cast<const A*>(&(ptr->*_acc)));
184  else
185  return variant();
186  }
187 
188  private:
189  accessor _acc;
190 };
191 
192 #endif // __RTTR_PROPERTY_CONTAINER_MEMBER_OBJECT_H__