ad_physics
Speed.hpp
Go to the documentation of this file.
1 /*
2  * ----------------- BEGIN LICENSE BLOCK ---------------------------------
3  *
4  * Copyright (C) 2018-2020 Intel Corporation
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * ----------------- END LICENSE BLOCK -----------------------------------
9  */
10 
18 #pragma once
19 
20 #include <cmath>
21 #include <iostream>
22 #include <limits>
23 #include <sstream>
24 #include <stdexcept>
25 #include "spdlog/fmt/ostr.h"
26 #include "spdlog/spdlog.h"
30 namespace ad {
34 namespace physics {
35 
39 #define AD_PHYSICS_SPEED_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_SPEED_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_SPEED_EXPLICIT_CONVERSION_
51 #endif
52 
59 class SpeedSquared;
60 
68 class Speed
69 {
70 public:
74  static const double cMinValue;
75 
79  static const double cMaxValue;
80 
85  static const double cPrecisionValue;
86 
94  : mSpeed(std::numeric_limits<double>::quiet_NaN())
95  {
96  }
97 
104  : mSpeed(iSpeed)
105  {
106  }
107 
111  Speed(const Speed &other) = default;
112 
116  Speed(Speed &&other) = default;
117 
125  Speed &operator=(const Speed &other) = default;
126 
134  Speed &operator=(Speed &&other) = default;
135 
143  bool operator==(const Speed &other) const
144  {
145  ensureValid();
146  other.ensureValid();
147  return std::fabs(mSpeed - other.mSpeed) < cPrecisionValue;
148  }
149 
157  bool operator!=(const Speed &other) const
158  {
159  return !operator==(other);
160  }
161 
171  bool operator>(const Speed &other) const
172  {
173  ensureValid();
174  other.ensureValid();
175  return (mSpeed > other.mSpeed) && operator!=(other);
176  }
177 
187  bool operator<(const Speed &other) const
188  {
189  ensureValid();
190  other.ensureValid();
191  return (mSpeed < other.mSpeed) && operator!=(other);
192  }
193 
203  bool operator>=(const Speed &other) const
204  {
205  ensureValid();
206  other.ensureValid();
207  return ((mSpeed > other.mSpeed) || operator==(other));
208  }
209 
219  bool operator<=(const Speed &other) const
220  {
221  ensureValid();
222  other.ensureValid();
223  return ((mSpeed < other.mSpeed) || operator==(other));
224  }
225 
236  Speed operator+(const Speed &other) const
237  {
238  ensureValid();
239  other.ensureValid();
240  Speed const result(mSpeed + other.mSpeed);
241  result.ensureValid();
242  return result;
243  }
244 
255  Speed &operator+=(const Speed &other)
256  {
257  ensureValid();
258  other.ensureValid();
259  mSpeed += other.mSpeed;
260  ensureValid();
261  return *this;
262  }
263 
274  Speed operator-(const Speed &other) const
275  {
276  ensureValid();
277  other.ensureValid();
278  Speed const result(mSpeed - other.mSpeed);
279  result.ensureValid();
280  return result;
281  }
282 
293  Speed operator-=(const Speed &other)
294  {
295  ensureValid();
296  other.ensureValid();
297  mSpeed -= other.mSpeed;
298  ensureValid();
299  return *this;
300  }
301 
313  SpeedSquared operator*(const Speed &other) const;
314 
325  Speed operator*(const double &scalar) const
326  {
327  ensureValid();
328  Speed const result(mSpeed * scalar);
329  result.ensureValid();
330  return result;
331  }
332 
343  Speed operator/(const double &scalar) const
344  {
345  Speed const scalarSpeed(scalar);
346  Speed const result(operator/(scalarSpeed));
347  result.ensureValid();
348  return result;
349  }
350 
362  double operator/(const Speed &other) const
363  {
364  ensureValid();
365  other.ensureValidNonZero();
366  double const result = mSpeed / other.mSpeed;
367  return result;
368  }
369 
378  Speed operator-() const
379  {
380  ensureValid();
381  Speed const result(-mSpeed);
382  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
383  return result;
384  }
385 
393  {
394  return mSpeed;
395  }
396 
404  bool isValid() const
405  {
406  auto const valueClass = std::fpclassify(mSpeed);
407  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mSpeed) && (mSpeed <= cMaxValue);
408  }
409 
416  void ensureValid() const
417  {
418  if (!isValid())
419  {
420  spdlog::info("ensureValid(::ad::physics::Speed)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
421 #if (AD_PHYSICS_SPEED_THROWS_EXCEPTION == 1)
422  throw std::out_of_range("Speed value out of range"); // LCOV_EXCL_BR_LINE
423 #endif
424  }
425  }
426 
433  void ensureValidNonZero() const
434  {
435  ensureValid();
436  if (operator==(Speed(0.))) // LCOV_EXCL_BR_LINE
437  {
438  spdlog::info("ensureValid(::ad::physics::Speed)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
439 #if (AD_PHYSICS_SPEED_THROWS_EXCEPTION == 1)
440  throw std::out_of_range("Speed value is zero"); // LCOV_EXCL_BR_LINE
441 #endif
442  }
443  }
444 
448  static Speed getMin()
449  {
450  return Speed(cMinValue);
451  }
452 
456  static Speed getMax()
457  {
458  return Speed(cMaxValue);
459  }
460 
465  {
466  return Speed(cPrecisionValue);
467  }
468 
469 private:
473  double mSpeed;
474 };
475 
476 } // namespace physics
477 } // namespace ad
489 inline ::ad::physics::Speed operator*(const double &other, ::ad::physics::Speed const &value)
490 {
491  return value.operator*(other);
492 }
493 
497 namespace std {
498 
502 inline ::ad::physics::Speed fabs(const ::ad::physics::Speed other)
503 {
504  ::ad::physics::Speed const result(std::fabs(static_cast<double>(other)));
505  return result;
506 }
507 
516 template <> class numeric_limits<::ad::physics::Speed> : public numeric_limits<double>
517 {
518 public:
522  static inline ::ad::physics::Speed lowest()
523  {
524  return ::ad::physics::Speed::getMin();
525  }
529  static inline ::ad::physics::Speed max()
530  {
531  return ::ad::physics::Speed::getMax();
532  }
533 
537  static inline ::ad::physics::Speed epsilon()
538  {
539  return ::ad::physics::Speed::getPrecision();
540  }
541 };
542 
543 } // namespace std
544 
548 #ifndef GEN_GUARD_AD_PHYSICS_SPEED
549 #define GEN_GUARD_AD_PHYSICS_SPEED
550 
553 namespace ad {
557 namespace physics {
558 
568 inline std::ostream &operator<<(std::ostream &os, Speed const &_value)
569 {
570  return os << double(_value);
571 }
572 
573 } // namespace physics
574 } // namespace ad
575 
576 namespace std {
580 inline std::string to_string(::ad::physics::Speed const &value)
581 {
582  return to_string(static_cast<double>(value));
583 }
584 } // namespace std
585 #endif // GEN_GUARD_AD_PHYSICS_SPEED
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Speed::operator<=
bool operator<=(const Speed &other) const
standard comparison operator
Definition: Speed.hpp:219
ad::physics::Speed::cMaxValue
static const double cMaxValue
constant defining the maximum valid Speed value (used in isValid())
Definition: Speed.hpp:79
ad::physics::Speed::operator/
double operator/(const Speed &other) const
standard arithmetic operator
Definition: Speed.hpp:362
operator*
inline ::ad::physics::Speed operator*(const double &other, ::ad::physics::Speed const &value)
standard arithmetic operator
Definition: Speed.hpp:489
ad::physics::Speed::operator!=
bool operator!=(const Speed &other) const
standard comparison operator
Definition: Speed.hpp:157
std::numeric_limits<::ad::physics::Speed >::max
static inline ::ad::physics::Speed max()
Definition: Speed.hpp:529
ad::physics::Speed::operator*
SpeedSquared operator*(const Speed &other) const
standard arithmetic operator
ad::physics::Speed
DataType Speed.
Definition: Speed.hpp:68
ad::physics::Speed::getPrecision
static Speed getPrecision()
get assumed accuracy of Speed (i.e. cPrecisionValue)
Definition: Speed.hpp:464
ad::physics::Speed::operator-
Speed operator-() const
standard arithmetic operator
Definition: Speed.hpp:378
ad::physics::Speed::operator+=
Speed & operator+=(const Speed &other)
standard arithmetic operator
Definition: Speed.hpp:255
ad::physics::Speed::operator<
bool operator<(const Speed &other) const
standard comparison operator
Definition: Speed.hpp:187
ad::physics::Speed::ensureValid
void ensureValid() const
ensure that the Speed is valid
Definition: Speed.hpp:416
ad::physics::Speed::Speed
_AD_PHYSICS_SPEED_EXPLICIT_CONVERSION_ Speed(double const iSpeed)
standard constructor
Definition: Speed.hpp:103
ad::physics::Speed::operator>
bool operator>(const Speed &other) const
standard comparison operator
Definition: Speed.hpp:171
ad::physics::Speed::operator*
Speed operator*(const double &scalar) const
standard arithmetic operator
Definition: Speed.hpp:325
ad::physics::Speed::operator-
Speed operator-(const Speed &other) const
standard arithmetic operator
Definition: Speed.hpp:274
ad::physics::Speed::isValid
bool isValid() const
Definition: Speed.hpp:404
std::numeric_limits<::ad::physics::Speed >::lowest
static inline ::ad::physics::Speed lowest()
Definition: Speed.hpp:522
ad::physics::Speed::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Speed value accuracy (used in comparison operator==(),...
Definition: Speed.hpp:85
ad::physics::Speed::cMinValue
static const double cMinValue
constant defining the minimum valid Speed value (used in isValid())
Definition: Speed.hpp:74
ad::physics::Speed::operator==
bool operator==(const Speed &other) const
standard comparison operator
Definition: Speed.hpp:143
_AD_PHYSICS_SPEED_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_SPEED_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Speed.hpp:50
ad::physics::Speed::operator=
Speed & operator=(const Speed &other)=default
standard assignment operator
std::numeric_limits<::ad::physics::Speed >::epsilon
static inline ::ad::physics::Speed epsilon()
Definition: Speed.hpp:537
ad::physics::SpeedSquared
DataType SpeedSquared.
Definition: SpeedSquared.hpp:67
std::fabs
inline ::ad::physics::Weight fabs(const ::ad::physics::Weight other)
overload of the std::fabs for Weight
Definition: Weight.hpp:480
std::fabs
inline ::ad::physics::Acceleration fabs(const ::ad::physics::Acceleration other)
overload of the std::fabs for Acceleration
Definition: Acceleration.hpp:481
std::to_string
std::string to_string(::ad::physics::Acceleration const &value)
overload of the std::to_string for Acceleration
Definition: Acceleration.hpp:559
ad::physics::Speed::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Speed is valid and non zero
Definition: Speed.hpp:433
ad::physics::Speed::operator>=
bool operator>=(const Speed &other) const
standard comparison operator
Definition: Speed.hpp:203
ad::physics::Speed::getMin
static Speed getMin()
get minimum valid Speed (i.e. cMinValue)
Definition: Speed.hpp:448
ad::physics::Speed::Speed
Speed()
default constructor
Definition: Speed.hpp:93
ad::physics::Speed::getMax
static Speed getMax()
get maximum valid Speed (i.e. cMaxValue)
Definition: Speed.hpp:456
ad::physics::Speed::operator/
Speed operator/(const double &scalar) const
standard arithmetic operator
Definition: Speed.hpp:343
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Speed::operator+
Speed operator+(const Speed &other) const
standard arithmetic operator
Definition: Speed.hpp:236
ad::physics::Speed::operator-=
Speed operator-=(const Speed &other)
standard arithmetic operator
Definition: Speed.hpp:293