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