Scope
ScopeValue.h
1 #pragma once
2 
3 #include "ScopeValueBase.h"
4 #include "ScopeDatatypes.h"
5 #include "ScopeException.h"
6 
7 namespace scope {
8 
10 template<class T>
12  : virtual public ScopeValueBase {
13 
14 protected:
16  T value;
17 
18 protected:
20  virtual inline T CoerceValue(const T& v) const { return v; }
21 
22 public:
24  ScopeValue(const T& _value = T(0), const std::wstring& _name = L"None")
25  :ScopeValueBase(_name)
26  , value(_value) {
27  }
28 
31  // Avoid self-assignment
32  if ( this != &v )
33  Set(v.Value(), true, true, false);
34  // assign name and rw state (after we have set the value)
36  return *this;
37  }
38 
40  ScopeValue& operator=(const T& v) {
41  Set(v, true, true, false);
42  return *this;
43  }
44 
46  T Value() const {
47  std::lock_guard<std::mutex> lock(mutex);
48  return value;
49  }
50 
52  T operator() () const {
53  return Value();
54  }
55 
57  operator T() const {
58  return Value();
59  }
60 
67  virtual T Set(const T& _v, const bool& _callguisignal = true, const bool& _callothersignal = true, const bool& _callatnochange = false) {
68  T tmp;
69  bool changed = false;
70  // protected
71  {
72  std::lock_guard<std::mutex> lock(mutex);
73  // If we try to set while onyl read access return immediately
74  if ( !readwrite )
75  return value;
76  // Check if value is actually changed
77  if ( value != _v ) {
78  changed = true;
79  value = CoerceValue(_v);
80  }
81  // keep the value for later outside the protected area
82  tmp = value;
83  }
84  // call signals after unlocking the mutex to avoid possible deadlock!
85  // Since signals2 is thread-safe we do not have to protect call to signal methods with a mutex.
86  // avoid calling when variable was not changed unless desired.
87  if ( changed || _callatnochange) {
88  if ( _callguisignal )
89  changesiggui();
90  if ( _callothersignal )
92  }
93  return tmp;
94  }
95 
97  void SetFromString(const std::wstring& str);
98 
100  std::wstring ToChar() const;
101 
103  void AddToPropertyTree(boost::property_tree::wptree& pt) const {
104  pt.add<std::wstring>(Name(), ToChar());
105  }
106 
108  void SetFromPropertyTree(const boost::property_tree::wptree& pt) {
109  try {
110  SetFromString(pt.get<std::wstring>(Name()));
111  } // catch if we did not find this ScopeValue in the property tree
112  catch (...) {
113  ScopeExceptionHandler(__FUNCTION__, true, true);
114  }
115  }
116 };
117 
118 }
std::wstring Name() const
signalchange_t changesigother
signal that is called for other stuff on value changes
virtual T Set(const T &_v, const bool &_callguisignal=true, const bool &_callothersignal=true, const bool &_callatnochange=false)
Sets the value and calls both change signals if the value actually changed.
Definition: ScopeValue.h:67
ScopeValueBase & operator=(const ScopeValueBase &_svb)
Safe assignment.
ScopeValue & operator=(const T &v)
Safe assignment.
Definition: ScopeValue.h:40
void AddToPropertyTree(boost::property_tree::wptree &pt) const
Adds the value to a Boost property tree, using its name and value.
Definition: ScopeValue.h:103
T Value() const
Definition: ScopeValue.h:46
std::mutex mutex
a mutex for protection.
virtual T CoerceValue(const T &v) const
When setting the value it can be coerced by a derivation of this function.
Definition: ScopeValue.h:20
bool readwrite
read-write state, true if read&write, false if onyl read (not an atomic, since access to the data var...
In here all declarations for all kinds of datatypes Scope needs.
ScopeValue & operator=(const ScopeValue &v)
Safe assignment.
Definition: ScopeValue.h:30
std::wstring ToChar() const
Converts value into a wstring.
T value
the value proper
Definition: ScopeValue.h:16
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.
signalchange_t changesiggui
signal that is called for GUI on value changes
A templated class for a thread-safe value, with signals to GUI or other stuff that are called on valu...
Definition: ScopeValue.h:11
T operator()() const
Definition: ScopeValue.h:52
void SetFromString(const std::wstring &str)
Parses a wstring and sets the value accordingly.
void SetFromPropertyTree(const boost::property_tree::wptree &pt)
Set value from a Boost property, using its name as a key.
Definition: ScopeValue.h:108
Base class for a thread-safe value, with signals that are called on value changes.
Definition: ScopeValueBase.h:7
ScopeValue(const T &_value=T(0), const std::wstring &_name=L"None")
Construct and set silently.
Definition: ScopeValue.h:24