Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
property.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include <type_traits>
22 #include <functional>
23 #include <memory>
24 #include <cstdint>
25 
26 namespace smlt {
27 
28 namespace property_impl {
29 
30 template<typename MP, typename C, typename T>
31 struct Getter;
32 
33 template<typename MP, typename C, typename T>
34 struct Getter<MP, C, std::weak_ptr<T>> {
35  MP mp_;
36 
37  Getter(MP mp):
38  mp_(mp) {
39 
40  }
41 
42  T* get(C* _this) const {
43  if(auto sptr = (_this->*mp_).lock()) {
44  return sptr.get();
45  } else {
46  return nullptr;
47  }
48  }
49 };
50 
51 template<typename MP, typename C, typename T>
52 struct Getter<MP, C, std::shared_ptr<T>> {
53  MP mp_;
54 
55  Getter(MP mp):
56  mp_(mp) {
57 
58  }
59 
60  T* get(C* _this) const {
61  return (_this->*mp_).get();
62  }
63 };
64 
65 template<typename MP, typename C, typename T>
66 struct Getter<MP, C, std::unique_ptr<T>> {
67  MP mp_;
68 
69  Getter(MP mp):
70  mp_(mp) {
71 
72  }
73 
74  T* get(C* _this) const {
75  return (_this->*mp_).get();
76  }
77 };
78 
79 template<typename MP, typename C, typename T>
80 struct Getter<MP, C, T*> {
81  static_assert(!std::is_function<MP>::value, "MP was a function");
82 
83  MP mp_;
84 
85  Getter(MP mp):
86  mp_(mp) {
87 
88  }
89 
90  T* get(C* _this) const {
91  return (_this->*mp_);
92  }
93 };
94 
95 
96 template<typename MP, typename C, typename T>
97 struct Getter<MP, C, T* () const> {
98  MP mp_;
99 
100  Getter(MP mp):
101  mp_(mp) {
102 
103  }
104 
105  T* get(C* _this) const {
106  return (_this->*mp_)();
107  }
108 };
109 
110 template<typename MP, typename C, typename T>
111 struct Getter {
112  MP mp_;
113 
114  static_assert(!std::is_pointer<T>::value, "T was a pointer");
115  static_assert(!std::is_function<T>::value, "T was a function");
116 
117  Getter(MP mp):
118  mp_(mp) {
119 
120  }
121 
122  T* get(C* _this) const {
123  return &(_this->*mp_);
124  }
125 };
126 
127 }
128 
129 template<typename T>
130 struct ExtractType {
131  static_assert(!std::is_function<T>::value, "T was a function");
132 
133  typedef T type;
134 };
135 
136 template<typename T>
137 struct ExtractType<T*> {
138  typedef typename std::remove_pointer<T>::type type;
139 };
140 
141 template<typename T>
142 struct ExtractType<std::weak_ptr<T>> {
143  typedef T type;
144 };
145 
146 template<typename T>
147 struct ExtractType<std::shared_ptr<T>> {
148  typedef T type;
149 };
150 
151 template<typename T>
152 struct ExtractType<std::unique_ptr<T>> {
153  typedef T type;
154 };
155 
156 template<typename T>
157 struct ExtractType<T() const> {
158  typedef typename std::remove_pointer<T>::type type;
159 };
160 
161 
162 template<typename MP>
164 
165 
166 template<typename T, typename U>
167 struct PointerToMemberHelper<T U::*> {
168  typedef U class_type; /* Class */
169  typedef T variable_type; /* The source type */
170 
171  /* This is the underlying type, which needs extracting */
172  typedef typename ExtractType<
173  T
174  >::type result_type;
175 };
176 
177 template<typename MP>
178 struct PointerToMember : PointerToMemberHelper<typename std::remove_cv<MP>::type> {};
179 
180 template<typename MP>
181 class Property {
182 public:
183  typedef typename PointerToMember<MP>::class_type class_type;
184  typedef typename PointerToMember<MP>::variable_type variable_type;
185  typedef typename PointerToMember<MP>::result_type T;
186 
187  Property(class_type* _this, MP member):
188  this_(_this),
189  getter_(member) {}
190 
191  Property() = delete;
192  Property(const Property&) = delete;
193  Property& operator=(const Property& rhs) {
194  assert(this_); /* We don't copy this */
195  getter_ = rhs.getter_;
196  }
197 
198 private:
199  class_type* this_;
201 
202 public:
203 
204  inline operator T&() const {
205  return *get();
206  }
207 
208  inline T* operator->() const {
209  return get();
210  }
211 
212  inline operator T*() {
213  return get();
214  }
215 
216  T* get() const {
217  return getter_.get(this_);
218  }
219 
220  operator bool() const { return bool(get(this));}
221 };
222 
223 }
224 
225 #define S_DEFINE_PROPERTY(name, member) \
226  Property<decltype(member)> name = {this, member}
smlt::Property
Definition: property.h:181
smlt::PointerToMemberHelper
Definition: property.h:163
smlt::PointerToMember
Definition: property.h:178
smlt
Definition: animation.cpp:25
smlt::property_impl::Getter
Definition: property.h:111
smlt::ExtractType
Definition: property.h:130
std
Extensions to the C++ standard library.
Definition: variant.hpp:2752