| e8555c89 Andy McCall |
1 |
#ifndef PROBE_HH |
| 2 |
#define PROBE_HH |
| 3 |
|
| 4 |
#include "TclObject.hh" |
| 5 |
#include "TraceValue.hh" |
| 6 |
|
| 7 |
#include "Subject.hh" |
| 8 |
#include "static_string_view.hh" |
| 9 |
|
| 10 |
#include <string> |
| 11 |
#include <type_traits> |
| 12 |
|
| 13 |
namespace openmsx { |
| 14 |
|
| 15 |
class Debugger; |
| 16 |
|
| 17 |
class ProbeBase : public Subject<ProbeBase> |
| 18 |
{ |
| 19 |
public: |
| 20 |
ProbeBase(const ProbeBase&) = delete; |
| 21 |
ProbeBase(ProbeBase&&) = delete; |
| 22 |
ProbeBase& operator=(const ProbeBase&) = delete; |
| 23 |
ProbeBase& operator=(ProbeBase&&) = delete; |
| 24 |
|
| 25 |
[[nodiscard]] const std::string& getName() const { return name; } |
| 26 |
[[nodiscard]] std::string_view getDescription() const { return description; } |
| 27 |
[[nodiscard]] virtual TclObject getValue() const = 0; |
| 28 |
[[nodiscard]] virtual TraceValue getTraceValue() const = 0; |
| 29 |
|
| 30 |
protected: |
| 31 |
ProbeBase(Debugger& debugger, std::string name, |
| 32 |
static_string_view description); |
| 33 |
~ProbeBase(); |
| 34 |
|
| 35 |
private: |
| 36 |
Debugger& debugger; |
| 37 |
const std::string name; |
| 38 |
const static_string_view description; |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
template<typename T> class Probe final : public ProbeBase |
| 43 |
{ |
| 44 |
public: |
| 45 |
Probe(Debugger& debugger, std::string name, |
| 46 |
static_string_view description, T t); |
| 47 |
|
| 48 |
Probe& operator=(const T& newValue) { |
| 49 |
if (value != newValue) { |
| 50 |
value = newValue; |
| 51 |
notify(); |
| 52 |
} |
| 53 |
return *this; |
| 54 |
} |
| 55 |
|
| 56 |
[[nodiscard]] operator const T&() const { |
| 57 |
return value; |
| 58 |
} |
| 59 |
|
| 60 |
private: |
| 61 |
[[nodiscard]] TclObject getValue() const override; |
| 62 |
[[nodiscard]] TraceValue getTraceValue() const override; |
| 63 |
|
| 64 |
T value; |
| 65 |
}; |
| 66 |
|
| 67 |
template<typename T> |
| 68 |
Probe<T>::Probe(Debugger& debugger_, std::string name_, |
| 69 |
static_string_view description_, T t) |
| 70 |
: ProbeBase(debugger_, std::move(name_), std::move(description_)) |
| 71 |
, value(std::move(t)) |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
template<typename T> |
| 76 |
TclObject Probe<T>::getValue() const |
| 77 |
{ |
| 78 |
return TclObject(value); |
| 79 |
} |
| 80 |
|
| 81 |
template<typename T> |
| 82 |
TraceValue Probe<T>::getTraceValue() const |
| 83 |
{ |
| 84 |
|
| 85 |
if constexpr (std::is_same_v<T, bool>) { |
| 86 |
return TraceValue{uint64_t(value)}; |
| 87 |
} else if constexpr (std::is_integral_v<T>) { |
| 88 |
return TraceValue{uint64_t(value)}; |
| 89 |
} else if constexpr (std::is_floating_point_v<T>) { |
| 90 |
return TraceValue{double(value)}; |
| 91 |
} else { |
| 92 |
return TraceValue{zstring_view(value)}; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
template<> class Probe<void> final : public ProbeBase |
| 98 |
{ |
| 99 |
public: |
| 100 |
Probe(Debugger& debugger, std::string name, static_string_view description); |
| 101 |
void signal() const; |
| 102 |
|
| 103 |
private: |
| 104 |
[[nodiscard]] TclObject getValue() const override; |
| 105 |
[[nodiscard]] TraceValue getTraceValue() const override; |
| 106 |
}; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
#endif |