SEScalar.h
1/* Distributed under the Apache License, Version 2.0.
2See accompanying NOTICE file for details.*/
3
4#pragma once
5#include "cdm/properties/SEProperty.h"
6
7#define ZERO_APPROX 1e-10
8
9class CDM_DECL NoUnit
10{
11public:
12 NoUnit() {}
13 virtual ~NoUnit() {}
14
15 static const NoUnit unitless;
16};
17
18class CDM_DECL SEScalar : public SEProperty
19{
20 friend class PBProperty;//friend the serialization class
21protected:
22 double m_value;
23
24 bool m_isnan=true;
25 bool m_isinf=false;
26 bool m_readOnly=false;
27
28public:
29 SEScalar();
30 virtual ~SEScalar();
31
36 void Invalidate() override;
37 virtual void ForceInvalidate();
38
43 bool Set(const SEScalar& s);
44 bool Force(const SEScalar& s);
45
51 void Copy(const SEScalar& s);
52
53 bool IsValid() const override;
54 bool IsInfinity() const { return m_isinf; }
55
56 bool IsPositive() const;
57 bool IsNegative() const;
58 bool IsZero(double limit = ZERO_APPROX) const;
59
60 void SetReadOnly(bool b);
61 bool IsReadOnly() const;
62
63 double GetValue() const;
64 double GetValue(int precision) const;
65 void SetValue(double d);
66 void ForceValue(double d);
67
68 double Increment(const SEScalar& s);
69 double Increment(double d);
70 double ForceIncrement(double d);
71
72 double Multiply(double d);
73
74 void Average(int cnt);
75
76 bool Equals(const SEScalar& to) const;
77
78 virtual std::string ToString() const;
79 virtual void ToString(std::ostream &str) const;
80
81 static double dNaN();
82 static bool IsNumber(double d);
83 static bool IsZero(double value, double limit);
84 static bool IsValue(double target, double value);
85 static double Truncate(double value, int decimal_places);
86};
87
88inline std::ostream& operator<< (std::ostream& out, const SEScalar* s)
89{
90 if (s == nullptr)
91 out << SEScalar::dNaN() << std::flush;
92 else
93 (*s).ToString(out);
94 return out;
95}
96inline std::ostream& operator<< (std::ostream& out, const SEScalar& s)
97{
98 s.ToString(out);
99 return out;
100}
101
102template <typename Enum>
103class CDM_DECL SEScalarEnum : public SEScalar
104{
105public:
106 SEScalarEnum() : SEScalar() { Invalidate(); }
107 virtual ~SEScalarEnum() { Invalidate(); }
108
109 virtual void Invalidate() override
110 {
112 m_enum = (Enum)-1;
113 }
114 virtual void ForceInvalidate() override
115 {
117 m_enum = (Enum)-1;
118 }
119
120 void Set(const SEScalarEnum<Enum>& s)
121 {
122 SEScalar::Set(s);
123 m_enum = s.m_enum;
124 }
126 {
127 SEScalar::Set(s);
128 m_enum = s.m_enum;
129 }
130
131 Enum GetEnum() const { return m_enum; }
132 //void SetEnum(Enum e)
133 //{
134 // m_enum = e;
135 // SetValue((double)e);
136 //}
137 void operator=(const Enum& e)
138 {
139 m_enum = e;
140 SetValue((double)e);
141 }
142 bool operator==(const Enum& e)
143 {
144 return m_enum == e;
145 }
146 bool operator!=(const Enum& e)
147 {
148 return m_enum != e;
149 }
150
151private:
153
156 using SEScalar::IsZero;
157
158 using SEScalar::SetValue;
160
163
164 using SEScalar::Multiply;
165
166 using SEScalar::Average;
167
168protected:
169 Enum m_enum;
170};
171
177class CDM_DECL SEUnitScalar : public SEScalar
178{
179 friend class PBProperty;//friend the serialization class
181public:
182
184 virtual ~SEUnitScalar() {}
185
186 bool IsValid() const override = 0;
187 void Invalidate() override = 0;
188 void ForceInvalidate() override = 0;
189 virtual bool HasUnit() const = 0;
190 virtual const CCompoundUnit* GetUnit() const = 0;
191
192 virtual bool Set(const SEScalar& s) = 0;
193 virtual bool Force(const SEScalar& s) = 0;
194 virtual void Copy(const SEScalar& s) = 0;
195 virtual double GetValue(const CCompoundUnit& unit) const = 0;
196 virtual double GetValue(const CCompoundUnit& unit, int decimal_places) const = 0;
197 virtual void SetValue(double d, const CCompoundUnit& unit) = 0;
198 virtual void ForceValue(double d, const CCompoundUnit& unit) = 0;
199 virtual double Increment(double d, const CCompoundUnit& unit) = 0;
200 virtual double ForceIncrement(double d, const CCompoundUnit& unit) = 0;
201
202protected:
203 virtual const CCompoundUnit* GetCompoundUnit(const std::string& unit) const = 0;
204};
205
206template <typename Unit>
208{
209 friend class PBProperty;//friend the serialization class
210public:
212 virtual ~SEScalarQuantity();
213
214 void Invalidate() override;
215 void ForceInvalidate() override;
216 bool IsValid() const override;
217
218protected:
219
220 // We need to support the SEUnitScalar interface, but make these protected
221 // If you want access in a generic unit way, us an SEGenericScalar wrapper
222 bool Set(const SEScalar& s) override;
223 void Copy(const SEScalar& s) override;
224 bool Force(const SEScalar& s) override;
225
226 double GetValue(const CCompoundUnit& unit) const override;
227 double GetValue(const CCompoundUnit& unit, int decimal_places) const override;
228 void SetValue(double d, const CCompoundUnit& unit) override;
229 void ForceValue(double d, const CCompoundUnit& unit) override;
230 double Increment(double d, const CCompoundUnit& unit) override;
231 double ForceIncrement(double d, const CCompoundUnit& unit) override;
232
233 const CCompoundUnit* GetCompoundUnit(const std::string& unit) const override;
234
235public:
236
237 virtual bool Set(const SEScalarQuantity<Unit>& s);
238 virtual void Copy(const SEScalarQuantity<Unit>& s);
239 virtual bool Force(const SEScalarQuantity<Unit>& s);
240
241 bool HasUnit() const override;
242 const Unit* GetUnit() const override;
243
244 double GetValue() const = delete;// Must provide a unit
245 double GetValue(int precision) const = delete;// Must provide a unit
246 virtual double GetValue(const Unit& unit) const;
247 virtual double GetValue(const Unit& unit, int decimal_places) const;
248
249 void SetValue(double d) = delete;// Must provide a unit
250 virtual void SetValue(double d, const Unit& unit);
251
252 void ForceValue(double d) = delete;// Must provide a unit
253 virtual void ForceValue(double d, const Unit& unit);
254
255 double Increment(const SEScalar& s) = delete;// Must provide a unit
256 virtual double Increment(const SEScalarQuantity& s);
257
258 double Increment(double d) = delete;// Must provide a unit
259 virtual double Increment(double d, const Unit& unit);
260
261 double ForceIncrement(double d) = delete;// Must provide a unit
262 virtual double ForceIncrement(double d, const Unit& unit);
263
264 bool Equals(const SEScalar& to) const = delete;// Must provide a unit
265 virtual bool Equals(const SEScalarQuantity<Unit>& to) const;
266
267 std::string ToString() const override;
268 void ToString(std::ostream &str) const override;
269
270protected:
271 const Unit* m_unit=nullptr;
272};
273
274
275// I created this class for use in connecting DataRequests to SEScalars for the PhysiologyEngineTrack class
281class CDM_DECL SEGenericScalar : public Loggable
282{
283 friend class PBProperty;//friend the serialization class
284public:
285 SEGenericScalar(Logger* logger);
286 virtual ~SEGenericScalar() {};
287
288 virtual bool HasScalar() const;
289 virtual void SetScalar(const SEScalar& s);
290
291 virtual bool IsValid() const;
292 virtual bool IsInfinity() const;
293
294 virtual bool HasUnit() const;
295 virtual const CCompoundUnit* GetUnit() const;
296 virtual bool IsValidUnit(const CCompoundUnit& unit) const;
297 virtual const CCompoundUnit* GetCompoundUnit(const std::string& unit) const;
298
299 virtual double GetValue() const;
300 virtual double GetValue(int decimal_places) const;
301 virtual double GetValue(const CCompoundUnit& unit) const;
302 virtual double GetValue(const CCompoundUnit& unit, int decimal_places) const;
303
304 std::string GetString() const;
305
306protected:
307
310};
311
312inline void Override(const SEScalar& from, SEScalar& to)
313{
314 bool b = to.IsReadOnly();
315 to.SetReadOnly(false);
316 to.Set(from);
317 to.SetReadOnly(b);
318}
319template<class Unit>
320inline void Override(const SEScalarQuantity<Unit>& from, SEScalarQuantity<Unit>& to)
321{
322 bool b = to.IsReadOnly();
323 to.SetReadOnly(false);
324 to.Set(from);
325 to.SetReadOnly(b);
326}
327
328inline void ValueOverride(SEScalar& s, double value)
329{
330 bool b = s.IsReadOnly();
331 s.SetReadOnly(false);
332 s.SetValue(value);
333 s.SetReadOnly(b);
334}
335template<class Unit>
336inline void ValueOverride(SEScalarQuantity<Unit>& s, double value, const Unit& unit)
337{
338 bool b = s.IsReadOnly();
339 s.SetReadOnly(false);
340 s.SetValue(value, unit);
341 s.SetReadOnly(b);
342}
343
344inline void IncrementOverride(SEScalar& s, double value)
345{
346 bool b = s.IsReadOnly();
347 s.SetReadOnly(false);
348 s.Increment(value);
349 s.SetReadOnly(b);
350}
351template<class Unit>
352inline void IncrementOverride(SEScalarQuantity<Unit>& s, double value, const Unit& unit)
353{
354 bool b = s.IsReadOnly();
355 s.SetReadOnly(false);
356 s.Increment(value, unit);
357 s.SetReadOnly(b);
358}
359
360#define DEFINE_SCALAR_PTR(name, type) \
361 public: \
362 bool Has##name() const \
363 { \
364 return m_##name==nullptr?false:m_##name->IsValid(); \
365 } \
366 SEScalar##type& Get##name() \
367 { \
368 if(m_##name==nullptr) \
369 m_##name = new SE##type(); \
370 return *m_##name; \
371 } \
372 double Get##name() const \
373 { \
374 if (m_##name == nullptr) \
375 return SEScalar::dNaN(); \
376 return m_##name->GetValue(); \
377 } \
378 protected: \
379 SEScalar##type m_##name;
380
381#define DEFINE_UNIT_SCALAR_PTR(name, type) \
382 public: \
383 bool Has##name() const \
384 { \
385 return m_##name==nullptr?false:m_##name->IsValid(); \
386 } \
387 SEScalar##type& Get##name() \
388 { \
389 if(m_##name==nullptr) \
390 m_##name = new SE##type(); \
391 return *m_##name; \
392 } \
393 double Get##name(const type##Unit& unit) const \
394 { \
395 if (m_##name == nullptr) \
396 return SEScalar::dNaN(); \
397 return m_##name->GetValue(unit); \
398 } \
399 protected: \
400 SEScalar##type m_##name;
401
402#define DEFINE_SCALAR(name, type) \
403 public: \
404 bool Has##name() const { return m_##name.IsValid(); } \
405 SEScalar##type& Get##name() { return m_##name; } \
406 double Get##name() const { return m_##name.GetValue(); } \
407 protected: \
408 SEScalar##type m_##name;
409
410#define DEFINE_UNIT_SCALAR(name, type) \
411 public: \
412 bool Has##name() const { return m_##name.IsValid(); } \
413 SEScalar##type& Get##name() { return m_##name; } \
414 double Get##name(const type##Unit& unit) const { return m_##name.GetValue(unit); } \
415 protected: \
416 SEScalar##type m_##name;
417
418#include "cdm/properties/SEScalar.hxx"
Definition: CompoundUnit.h:40
Definition: Logger.h:23
Definition: Logger.h:71
Definition: SEScalar.h:10
static const NoUnit unitless
Definition: SEScalar.h:15
virtual ~NoUnit()
Definition: SEScalar.h:13
NoUnit()
Definition: SEScalar.h:12
Definition: PBProperties.h:87
static void Copy(const SECurve &src, SECurve &dst)
Definition: PBProperties.cpp:82
If you want to querry what a scalar is and don't know what scalar type you have...
Definition: SEScalar.h:282
const SEScalar * m_Scalar
Definition: SEScalar.h:308
const SEUnitScalar * m_UnitScalar
Definition: SEScalar.h:309
virtual ~SEGenericScalar()
Definition: SEScalar.h:286
Definition: SEProperty.h:8
Definition: SEScalar.h:104
bool operator==(const Enum &e)
Definition: SEScalar.h:142
Enum m_enum
Definition: SEScalar.h:169
bool operator!=(const Enum &e)
Definition: SEScalar.h:146
void operator=(const Enum &e)
Definition: SEScalar.h:137
void Set(const SEScalarEnum< Enum > &s)
Definition: SEScalar.h:120
Enum GetEnum() const
Definition: SEScalar.h:131
void Copy(const SEScalarEnum< Enum > &s)
Definition: SEScalar.h:125
virtual void ForceInvalidate() override
Definition: SEScalar.h:114
SEScalarEnum()
Definition: SEScalar.h:106
virtual ~SEScalarEnum()
Definition: SEScalar.h:107
virtual void Invalidate() override
Definition: SEScalar.h:109
Definition: SEScalar.h:19
bool IsInfinity() const
Definition: SEScalar.h:54
bool Set(const SEScalar &s)
Definition: SEScalar.cpp:41
virtual void ForceInvalidate()
Definition: SEScalar.cpp:94
static double dNaN()
Definition: SEScalar.cpp:10
void Average(int cnt)
Definition: SEScalar.cpp:219
void SetReadOnly(bool b)
Definition: SEScalar.cpp:129
void ForceValue(double d)
Definition: SEScalar.cpp:165
void SetValue(double d)
Definition: SEScalar.cpp:152
void Invalidate() override
Definition: SEScalar.cpp:85
virtual std::string ToString() const
Definition: SEScalar.cpp:239
double Multiply(double d)
Definition: SEScalar.cpp:208
bool IsReadOnly() const
Definition: SEScalar.cpp:133
bool IsPositive() const
Definition: SEScalar.cpp:115
double m_value
Definition: SEScalar.h:22
bool IsZero(double limit=ZERO_APPROX) const
Definition: SEScalar.cpp:108
bool IsNegative() const
Definition: SEScalar.cpp:122
double ForceIncrement(double d)
Definition: SEScalar.cpp:197
double Increment(const SEScalar &s)
Definition: SEScalar.cpp:177
Definition: SEScalar.h:208
bool Force(const SEScalar &s) override
Definition: SEScalar.hxx:80
double GetValue() const =delete
void ForceValue(double d, const CCompoundUnit &unit) override
Definition: SEScalar.hxx:189
void Invalidate() override
Definition: SEScalar.hxx:14
bool IsValid() const override
Definition: SEScalar.hxx:28
void SetValue(double d)=delete
void ForceInvalidate() override
Definition: SEScalar.hxx:21
const Unit * GetUnit() const override
Definition: SEScalar.hxx:145
double GetValue(int precision) const =delete
double Increment(double d)=delete
void ForceValue(double d)=delete
const Unit * m_unit
Definition: SEScalar.h:271
bool HasUnit() const override
Definition: SEScalar.hxx:140
const CCompoundUnit * GetCompoundUnit(const std::string &unit) const override
Definition: SEScalar.hxx:267
bool Set(const SEScalar &s) override
Definition: SEScalar.hxx:38
SEScalarQuantity()
Definition: SEScalar.h:211
std::string ToString() const override
Definition: SEScalar.hxx:273
void Copy(const SEScalar &s) override
Definition: SEScalar.hxx:65
double Increment(const SEScalar &s)=delete
void SetValue(double d, const CCompoundUnit &unit) override
Definition: SEScalar.hxx:181
double Increment(double d, const CCompoundUnit &unit) override
Definition: SEScalar.hxx:220
virtual ~SEScalarQuantity()
Definition: SEScalar.hxx:8
bool Equals(const SEScalar &to) const =delete
double ForceIncrement(double d)=delete
double ForceIncrement(double d, const CCompoundUnit &unit) override
Definition: SEScalar.hxx:241
An interface to be used for gaining access to a scalar with any unit type
Definition: SEScalar.h:178
SEUnitScalar()
Definition: SEScalar.h:183
bool IsValid() const override=0
void ForceInvalidate() override=0
friend SEGenericScalar
Definition: SEScalar.h:180
virtual ~SEUnitScalar()
Definition: SEScalar.h:184
virtual bool Set(const SEScalar &s)=0
virtual void SetValue(double d, const CCompoundUnit &unit)=0
virtual double GetValue(const CCompoundUnit &unit, int decimal_places) const =0
virtual bool HasUnit() const =0
virtual double GetValue(const CCompoundUnit &unit) const =0
virtual void Copy(const SEScalar &s)=0
virtual bool Force(const SEScalar &s)=0
virtual double ForceIncrement(double d, const CCompoundUnit &unit)=0
virtual const CCompoundUnit * GetUnit() const =0
virtual void ForceValue(double d, const CCompoundUnit &unit)=0
void Invalidate() override=0
virtual double Increment(double d, const CCompoundUnit &unit)=0
virtual const CCompoundUnit * GetCompoundUnit(const std::string &unit) const =0

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.