SEScalar.h
1 /* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3 
4 #pragma once
5 #include "properties/SEProperty.h"
6 #include "utils/unitconversion/UCCommon.h"
7 
8 #define ZERO_APPROX 1e-10
9 
10 class CDM_DECL NoUnit
11 {
12 public:
13  NoUnit() {}
14  virtual ~NoUnit() {}
15 
16  static const NoUnit unitless;
17 };
18 
19 class CDM_DECL SEScalar : public SEProperty
20 {
21  friend class PBProperty;//friend the serialization class
22 protected:
23  double m_value;
24 
25  bool m_isnan;
26  bool m_isinf;
27  bool m_readOnly;
28 
29 public:
30  SEScalar();
31  virtual ~SEScalar();
32 
33  virtual void Clear();
38  virtual void Invalidate();
39 
44  bool Set(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  void Average(int cnt);
71 
72  bool Equals(const SEScalar& to) const;
73 
74  virtual void ToString(std::ostream &str) const;
75 
76  static double dNaN();
77  static unsigned long long int NaN;
78  static bool IsZero(double value, double limit);
79  static bool IsValue(double target, double value);
80 };
81 
82 inline std::ostream& operator<< (std::ostream& out, const SEScalar* s)
83 {
84  if (s == nullptr)
85  out << SEScalar::NaN << std::flush;
86  else
87  (*s).ToString(out);
88  return out;
89 }
90 inline std::ostream& operator<< (std::ostream& out, const SEScalar& s)
91 {
92  s.ToString(out);
93  return out;
94 }
95 
101 class CDM_DECL SEUnitScalar : public SEScalar
102 {
103  friend class PBProperty;//friend the serialization class
105 public:
106 
108  virtual ~SEUnitScalar() {}
109 
110  virtual bool IsValid() const = 0;
111  virtual void Invalidate() = 0;
112  virtual bool HasUnit() const = 0;
113  virtual const CCompoundUnit* GetUnit() const = 0;
114 
115  virtual bool Set(const SEScalar& s) = 0;
116  virtual void Copy(const SEScalar& s) = 0;
117  virtual double GetValue(const CCompoundUnit& unit) const = 0;
118  virtual void SetValue(double d, const CCompoundUnit& unit) = 0;
119  virtual void ForceValue(double d, const CCompoundUnit& unit) = 0;
120  virtual double IncrementValue(double d, const CCompoundUnit& unit) = 0;
121 
122 protected:
123  virtual const CCompoundUnit* GetCompoundUnit(const std::string& unit) const = 0;
124 };
125 
126 template <typename Unit>
128 {
129  friend class PBProperty;//friend the serialization class
130 public:
132  virtual ~SEScalarQuantity();
133 
134  virtual void Clear();
135  virtual void Invalidate();
136  virtual bool IsValid() const;
137 
138 protected:
139 
140  // We need to support the SEUnitScalar interface, but make these protected
141  // If you want access in a generic unit way, us an SEGenericScalar wrapper
142  bool Set(const SEScalar& s);
143  void Copy(const SEScalar& s);
144 
145  virtual double GetValue(const CCompoundUnit& unit) const;
146  virtual void SetValue(double d, const CCompoundUnit& unit);
147  virtual void ForceValue(double d, const CCompoundUnit& unit);
148  virtual double IncrementValue(double d, const CCompoundUnit& unit);
149 
150  virtual const CCompoundUnit* GetCompoundUnit(const std::string& unit) const;
151 
152 public:
153 
154  virtual bool Set(const SEScalarQuantity<Unit>& s);
155  virtual void Copy(const SEScalarQuantity<Unit>& s);
156 
157  virtual bool HasUnit() const;
158  virtual const Unit* GetUnit() const;
159 
160  double GetValue() const = delete;// Must provide a unit
161  virtual double GetValue(const Unit& unit) const;
162 
163  void SetValue(double d) = delete;// Must provide a unit
164  virtual void SetValue(double d, const Unit& unit);
165 
166  void ForceValue(double d) = delete;// Must provide a unit
167  virtual void ForceValue(double d, const Unit& unit);
168 
169  double IncrementValue(double d) = delete;// Must provide a unit
170  virtual double IncrementValue(double d, const Unit& unit);
171 
172  double Increment(const SEScalar& s) = delete;// Must provide a unit
173  virtual double Increment(const SEScalarQuantity& s);
174 
175  bool Equals(const SEScalar& to) const = delete;// Must provide a unit
176  virtual bool Equals(const SEScalarQuantity<Unit>& to) const;
177 
178  virtual void ToString(std::ostream &str) const;
179 
180 protected:
181  const Unit* m_unit;
182 };
183 
184 
185 // I created this class for use in connecting DataRequests to SEScalars for the PhysiologyEngineTrack class
191 class CDM_DECL SEGenericScalar : public Loggable
192 {
193  friend class PBProperty;//friend the serialization class
194 public:
195  SEGenericScalar(Logger* logger);
196  virtual ~SEGenericScalar() {};
197 
198  virtual bool HasScalar();
199  virtual void SetScalar(const SEScalar& s);
200 
201  virtual bool IsValid();
202  virtual bool IsInfinity();
203 
204  virtual bool HasUnit();
205  virtual const CCompoundUnit* GetUnit();
206  virtual bool IsValidUnit(const CCompoundUnit& unit) const;
207  virtual const CCompoundUnit* GetCompoundUnit(const std::string& unit) const;
208 
209  virtual double GetValue() const;
210  virtual double GetValue(const CCompoundUnit& unit) const;
211 
212 protected:
213 
216 };
217 
218 CDM_DECL double Convert(double d, const CCompoundUnit& from, const CCompoundUnit& to);
219 CDM_DECL bool CompatibleUnits(const CCompoundUnit& u1, const CCompoundUnit& u2);
220 
221 inline void Override(const SEScalar& from, SEScalar& to)
222 {
223  bool b = to.IsReadOnly();
224  to.SetReadOnly(false);
225  to.Set(from);
226  to.SetReadOnly(b);
227 }
228 template<class Unit>
229 inline void Override(const SEScalarQuantity<Unit>& from, SEScalarQuantity<Unit>& to)
230 {
231  bool b = to.IsReadOnly();
232  to.SetReadOnly(false);
233  to.Set(from);
234  to.SetReadOnly(b);
235 }
236 
237 inline void ValueOverride(SEScalar& s, double value)
238 {
239  bool b = s.IsReadOnly();
240  s.SetReadOnly(false);
241  s.SetValue(value);
242  s.SetReadOnly(b);
243 }
244 template<class Unit>
245 inline void ValueOverride(SEScalarQuantity<Unit>& s, double value, const Unit& unit)
246 {
247  bool b = s.IsReadOnly();
248  s.SetReadOnly(false);
249  s.SetValue(value, unit);
250  s.SetReadOnly(b);
251 }
252 
253 inline void IncrementOverride(SEScalar& s, double value)
254 {
255  bool b = s.IsReadOnly();
256  s.SetReadOnly(false);
257  s.IncrementValue(value);
258  s.SetReadOnly(b);
259 }
260 template<class Unit>
261 inline void IncrementOverride(SEScalarQuantity<Unit>& s, double value, const Unit& unit)
262 {
263  bool b = s.IsReadOnly();
264  s.SetReadOnly(false);
265  s.IncrementValue(value, unit);
266  s.SetReadOnly(b);
267 }
268 
269 #include "properties/SEScalar.inl"
virtual double IncrementValue(double d, const CCompoundUnit &unit)
Definition: SEScalar.h:19
virtual ~NoUnit()
Definition: SEScalar.h:14
void SetReadOnly(bool b)
Definition: SEScalar.cpp:104
bool IsInfinity() const
Definition: SEScalar.h:54
bool IsReadOnly() const
Definition: SEScalar.cpp:108
Definition: Logger.h:61
double GetValue() const
Definition: SEScalar.cpp:113
virtual void Invalidate()=0
virtual void ToString(std::ostream &str) const
Definition: SEScalar.cpp:177
SEUnitScalar()
Definition: SEScalar.h:107
static unsigned long long int NaN
Definition: SEScalar.h:77
void SetValue(double d)
Definition: SEScalar.cpp:120
If you want to querry what a scalar is and don&#39;t know what scalar type you have...
Definition: SEScalar.h:191
virtual void Clear()
Definition: SEProperty.cpp:17
Definition: SEScalar.h:127
Definition: Logger.h:11
bool m_readOnly
Definition: SEScalar.h:27
Definition: SEScalar.h:10
virtual bool IsValid() const
Definition: SEScalar.cpp:76
static const NoUnit unitless
Definition: SEScalar.h:16
void Copy(const SEScalar &s)
Definition: SEScalar.cpp:58
double m_value
Definition: SEScalar.h:23
double IncrementValue(double d)
Definition: SEScalar.cpp:146
An interface to be used for gaining access to a scalar with any unit type
Definition: SEScalar.h:101
virtual ~SEGenericScalar()
Definition: SEScalar.h:196
void ForceValue(double d)
Definition: SEScalar.cpp:128
virtual void Invalidate()
Definition: SEScalar.cpp:67
virtual bool IsValid() const =0
NoUnit()
Definition: SEScalar.h:13
bool Set(const SEScalar &s)
Definition: SEScalar.cpp:44
virtual ~SEUnitScalar()
Definition: SEScalar.h:108
const SEScalar * m_Scalar
Definition: SEScalar.h:214
bool m_isnan
Definition: SEScalar.h:25
bool Set(const SEScalar &s)
friend SEGenericScalar
Definition: SEScalar.h:104
const SEUnitScalar * m_UnitScalar
Definition: SEScalar.h:215
Definition: CompoundUnit.h:39
virtual void SetValue(double d, const CCompoundUnit &unit)
Definition: PBProperties.h:76
Definition: SEProperty.h:7
bool m_isinf
Definition: SEScalar.h:26
const Unit * m_unit
Definition: SEScalar.h:181