hx3d  1
2D/3D Simple Game Framework
property.inl.hpp
1 /*
2  Property system.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library 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 GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 namespace hx3d {
22 
23 template <class T>
24 CallbackProperty<T>::CallbackProperty(const T& value, std::function<void(const T&, const T&)> func):
25  _value(value), _func(func) {}
26 
27 template <class T>
29  return _value;
30 }
31 
32 template <class T>
33 T& CallbackProperty<T>::operator=(const T& value) {
34  _func(_value, value);
35 
36  _value = value;
37  return _value;
38 }
39 
41 
42 template <class T>
43 Property<T>::Property(const T& value):
44  _value(value) {}
45 
46 template <class T>
48  _observers.push_back(observer);
49 }
50 
51 template <class T>
52 Property<T>::operator T const&() const {
53  return _value;
54 }
55 
56 template <class T>
57 T& Property<T>::operator=(const T& value) {
58  for (auto obs: _observers) {
59  obs->observe(_value, value);
60  }
61 
62  _value = value;
63  return _value;
64 }
65 
66 } /* hx3d */
Callback-on-change property.
Definition: property.hpp:48
Property(const T &value)
Create an observable property with value.
T & operator=(const T &value)
Set the value and start the callback.
T & operator=(const T &value)
Set the value and call the observers.
hx3d framework namespace
Definition: audio.hpp:26
Observable property.
Definition: property.hpp:83
void addObserver(PropertyObserver< T > *observer)
Add an observer.
Property observer (Observer pattern)
Definition: property.hpp:33
CallbackProperty(const T &value, std::function< void(const T &, const T &)> func)
Create a callback property with value and function.