Plaquette
 
Loading...
Searching...
No Matches
PqValue.h
1// PqValue.h (or inside an existing core header)
2//
3// Typed wrappers for basic values as Chainables.
4// Implements: in >> value; value >> out; and convenient C++ value semantics.
5
6#ifndef PQ_VALUE_INC_
7#define PQ_VALUE_INC_
8
9#include "PqCore.h"
10
11namespace pq {
12
13// ---------- Conversion traits ----------
14template <typename T>
16
17// float passthrough
18template <>
19struct ValueCodec<float> {
20 static inline float fromFloat(float v) { return v; }
21 static inline float toFloat(float v) { return v; }
22};
23
24// int rounding policy (deterministic, no libm dependency)
25template <>
26struct ValueCodec<int> {
27 static inline int fromFloat(float v) {
28 return static_cast<int>(round(v));
29 }
30 static inline float toFloat(int v) { return static_cast<float>(v); }
31};
32
33// bool threshold policy
34template <>
35struct ValueCodec<bool> {
36 static inline bool fromFloat(float v) { return Flowable::analogToDigital(v); }
37 static inline float toFloat(bool v) { return Flowable::digitalToAnalog(v); }
38};
39
40// ---------- Generic Flowable value wrapper ----------
41
42template <typename T, class Codec = ValueCodec<T>>
43class Value : public Flowable {
44public:
45 using value_type = T;
46
47 Value() : _v(0) {}
48 Value(T initial) : _v(initial) {}
49
50 // Flowable API
51 float put(float x) override { _v = Codec::fromFloat(x); return x; }
52 float get() override { return Codec::toFloat(_v); }
53
54 // Explicit accessors
55 inline T value() const { return _v; }
56 inline void value(T v) { _v = v; }
57
58 // Implicit conversion to base type (enables: T t = myValue;)
59 inline operator T() const { return _v; }
60
61 // Assignment from base type
62 inline Value& operator=(T v) { _v = v; return *this; }
63
64 // Prefix ++ / --
65 inline Value& operator++() { ++_v; return *this; }
66 inline Value& operator--() { --_v; return *this; }
67
68 // Postfix ++ / -- (returns the *old* base value like built-in types)
69 inline T operator++(int) { T old = _v; ++_v; return old; }
70 inline T operator--(int) { T old = _v; --_v; return old; }
71
72 // Unary negation
73 inline T operator-() const { return -_v; }
74
75 // Compound assignment with base type
76 inline Value& operator+=(T rhs) { _v += rhs; return *this; }
77 inline Value& operator-=(T rhs) { _v -= rhs; return *this; }
78 inline Value& operator*=(T rhs) { _v *= rhs; return *this; }
79 inline Value& operator/=(T rhs) { _v /= rhs; return *this; }
80
81 // Compound assignment with another Value
82 inline Value& operator+=(const Value& rhs) { _v += rhs._v; return *this; }
83 inline Value& operator-=(const Value& rhs) { _v -= rhs._v; return *this; }
84 inline Value& operator*=(const Value& rhs) { _v *= rhs._v; return *this; }
85 inline Value& operator/=(const Value& rhs) { _v /= rhs._v; return *this; }
86
87 // Compound modulo (integer types only, excluding bool)
88 template <typename U = T>
89 inline enable_if_t<supports_modulo<U>::value, Value&>
90 operator%=(U rhs) { _v %= rhs; return *this; }
91
92 template <typename U = T>
93 inline enable_if_t<supports_modulo<U>::value, Value&>
94 operator%=(const Value& rhs) { _v %= rhs._v; return *this; }
95
96 // Comparisons (Value-to-Value, Value-to-scalar)
97 friend inline bool operator==(const Value& a, const Value& b) { return a._v == b._v; }
98 friend inline bool operator!=(const Value& a, const Value& b) { return a._v != b._v; }
99 friend inline bool operator< (const Value& a, const Value& b) { return a._v < b._v; }
100 friend inline bool operator<=(const Value& a, const Value& b) { return a._v <= b._v; }
101 friend inline bool operator> (const Value& a, const Value& b) { return a._v > b._v; }
102 friend inline bool operator>=(const Value& a, const Value& b) { return a._v >= b._v; }
103
104 friend inline bool operator==(const Value& a, T b) { return a._v == b; }
105 friend inline bool operator==(T a, const Value& b) { return a == b._v; }
106 friend inline bool operator!=(const Value& a, T b) { return a._v != b; }
107 friend inline bool operator!=(T a, const Value& b) { return a != b._v; }
108 friend inline bool operator< (const Value& a, T b) { return a._v < b; }
109 friend inline bool operator< (T a, const Value& b) { return a < b._v; }
110 friend inline bool operator<=(const Value& a, T b) { return a._v <= b; }
111 friend inline bool operator<=(T a, const Value& b) { return a <= b._v; }
112 friend inline bool operator> (const Value& a, T b) { return a._v > b; }
113 friend inline bool operator> (T a, const Value& b) { return a > b._v; }
114 friend inline bool operator>=(const Value& a, T b) { return a._v >= b; }
115 friend inline bool operator>=(T a, const Value& b) { return a >= b._v; }
116
117 // Basic arithmetic: return base type (or choose float — see notes below)
118 friend inline T operator+(const Value& a, const Value& b) { return a._v + b._v; }
119 friend inline T operator-(const Value& a, const Value& b) { return a._v - b._v; }
120 friend inline T operator*(const Value& a, const Value& b) { return a._v * b._v; }
121 friend inline T operator/(const Value& a, const Value& b) { return a._v / b._v; }
122
123 friend inline T operator+(const Value& a, T b) { return a._v + b; }
124 friend inline T operator+(T a, const Value& b) { return a + b._v; }
125 friend inline T operator-(const Value& a, T b) { return a._v - b; }
126 friend inline T operator-(T a, const Value& b) { return a - b._v; }
127 friend inline T operator*(const Value& a, T b) { return a._v * b; }
128 friend inline T operator*(T a, const Value& b) { return a * b._v; }
129 friend inline T operator/(const Value& a, T b) { return a._v / b; }
130 friend inline T operator/(T a, const Value& b) { return a / b._v; }
131
132 // Modulo (integer types only, excluding bool)
133 template <typename U = T>
134 friend inline enable_if_t<supports_modulo<U>::value, U>
135 operator%(const Value& a, const Value& b) { return a._v % b._v; }
136
137 template <typename U = T>
138 friend inline enable_if_t<supports_modulo<U>::value, U>
139 operator%(const Value& a, U b) { return a._v % b; }
140
141 template <typename U = T>
142 friend inline enable_if_t<supports_modulo<U>::value, U>
143 operator%(U a, const Value& b) { return a % b._v; }
144
145protected:
146 T _v;
147};
148
149// ---------- Convenient aliases matching issue #157 ----------
150using Float = Value<float>;
151using Integer = Value<int>;
152using Boolean = Value<bool>;
153
154} // namespace pq
155
156#endif
Definition PqCore.h:303
static float digitalToAnalog(bool b)
Converts digital (bool) value to analog (float) value.
Definition PqCore.h:333
static bool analogToDigital(float f)
Converts analog (float) value to digital (bool) value.
Definition PqCore.h:330
Definition PqValue.h:43
float get() override
Returns value (typically between 0 and 1, may vary depending on class).
Definition PqValue.h:52
float put(float x) override
Pushes value into the unit.
Definition PqValue.h:51
Definition PqValue.h:15