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