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, const std::string& name,
21  const std::string& description);
22  virtual ~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 : public ProbeBase
32 {
33 public:
34  Probe(Debugger& debugger, const std::string& name,
35  const std::string& description, const 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  virtual std::string getValue() const;
51 
52  T value;
53 };
54 
55 template<typename T>
56 Probe<T>::Probe(Debugger& debugger, const std::string& name,
57  const std::string& description, const T& t)
58  : ProbeBase(debugger, name, description)
59  , value(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> : public ProbeBase
71 {
72 public:
73  Probe(Debugger& debugger, const std::string& name,
74  const std::string& description);
75  void signal();
76 
77 private:
78  virtual std::string getValue() const;
79 };
80 
81 } // namespace openmsx
82 
83 #endif
string toString(long long a)
Definition: StringOp.cc:155
virtual ~ProbeBase()
Definition: Probe.cc:15
Probe(Debugger &debugger, const std::string &name, const std::string &description, const T &t)
Definition: Probe.hh:56
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 std::string & getName() const
Definition: Probe.hh:15
const T & operator=(const T &newValue)
Definition: Probe.hh:37
virtual std::string getValue() const =0
ProbeBase(Debugger &debugger, const std::string &name, const std::string &description)
Definition: Probe.cc:6