i6engine  1.0
AutoUpdater.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine 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 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_AUTOUPDATER_H__
26 #define __I6ENGINE_UTILS_AUTOUPDATER_H__
27 
28 #include <functional>
29 #include <string>
30 #include <vector>
31 
32 #include "i6eSystemParameters.h"
33 
34 namespace i6e {
35 namespace utils {
36 
37  template<typename T>
38  class AutoUpdater {
39  public:
43  AutoUpdater() : _value(T()), _functions() {
44  }
45 
50  AutoUpdater(const T & value) : _value(value), _functions() {
51  }
52 
57  void registerUpdate(const std::function<void(T)> & func) {
58  _functions.push_back(func);
59  }
60 
65  T get() const {
66  return _value;
67  }
68 
73  void set(const T & val) {
74  _value = val;
75  for (std::function<void(T)> & method : _functions) {
76  method(_value);
77  }
78  }
79 
83  T & operator=(const T & val) {
84  set(val);
85  return _value;
86  }
87 
91  AutoUpdater<T> & operator+=(const T & val) {
92  set(_value + val);
93  return *this;
94  }
95 
99  AutoUpdater<T> & operator-=(const T & val) {
100  set(_value - val);
101  return *this;
102  }
103 
107  AutoUpdater<T> & operator*=(const T & val) {
108  set(_value * val);
109  return *this;
110  }
111 
115  AutoUpdater<T> & operator/=(const T & val) {
116  set(_value / val);
117  return *this;
118  }
119 
123  operator T() const {
124  return _value;
125  }
126 
131  *this += 1;
132  return _value;
133  }
134 
135  T operator++(int) {
136  T tmp = _value;
137  *this += 1;
138  return tmp;
139  }
140 
141  private:
145  T _value;
146 
150  std::vector<std::function<void(T)>> _functions;
151  };
152 
153 } /* namespace utils */
154 } /* namespace i6e */
155 
156 #endif /* __I6ENGINE_UTILS_AUTOUPDATER_H__ */
157 
AutoUpdater< T > & operator/=(const T &val)
/= operator
Definition: AutoUpdater.h:115
AutoUpdater(const T &value)
Constructor for a new AutoUpdate variable.
Definition: AutoUpdater.h:50
AutoUpdater< T > & operator-=(const T &val)
-= operator
Definition: AutoUpdater.h:99
AutoUpdater< T > & operator+=(const T &val)
+= operator
Definition: AutoUpdater.h:91
void set(const T &val)
Setter for the value.
Definition: AutoUpdater.h:73
AutoUpdater< T > & operator*=(const T &val)
*= operator
Definition: AutoUpdater.h:107
AutoUpdater()
default constructor
Definition: AutoUpdater.h:43
void registerUpdate(const std::function< void(T)> &func)
Registers a callback for autoUpdate being called on every change of the value.
Definition: AutoUpdater.h:57
T & operator=(const T &val)
assignment operator
Definition: AutoUpdater.h:83