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

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.