SEScalar.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 #pragma once
14 #include <biogears/cdm/circuit/SECircuit.inl>
15 #include <biogears/cdm/properties/SEProperty.h>
16 #include <biogears/cdm/utils/unitconversion/UCCommon.h>
17 
18 CDM_BIND_DECL(ScalarData)
19 
20 namespace biogears {
21 class SEGenericScalar;
22 
23 static constexpr double ZERO_APPROX = 1e-10;
24 
25 class BIOGEARS_API NoUnit {
26 public:
27  NoUnit() = default;
28  virtual ~NoUnit() = default;
29 
30  static const NoUnit unitless;
31 };
32 
33 class BIOGEARS_API SEScalar : public SEProperty {
34 protected:
35  double m_value;
36  bool m_readOnly;
37 
38 public:
39  SEScalar();
40  SEScalar(double);
41 
42  virtual ~SEScalar();
43 
44  virtual void Clear();
49  virtual void Invalidate();
50  virtual void Load(const CDM::ScalarData& in);
51  virtual CDM::ScalarData* Unload() const;
52 
57  bool Set(const SEScalar& s);
58 
64  void Copy(const SEScalar& s);
65 
66  virtual bool IsValid() const;
67  bool IsInfinity() const { return std::isinf(m_value); }
68 
69  bool IsPositive() const;
70  bool IsNegative() const;
71  bool IsZero(double limit = ZERO_APPROX) const;
72 
73  void SetReadOnly(bool b);
74  bool IsReadOnly() const;
75 
76  virtual double GetValue() const;
77  virtual void SetValue(double d);
78 
79  double Increment(const SEScalar& s);
80  double IncrementValue(double d);
81  double Decrement(const SEScalar& s);
82  double DecrementValue(double d);
83  double Multiply(const SEScalar& s);
84  double MultiplyValue(double d);
85  double Divide(const SEScalar& s);
86  double DivideValue(double d);
87 
88  bool Equals(const SEScalar& to) const;
89 
90  virtual void ToString(std::ostream& str) const;
91 
92  static double dNaN();
93  static double NaN;
94  static bool IsZero(double value, double limit);
95  static bool IsValue(double target, double value);
96 
97  static const std::string unitless;
98 
99  bool operator<(const SEScalar& rhs) const;
100  bool operator<=(const SEScalar& rhs) const;
101  bool operator>(const SEScalar& rhs) const;
102  bool operator>=(const SEScalar& rhs) const;
103 
104  bool operator==(const SEScalar& rhs) const { return Equals(rhs); }
105  bool operator!=(const SEScalar& rhs) const { return !Equals(rhs); }
106 
107  SEScalar operator+(const SEScalar& rhs) const;
108  SEScalar& operator+=(const SEScalar& rhs);
109  SEScalar operator-(const SEScalar& rhs) const;
110  SEScalar& operator-=(const SEScalar& rhs);
111  SEScalar operator/(const SEScalar& rhs) const;
112  SEScalar& operator/=(const SEScalar& rhs);
113  SEScalar operator*(const SEScalar& rhs) const;
114  SEScalar& operator*=(const SEScalar& rhs);
115 
116 protected:
117  virtual void Unload(CDM::ScalarData& s) const;
118 };
119 //-------------------------------------------------------------------------------
120 inline SEScalar operator+(double lhs, const SEScalar& rhs) { return SEScalar{ lhs }.Increment(rhs); };
121 inline SEScalar operator-(double lhs, const SEScalar& rhs) { return SEScalar{ lhs }.Decrement(rhs); };
122 inline SEScalar operator/(double lhs, const SEScalar& rhs) { return SEScalar{ lhs }.Divide(rhs); };
123 inline SEScalar operator*(double lhs, const SEScalar& rhs) { return SEScalar{ lhs }.Multiply(rhs); };
124 inline bool operator<(double lhs, const SEScalar& rhs) { return SEScalar{ lhs } < rhs; };
125 inline bool operator<=(double lhs, const SEScalar& rhs) { return SEScalar{ lhs } <= rhs; };
126 inline bool operator>(double lhs, const SEScalar& rhs) { return SEScalar{ lhs } > rhs; };
127 inline bool operator>=(double lhs, const SEScalar& rhs) { return SEScalar{ lhs } >= rhs; };
128 inline bool operator==(double lhs, const SEScalar& rhs) { return rhs == lhs; }
129 inline bool operator!=(double lhs, const SEScalar& rhs) { return rhs != lhs; }
130 //-------------------------------------------------------------------------------
131 inline std::ostream& operator<<(std::ostream& out, const SEScalar* s)
132 {
133  if (s == nullptr)
134  out << SEScalar::NaN << std::flush;
135  else
136  (*s).ToString(out);
137  return out;
138 }
139 //-------------------------------------------------------------------------------
140 inline std::ostream& operator<<(std::ostream& out, const SEScalar& s)
141 {
142  s.ToString(out);
143  return out;
144 }
145 inline void Override(const SEScalar& from, SEScalar& to)
146 {
147  bool b = to.IsReadOnly();
148  to.SetReadOnly(false);
149  to.Set(from);
150  to.SetReadOnly(b);
151 }
152 
153 //-------------------------------------------------------------------------------
154 inline void ValueOverride(SEScalar& s, double value)
155 {
156  bool b = s.IsReadOnly();
157  s.SetReadOnly(false);
158  s.SetValue(value);
159  s.SetReadOnly(b);
160 }
161 //-------------------------------------------------------------------------------
162 inline void IncrementOverride(SEScalar& s, double value)
163 {
164  bool b = s.IsReadOnly();
165  s.SetReadOnly(false);
166  s.IncrementValue(value);
167  s.SetReadOnly(b);
168 }
169 }
bool operator>=(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:127
static constexpr double ZERO_APPROX
Definition: SEScalar.h:23
bool operator<=(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:125
bool IsReadOnly() const
Definition: SEScalar.cpp:177
bool m_readOnly
Definition: SEScalar.h:36
Definition: SEProperty.h:19
Definition: SEScalar.h:33
void Copy(const std::vector< T * > &from, std::vector< T * > &to)
Definition: Macros.h:69
void Override(const SEScalar &from, SEScalar &to)
Definition: SEScalar.h:145
virtual void ToString(std::ostream &str) const
Definition: SEScalar.cpp:293
static double NaN
Definition: SEScalar.h:93
SEScalar operator+(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:120
virtual void Invalidate()=0
static const std::string unitless
Definition: SEScalar.h:97
double Multiply(const SEScalar &s)
Definition: SEScalar.cpp:241
CPScalar & operator*=(CPScalar &lhs, const T &rhs)
Definition: PScalar.h:376
bool Set(const SEScalar &s)
Definition: SEScalar.cpp:96
virtual void Unload(CDM::PropertyData &data) const
Definition: SEProperty.cpp:35
double Decrement(const SEScalar &s)
Definition: SEScalar.cpp:227
bool operator!=(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:129
SEScalar operator/(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:122
void ValueOverride(SEScalar &s, double value)
Definition: SEScalar.h:154
SEScalar operator*(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:123
Class corresponding to the ScalarData schema type.
Definition: Properties.hxx:3007
virtual void Clear()
Definition: SEProperty.cpp:26
virtual bool IsValid() const =0
double Increment(const SEScalar &s)
Definition: SEScalar.cpp:207
bool operator==(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:128
bool operator!=(const SEScalar &rhs) const
Definition: SEScalar.h:105
bool operator==(const SEScalar &rhs) const
Definition: SEScalar.h:104
bool operator>(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:126
Definition: SEScalar.h:25
virtual void SetValue(double d)
Definition: SEScalar.cpp:194
virtual bool Load(const CDM::PropertyData &in)
Definition: SEProperty.cpp:30
bool operator<(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:124
static const NoUnit unitless
Definition: SEScalar.h:30
CPScalar & operator/=(CPScalar &lhs, const T &rhs)
Definition: PScalar.h:396
SEScalar operator-(double lhs, const SEScalar &rhs)
Definition: SEScalar.h:121
void SetReadOnly(bool b)
Definition: SEScalar.cpp:172
Definition: SEElectricalCircuit.h:18
void IncrementOverride(SEScalar &s, double value)
Definition: SEScalar.h:162
double m_value
Definition: SEScalar.h:35
double Divide(const SEScalar &s)
Definition: SEScalar.cpp:260
double IncrementValue(double d)
Definition: SEScalar.cpp:217
bool IsInfinity() const
Definition: SEScalar.h:67