ad_physics
Duration.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_DURATION_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_DURATION_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_DURATION_EXPLICIT_CONVERSION_
51 #endif
52 
59 class DurationSquared;
60 
67 class Duration
68 {
69 public:
73  static const double cMinValue;
74 
78  static const double cMaxValue;
79 
84  static const double cPrecisionValue;
85 
93  : mDuration(std::numeric_limits<double>::quiet_NaN())
94  {
95  }
96 
103  : mDuration(iDuration)
104  {
105  }
106 
110  Duration(const Duration &other) = default;
111 
115  Duration(Duration &&other) = default;
116 
124  Duration &operator=(const Duration &other) = default;
125 
133  Duration &operator=(Duration &&other) = default;
134 
142  bool operator==(const Duration &other) const
143  {
144  ensureValid();
145  other.ensureValid();
146  return std::fabs(mDuration - other.mDuration) < cPrecisionValue;
147  }
148 
156  bool operator!=(const Duration &other) const
157  {
158  return !operator==(other);
159  }
160 
170  bool operator>(const Duration &other) const
171  {
172  ensureValid();
173  other.ensureValid();
174  return (mDuration > other.mDuration) && operator!=(other);
175  }
176 
186  bool operator<(const Duration &other) const
187  {
188  ensureValid();
189  other.ensureValid();
190  return (mDuration < other.mDuration) && operator!=(other);
191  }
192 
202  bool operator>=(const Duration &other) const
203  {
204  ensureValid();
205  other.ensureValid();
206  return ((mDuration > other.mDuration) || operator==(other));
207  }
208 
218  bool operator<=(const Duration &other) const
219  {
220  ensureValid();
221  other.ensureValid();
222  return ((mDuration < other.mDuration) || operator==(other));
223  }
224 
235  Duration operator+(const Duration &other) const
236  {
237  ensureValid();
238  other.ensureValid();
239  Duration const result(mDuration + other.mDuration);
240  result.ensureValid();
241  return result;
242  }
243 
254  Duration &operator+=(const Duration &other)
255  {
256  ensureValid();
257  other.ensureValid();
258  mDuration += other.mDuration;
259  ensureValid();
260  return *this;
261  }
262 
273  Duration operator-(const Duration &other) const
274  {
275  ensureValid();
276  other.ensureValid();
277  Duration const result(mDuration - other.mDuration);
278  result.ensureValid();
279  return result;
280  }
281 
293  {
294  ensureValid();
295  other.ensureValid();
296  mDuration -= other.mDuration;
297  ensureValid();
298  return *this;
299  }
300 
312  DurationSquared operator*(const Duration &other) const;
313 
324  Duration operator*(const double &scalar) const
325  {
326  ensureValid();
327  Duration const result(mDuration * scalar);
328  result.ensureValid();
329  return result;
330  }
331 
342  Duration operator/(const double &scalar) const
343  {
344  Duration const scalarDuration(scalar);
345  Duration const result(operator/(scalarDuration));
346  result.ensureValid();
347  return result;
348  }
349 
361  double operator/(const Duration &other) const
362  {
363  ensureValid();
364  other.ensureValidNonZero();
365  double const result = mDuration / other.mDuration;
366  return result;
367  }
368 
378  {
379  ensureValid();
380  Duration const result(-mDuration);
381  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
382  return result;
383  }
384 
392  {
393  return mDuration;
394  }
395 
403  bool isValid() const
404  {
405  auto const valueClass = std::fpclassify(mDuration);
406  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mDuration)
407  && (mDuration <= cMaxValue);
408  }
409 
416  void ensureValid() const
417  {
418  if (!isValid())
419  {
420  spdlog::info("ensureValid(::ad::physics::Duration)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
421 #if (AD_PHYSICS_DURATION_THROWS_EXCEPTION == 1)
422  throw std::out_of_range("Duration value out of range"); // LCOV_EXCL_BR_LINE
423 #endif
424  }
425  }
426 
433  void ensureValidNonZero() const
434  {
435  ensureValid();
436  if (operator==(Duration(0.))) // LCOV_EXCL_BR_LINE
437  {
438  spdlog::info("ensureValid(::ad::physics::Duration)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
439 #if (AD_PHYSICS_DURATION_THROWS_EXCEPTION == 1)
440  throw std::out_of_range("Duration value is zero"); // LCOV_EXCL_BR_LINE
441 #endif
442  }
443  }
444 
448  static Duration getMin()
449  {
450  return Duration(cMinValue);
451  }
452 
456  static Duration getMax()
457  {
458  return Duration(cMaxValue);
459  }
460 
465  {
466  return Duration(cPrecisionValue);
467  }
468 
469 private:
473  double mDuration;
474 };
475 
476 } // namespace physics
477 } // namespace ad
489 inline ::ad::physics::Duration operator*(const double &other, ::ad::physics::Duration const &value)
490 {
491  return value.operator*(other);
492 }
493 
497 namespace std {
498 
502 inline ::ad::physics::Duration fabs(const ::ad::physics::Duration other)
503 {
504  ::ad::physics::Duration const result(std::fabs(static_cast<double>(other)));
505  return result;
506 }
507 
516 template <> class numeric_limits<::ad::physics::Duration> : public numeric_limits<double>
517 {
518 public:
522  static inline ::ad::physics::Duration lowest()
523  {
524  return ::ad::physics::Duration::getMin();
525  }
529  static inline ::ad::physics::Duration max()
530  {
531  return ::ad::physics::Duration::getMax();
532  }
533 
537  static inline ::ad::physics::Duration epsilon()
538  {
539  return ::ad::physics::Duration::getPrecision();
540  }
541 };
542 
543 } // namespace std
544 
548 #ifndef GEN_GUARD_AD_PHYSICS_DURATION
549 #define GEN_GUARD_AD_PHYSICS_DURATION
550 
553 namespace ad {
557 namespace physics {
558 
568 inline std::ostream &operator<<(std::ostream &os, Duration 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::Duration const &value)
581 {
582  return to_string(static_cast<double>(value));
583 }
584 } // namespace std
585 #endif // GEN_GUARD_AD_PHYSICS_DURATION
_AD_PHYSICS_DURATION_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_DURATION_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Duration.hpp:50
ad::physics::Duration::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Duration is valid and non zero
Definition: Duration.hpp:433
ad::physics::Duration::operator+=
Duration & operator+=(const Duration &other)
standard arithmetic operator
Definition: Duration.hpp:254
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Duration::operator*
Duration operator*(const double &scalar) const
standard arithmetic operator
Definition: Duration.hpp:324
ad::physics::Duration::Duration
Duration()
default constructor
Definition: Duration.hpp:92
std::numeric_limits<::ad::physics::Duration >::lowest
static inline ::ad::physics::Duration lowest()
Definition: Duration.hpp:522
ad::physics::Duration::getMax
static Duration getMax()
get maximum valid Duration (i.e. cMaxValue)
Definition: Duration.hpp:456
ad::physics::Duration::cMinValue
static const double cMinValue
constant defining the minimum valid Duration value (used in isValid())
Definition: Duration.hpp:73
ad::physics::Duration::ensureValid
void ensureValid() const
ensure that the Duration is valid
Definition: Duration.hpp:416
ad::physics::Duration::operator>
bool operator>(const Duration &other) const
standard comparison operator
Definition: Duration.hpp:170
ad::physics::Duration::operator=
Duration & operator=(const Duration &other)=default
standard assignment operator
ad::physics::DurationSquared
DataType DurationSquared.
Definition: DurationSquared.hpp:67
std::numeric_limits<::ad::physics::Duration >::max
static inline ::ad::physics::Duration max()
Definition: Duration.hpp:529
ad::physics::Duration::operator>=
bool operator>=(const Duration &other) const
standard comparison operator
Definition: Duration.hpp:202
ad::physics::Duration::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Duration value accuracy (used in comparison operator==(),...
Definition: Duration.hpp:84
ad::physics::Duration::operator-=
Duration operator-=(const Duration &other)
standard arithmetic operator
Definition: Duration.hpp:292
ad::physics::Duration::operator/
Duration operator/(const double &scalar) const
standard arithmetic operator
Definition: Duration.hpp:342
ad::physics::Duration::operator+
Duration operator+(const Duration &other) const
standard arithmetic operator
Definition: Duration.hpp:235
ad::physics::Duration::operator<=
bool operator<=(const Duration &other) const
standard comparison operator
Definition: Duration.hpp:218
ad::physics::Duration::operator/
double operator/(const Duration &other) const
standard arithmetic operator
Definition: Duration.hpp:361
ad::physics::Duration::operator-
Duration operator-() const
standard arithmetic operator
Definition: Duration.hpp:377
ad::physics::Duration::operator*
DurationSquared operator*(const Duration &other) const
standard arithmetic operator
ad::physics::Duration::cMaxValue
static const double cMaxValue
constant defining the maximum valid Duration value (used in isValid())
Definition: Duration.hpp:78
std::numeric_limits<::ad::physics::Duration >::epsilon
static inline ::ad::physics::Duration epsilon()
Definition: Duration.hpp:537
ad::physics::Duration::getMin
static Duration getMin()
get minimum valid Duration (i.e. cMinValue)
Definition: Duration.hpp:448
ad::physics::Duration::operator<
bool operator<(const Duration &other) const
standard comparison operator
Definition: Duration.hpp:186
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
ad::physics::Duration::Duration
_AD_PHYSICS_DURATION_EXPLICIT_CONVERSION_ Duration(double const iDuration)
standard constructor
Definition: Duration.hpp:102
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::Duration
DataType Duration.
Definition: Duration.hpp:67
ad::physics::Duration::isValid
bool isValid() const
Definition: Duration.hpp:403
ad::physics::Duration::getPrecision
static Duration getPrecision()
get assumed accuracy of Duration (i.e. cPrecisionValue)
Definition: Duration.hpp:464
operator*
inline ::ad::physics::Duration operator*(const double &other, ::ad::physics::Duration const &value)
standard arithmetic operator
Definition: Duration.hpp:489
ad::physics::Duration::operator!=
bool operator!=(const Duration &other) const
standard comparison operator
Definition: Duration.hpp:156
ad::physics::Duration::operator==
bool operator==(const Duration &other) const
standard comparison operator
Definition: Duration.hpp:142
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Duration::operator-
Duration operator-(const Duration &other) const
standard arithmetic operator
Definition: Duration.hpp:273