openMSX
Probe.hh
Go to the documentation of this file.
1 #ifndef PROBE_HH
2 #define PROBE_HH
3 
4 #include "static_string_view.hh"
5 #include "Subject.hh"
6 #include "strCat.hh"
7 #include <string>
8 
9 namespace openmsx {
10 
11 class Debugger;
12 
13 class ProbeBase : public Subject<ProbeBase>
14 {
15 public:
16  [[nodiscard]] const std::string& getName() const { return name; }
17  [[nodiscard]] std::string_view getDescription() const { return description; }
18  [[nodiscard]] virtual std::string getValue() const = 0;
19 
20 protected:
21  ProbeBase(Debugger& debugger, std::string name,
22  static_string_view description);
23  ~ProbeBase();
24 
25 private:
26  Debugger& debugger;
27  const std::string name;
28  const static_string_view description;
29 };
30 
31 
32 template<typename T> class Probe final : public ProbeBase
33 {
34 public:
35  Probe(Debugger& debugger, std::string name,
36  static_string_view description, T t);
37 
38  Probe& operator=(const T& newValue) {
39  if (value != newValue) {
40  value = newValue;
41  notify();
42  }
43  return *this;
44  }
45 
46  [[nodiscard]] operator const T&() const {
47  return value;
48  }
49 
50 private:
51  [[nodiscard]] std::string getValue() const override;
52 
53  T value;
54 };
55 
56 template<typename T>
57 Probe<T>::Probe(Debugger& debugger_, std::string name_,
58  static_string_view description_, T t)
59  : ProbeBase(debugger_, std::move(name_), std::move(description_))
60  , value(std::move(t))
61 {
62 }
63 
64 template<typename T>
65 std::string Probe<T>::getValue() const
66 {
67  return strCat(value);
68 }
69 
70 // specialization for void
71 template<> class Probe<void> final : public ProbeBase
72 {
73 public:
74  Probe(Debugger& debugger, std::string name, static_string_view description);
75  void signal();
76 
77 private:
78  [[nodiscard]] std::string getValue() const override;
79 };
80 
81 } // namespace openmsx
82 
83 #endif
TclObject t
virtual std::string getValue() const =0
const std::string & getName() const
Definition: Probe.hh:16
std::string_view getDescription() const
Definition: Probe.hh:17
ProbeBase(Debugger &debugger, std::string name, static_string_view description)
Definition: Probe.cc:6
Probe(Debugger &debugger, std::string name, static_string_view description, T t)
Definition: Probe.hh:57
Probe & operator=(const T &newValue)
Definition: Probe.hh:38
Generic Gang-of-Four Subject class of the Observer pattern, templatized edition.
Definition: Subject.hh:18
static_string_view
This file implemented 3 utility functions:
Definition: Autofire.cc:5
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591