Scope
ScopeValueBase.h
1 #pragma once
2 
3 namespace scope {
4 
8 
9 protected:
11  std::wstring name;
12 
14  mutable std::mutex mutex;
15 
17  bool readwrite;
18 
20  typedef boost::signals2::signal<void ()> signalchange_t;
21 
23  typedef boost::signals2::signal<void (bool)> signalstate_t;
24 
26  signalchange_t changesiggui;
27 
29  signalchange_t changesigother;
30 
32  signalstate_t statesig;
33 
34 public:
36  ScopeValueBase(const std::wstring& _name = L"None")
37  : readwrite(true)
38  , name(_name) {
39  }
40 
43  : readwrite(_svb.GetRWState())
44  , name(_svb.Name()) {
45  }
46 
49  // Avoid self-assignment
50  if ( this != &_svb ) {
51  std::lock_guard<std::mutex> lock(mutex);
52  readwrite = _svb.GetRWState();
53  name = _svb.Name();
54  }
55  return *this;
56  }
57 
59  virtual ~ScopeValueBase() {
60  std::lock_guard<std::mutex> lock(mutex);
61  // If anything still connected now is the time to let it go...
62  changesiggui.disconnect_all_slots();
63  changesigother.disconnect_all_slots();
64  statesig.disconnect_all_slots();
65  }
66 
68  std::wstring Name() const {
69  std::lock_guard<std::mutex> lock(mutex);
70  return name;
71  }
72 
74  void SetName(const std::wstring& _name) {
75  std::lock_guard<std::mutex> lock(mutex);
76  name = _name;
77  }
78 
80  void SetRWState(const bool& _state) {
81  {
82  std::lock_guard<std::mutex> lock(mutex);
83  readwrite = _state;
84  }
85  statesig(_state);
86  }
87 
89  bool GetRWState() const {
90  std::lock_guard<std::mutex> lock(mutex);
91  return readwrite;
92  }
93 
95  boost::signals2::connection ConnectGUI(signalchange_t::slot_type slot) {
96  // signals2 is thread-safe, we do not have to protect method calls on signals with a mutex
97  return changesiggui.connect(slot);
98  }
99 
101  boost::signals2::connection ConnectOther(signalchange_t::slot_type slot) {
102  return changesigother.connect(slot);
103  }
104 
106  boost::signals2::connection ConnectState(signalstate_t::slot_type slot) {
107  return statesig.connect(slot);
108  }
109 };
110 
111 }
std::wstring Name() const
boost::signals2::connection ConnectGUI(signalchange_t::slot_type slot)
Connect signal to slot to GUI.
signalchange_t changesigother
signal that is called for other stuff on value changes
std::wstring name
the name of the value
void SetName(const std::wstring &_name)
Set the name.
ScopeValueBase(const ScopeValueBase &_svb)
Safe copy.
ScopeValueBase & operator=(const ScopeValueBase &_svb)
Safe assignment.
ScopeValueBase(const std::wstring &_name=L"None")
Initialize name and readonly.
virtual ~ScopeValueBase()
Disconnects all slots from signals.
std::mutex mutex
a mutex for protection.
boost::signals2::signal< void()> signalchange_t
typedef for change signal slot functions
bool GetRWState() const
signalstate_t statesig
signal that is called on changes of read/write status
bool readwrite
read-write state, true if read&write, false if onyl read (not an atomic, since access to the data var...
boost::signals2::connection ConnectState(signalstate_t::slot_type slot)
Connect signal to slot for state changes.
boost::signals2::signal< void(bool)> signalstate_t
typedef for state signal slot functions
signalchange_t changesiggui
signal that is called for GUI on value changes
void SetRWState(const bool &_state)
Sets readonly/read-write state.
boost::signals2::connection ConnectOther(signalchange_t::slot_type slot)
Connect signal to slot to other stuff.
Base class for a thread-safe value, with signals that are called on value changes.
Definition: ScopeValueBase.h:7