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

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.