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  ProbeBase(const ProbeBase&) = delete;
17  ProbeBase& operator=(const ProbeBase&) = delete;
18 
19  [[nodiscard]] const std::string& getName() const { return name; }
20  [[nodiscard]] std::string_view getDescription() const { return description; }
21  [[nodiscard]] virtual std::string getValue() const = 0;
22 
23 protected:
24  ProbeBase(Debugger& debugger, std::string name,
25  static_string_view description);
26  ~ProbeBase();
27 
28 private:
29  Debugger& debugger;
30  const std::string name;
31  const static_string_view description;
32 };
33 
34 
35 template<typename T> class Probe final : public ProbeBase
36 {
37 public:
38  Probe(Debugger& debugger, std::string name,
39  static_string_view description, T t);
40 
41  Probe& operator=(const T& newValue) {
42  if (value != newValue) {
43  value = newValue;
44  notify();
45  }
46  return *this;
47  }
48 
49  [[nodiscard]] operator const T&() const {
50  return value;
51  }
52 
53 private:
54  [[nodiscard]] std::string getValue() const override;
55 
56  T value;
57 };
58 
59 template<typename T>
60 Probe<T>::Probe(Debugger& debugger_, std::string name_,
61  static_string_view description_, T t)
62  : ProbeBase(debugger_, std::move(name_), std::move(description_))
63  , value(std::move(t))
64 {
65 }
66 
67 template<typename T>
68 std::string Probe<T>::getValue() const
69 {
70  return strCat(value);
71 }
72 
73 // specialization for void
74 template<> class Probe<void> final : public ProbeBase
75 {
76 public:
77  Probe(Debugger& debugger, std::string name, static_string_view description);
78  void signal();
79 
80 private:
81  [[nodiscard]] std::string getValue() const override;
82 };
83 
84 } // namespace openmsx
85 
86 #endif
TclObject t
virtual std::string getValue() const =0
const std::string & getName() const
Definition: Probe.hh:19
ProbeBase(const ProbeBase &)=delete
std::string_view getDescription() const
Definition: Probe.hh:20
ProbeBase & operator=(const ProbeBase &)=delete
Probe(Debugger &debugger, std::string name, static_string_view description, T t)
Definition: Probe.hh:60
Probe & operator=(const T &newValue)
Definition: Probe.hh:41
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:9
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591