OpenWareLaboratory
TakeoverValue.h
Go to the documentation of this file.
1 #ifndef __TakeoverValue_h__
2 #define __TakeoverValue_h__
3 
4 #include "SimpleValue.h"
5 
18 template<typename T, typename Value = SimpleValue<T>>
19 class TakeoverValue : public Value {
20 protected:
21  bool locked = false;
22  T threshold = Value::DEFAULT_DELTA;
23 public:
25  TakeoverValue(T value): Value(value) {}
26  TakeoverValue(const TakeoverValue<T>& other): Value(other), locked(other.locked), threshold(other.threshold) {}
27  TakeoverValue(T threshold, T init): Value(init), threshold(threshold) {}
28  void setLocked(bool value){
29  locked = value;
30  }
31  bool isLocked(){
32  return locked;
33  }
34  void update(T x){
35  if(locked && abs(Value::get() - x) <= threshold)
36  locked = false;
37  if(!locked)
38  Value::update(x);
39  }
40  operator T(){
41  return Value::get();
42  }
44  update(x);
45  return *this;
46  }
48  update(Value::get() + other);
49  return *this;
50  }
52  update(Value::get() - other);
53  return *this;
54  }
56  update(Value::get() * other);
57  return *this;
58  }
60  update(Value::get() / other);
61  return *this;
62  }
63 };
64 
65 template class TakeoverValue<int>;
66 template class TakeoverValue<float>;
69 
70 #endif /* __TakeoverValue_h__ */
TakeoverValue< int > TakeoverInt
Definition: TakeoverValue.h:68
TakeoverValue< float > TakeoverFloat
Definition: TakeoverValue.h:67
#define abs(x)
Definition: basicmaths.h:44
TakeoverValue allows parameters to implement Takeover logic.
Definition: TakeoverValue.h:19
void setLocked(bool value)
Definition: TakeoverValue.h:28
TakeoverValue< T, Value > & operator=(const T &x)
Definition: TakeoverValue.h:43
TakeoverValue< T, Value > & operator/=(const T &other)
Definition: TakeoverValue.h:59
TakeoverValue< T, Value > & operator*=(const T &other)
Definition: TakeoverValue.h:55
TakeoverValue(const TakeoverValue< T > &other)
Definition: TakeoverValue.h:26
TakeoverValue(T threshold, T init)
Definition: TakeoverValue.h:27
void update(T x)
Definition: TakeoverValue.h:34
TakeoverValue< T, Value > & operator+=(const T &other)
Definition: TakeoverValue.h:47
TakeoverValue(T value)
Definition: TakeoverValue.h:25
TakeoverValue< T, Value > & operator-=(const T &other)
Definition: TakeoverValue.h:51