Branch data Line data Source code
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 : : 11 : : /** 12 : : * Generated file 13 : : * @file 14 : : * 15 : : * Generator Version : 11.0.0-1997 16 : : */ 17 : : 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" 27 : : /*! 28 : : * @brief namespace ad 29 : : */ 30 : : namespace ad { 31 : : /*! 32 : : * @brief namespace physics 33 : : */ 34 : : namespace physics { 35 : : 36 : : /*! 37 : : * \brief Define to indicate whether throwing exceptions is enabled 38 : : */ 39 : : #define AD_PHYSICS_PROBABILITY_THROWS_EXCEPTION 1 40 : : 41 : : #if SAFE_DATATYPES_EXPLICIT_CONVERSION 42 : : /*! 43 : : * \brief Enable/Disable explicit conversion. Currently set to "only explicit conversion". 44 : : */ 45 : : #define _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_ explicit 46 : : #else 47 : : /*! 48 : : * \brief Enable/Disable explicit conversion. Currently set to "implicit conversion allowed". 49 : : */ 50 : : #define _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_ 51 : : #endif 52 : : 53 : : /*! 54 : : * \brief DataType Probability 55 : : * 56 : : * A probability value in the range of [0.0; 1.0]. 57 : : */ 58 : : class Probability 59 : : { 60 : : public: 61 : : /*! 62 : : * \brief constant defining the minimum valid Probability value (used in isValid()) 63 : : */ 64 : : static const double cMinValue; 65 : : 66 : : /*! 67 : : * \brief constant defining the maximum valid Probability value (used in isValid()) 68 : : */ 69 : : static const double cMaxValue; 70 : : 71 : : /*! 72 : : * \brief constant defining the assumed Probability value accuracy 73 : : * (used in comparison operator==(), operator!=()) 74 : : */ 75 : : static const double cPrecisionValue; 76 : : 77 : : /*! 78 : : * \brief default constructor 79 : : * 80 : : * The default value of Probability is: 81 : : * std::numeric_limits<double>::quiet_NaN() 82 : : */ 83 : 1 : Probability() 84 : 1 : : mProbability(std::numeric_limits<double>::quiet_NaN()) 85 : : { 86 : 1 : } 87 : : 88 : : /*! 89 : : * \brief standard constructor 90 : : * 91 : : * \note \ref \_AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION\_ defines, if only an explicit conversion is allowed. 92 : : */ 93 : 36 : _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_ Probability(double const iProbability) 94 : 36 : : mProbability(iProbability) 95 : : { 96 : 36 : } 97 : : 98 : : /*! 99 : : * \brief standard copy constructor 100 : : */ 101 : : Probability(const Probability &other) = default; 102 : : 103 : : /*! 104 : : * \brief standard move constructor 105 : : */ 106 : : Probability(Probability &&other) = default; 107 : : 108 : : /** 109 : : * \brief standard assignment operator 110 : : * 111 : : * \param[in] other Other Probability 112 : : * 113 : : * \returns Reference to this Probability. 114 : : */ 115 : : Probability &operator=(const Probability &other) = default; 116 : : 117 : : /** 118 : : * \brief standard move operator 119 : : * 120 : : * \param[in] other Other Probability 121 : : * 122 : : * \returns Reference to this Probability. 123 : : */ 124 : : Probability &operator=(Probability &&other) = default; 125 : : 126 : : /** 127 : : * \brief standard comparison operator 128 : : * 129 : : * \param[in] other Other Probability 130 : : * 131 : : * \returns \c true if both Probability are valid and can be taken as numerically equal 132 : : */ 133 : 6 : bool operator==(const Probability &other) const 134 : : { 135 : 6 : ensureValid(); 136 : 6 : other.ensureValid(); 137 : 6 : return std::fabs(mProbability - other.mProbability) < cPrecisionValue; 138 : : } 139 : : 140 : : /** 141 : : * \brief standard comparison operator 142 : : * 143 : : * \param[in] other Other Probability. 144 : : * 145 : : * \returns \c true if one of the Probability is not valid or they can be taken as numerically different 146 : : */ 147 : : bool operator!=(const Probability &other) const 148 : : { 149 : : return !operator==(other); 150 : : } 151 : : 152 : : /** 153 : : * \brief standard comparison operator 154 : : * 155 : : * \param[in] other Other Probability. 156 : : * 157 : : * \returns \c true if both Probability are valid and 158 : : * this Probability is strictly numerically greater than other. 159 : : * \note the precision of Probability is considered 160 : : */ 161 : : bool operator>(const Probability &other) const 162 : : { 163 : : ensureValid(); 164 : : other.ensureValid(); 165 : : return (mProbability > other.mProbability) && operator!=(other); 166 : : } 167 : : 168 : : /** 169 : : * \brief standard comparison operator 170 : : * 171 : : * \param[in] other Other Probability. 172 : : * 173 : : * \returns \c true if both Probability are valid and 174 : : * this Probability is strictly numerically smaller than other. 175 : : * \note the precision of Probability is considered 176 : : */ 177 : : bool operator<(const Probability &other) const 178 : : { 179 : : ensureValid(); 180 : : other.ensureValid(); 181 : : return (mProbability < other.mProbability) && operator!=(other); 182 : : } 183 : : 184 : : /** 185 : : * \brief standard comparison operator 186 : : * 187 : : * \param[in] other Other Probability. 188 : : * 189 : : * \returns \c true if both Probability are valid and 190 : : * this Probability is numerically greater than other. 191 : : * \note the precision of Probability is considered 192 : : */ 193 : : bool operator>=(const Probability &other) const 194 : : { 195 : : ensureValid(); 196 : : other.ensureValid(); 197 : : return ((mProbability > other.mProbability) || operator==(other)); 198 : : } 199 : : 200 : : /** 201 : : * \brief standard comparison operator 202 : : * 203 : : * \param[in] other Other Probability 204 : : * 205 : : * \returns \c true if both Probability are valid and 206 : : * this Probability is numerically smaller than other. 207 : : * \note the precision of Probability is considered 208 : : */ 209 : 22 : bool operator<=(const Probability &other) const 210 : : { 211 : 22 : ensureValid(); 212 : 22 : other.ensureValid(); 213 [ + + + + ]: 22 : return ((mProbability < other.mProbability) || operator==(other)); 214 : : } 215 : : 216 : : /** 217 : : * \brief standard arithmetic operator 218 : : * 219 : : * \param[in] other Other Probability 220 : : * 221 : : * \returns Result of arithmetic operation. 222 : : * 223 : : * \note throws a std::out_of_range exception if one of the two operands or the result of 224 : : * the operation is not valid 225 : : */ 226 : : Probability operator+(const Probability &other) const 227 : : { 228 : : ensureValid(); 229 : : other.ensureValid(); 230 : : Probability const result(mProbability + other.mProbability); 231 : : result.ensureValid(); 232 : : return result; 233 : : } 234 : : 235 : : /** 236 : : * \brief standard arithmetic operator 237 : : * 238 : : * \param[in] other Other Probability 239 : : * 240 : : * \returns Result of arithmetic operation. 241 : : * 242 : : * \note throws a std::out_of_range exception if one of the two operands or the result of 243 : : * the operation is not valid 244 : : */ 245 : : Probability &operator+=(const Probability &other) 246 : : { 247 : : ensureValid(); 248 : : other.ensureValid(); 249 : : mProbability += other.mProbability; 250 : : ensureValid(); 251 : : return *this; 252 : : } 253 : : 254 : : /** 255 : : * \brief standard arithmetic operator 256 : : * 257 : : * \param[in] other Other Probability 258 : : * 259 : : * \returns Result of arithmetic operation. 260 : : * 261 : : * \note throws a std::out_of_range exception if one of the two operands or the result of 262 : : * the operation is not valid 263 : : */ 264 : : Probability operator-(const Probability &other) const 265 : : { 266 : : ensureValid(); 267 : : other.ensureValid(); 268 : : Probability const result(mProbability - other.mProbability); 269 : : result.ensureValid(); 270 : : return result; 271 : : } 272 : : 273 : : /** 274 : : * \brief standard arithmetic operator 275 : : * 276 : : * \param[in] other Other Probability 277 : : * 278 : : * \returns Result of arithmetic operation. 279 : : * 280 : : * \note throws a std::out_of_range exception if one of the two operands or the result of 281 : : * the operation is not valid 282 : : */ 283 : : Probability operator-=(const Probability &other) 284 : : { 285 : : ensureValid(); 286 : : other.ensureValid(); 287 : : mProbability -= other.mProbability; 288 : : ensureValid(); 289 : : return *this; 290 : : } 291 : : 292 : : /** 293 : : * \brief standard arithmetic operator 294 : : * 295 : : * \param[in] scalar Scalar double value 296 : : * 297 : : * \returns Result of arithmetic operation. 298 : : * 299 : : * \note throws a std::out_of_range exception if \c value or the result of 300 : : * the operation is not valid 301 : : */ 302 : : Probability operator*(const double &scalar) const 303 : : { 304 : : ensureValid(); 305 : : Probability const result(mProbability * scalar); 306 : : result.ensureValid(); 307 : : return result; 308 : : } 309 : : 310 : : /** 311 : : * \brief standard arithmetic operator 312 : : * 313 : : * \param[in] scalar Scalar double value 314 : : * 315 : : * \returns Result of arithmetic operation. 316 : : * 317 : : * \note throws a std::out_of_range exception if this or the result of 318 : : * the operation is not valid or other is zero 319 : : */ 320 : : Probability operator/(const double &scalar) const 321 : : { 322 : : Probability const scalarProbability(scalar); 323 : : Probability const result(operator/(scalarProbability)); 324 : : result.ensureValid(); 325 : : return result; 326 : : } 327 : : 328 : : /** 329 : : * \brief standard arithmetic operator 330 : : * 331 : : * \param[in] other Other Probability 332 : : * 333 : : * \returns Result of arithmetic operation. 334 : : * 335 : : * \note throws a std::out_of_range exception if one of the two operands or the result of 336 : : * the operation is not valid or other is zero 337 : : * \note since Probability is a type with physical unit, the division results in the dimensionless type. 338 : : */ 339 : : double operator/(const Probability &other) const 340 : : { 341 : : ensureValid(); 342 : : other.ensureValidNonZero(); 343 : : double const result = mProbability / other.mProbability; 344 : : return result; 345 : : } 346 : : 347 : : /** 348 : : * \brief standard arithmetic operator 349 : : * 350 : : * \returns Result of arithmetic operation. 351 : : * 352 : : * \note throws a std::out_of_range exception if this or the result of 353 : : * the operation is not valid 354 : : */ 355 : : Probability operator-() const 356 : : { 357 : : ensureValid(); 358 : : Probability const result(-mProbability); 359 : : result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception 360 : : return result; 361 : : } 362 : : 363 : : /*! 364 : : * \brief conversion to base type: double 365 : : * 366 : : * \note the conversion to the base type removes the physical unit. 367 : : * \ref \_AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION\_ defines, if only explicit calls are allowed. 368 : : */ 369 : 14 : _AD_PHYSICS_PROBABILITY_EXPLICIT_CONVERSION_ operator double() const 370 : : { 371 : 14 : return mProbability; 372 : : } 373 : : 374 : : /*! 375 : : * \returns \c true if the Probability in a valid range 376 : : * 377 : : * An Probability value is defined to be valid if: 378 : : * - It is normal or zero (see std::fpclassify()) 379 : : * - \ref cMinValue <= value <= \ref cMaxValue 380 : : */ 381 : 63 : bool isValid() const 382 : : { 383 : 63 : auto const valueClass = std::fpclassify(mProbability); 384 [ + + + - ]: 63 : return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mProbability) 385 [ + + + - ]: 126 : && (mProbability <= cMaxValue); 386 : : } 387 : : 388 : : /*! 389 : : * \brief ensure that the Probability is valid 390 : : * 391 : : * Throws an std::out_of_range() exception if the Probability 392 : : * in not valid (i.e. isValid() returns false) 393 : : */ 394 : 56 : void ensureValid() const 395 : : { 396 [ - + ]: 56 : if (!isValid()) 397 : : { 398 : 0 : spdlog::info("ensureValid(::ad::physics::Probability)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE 399 : : #if (AD_PHYSICS_PROBABILITY_THROWS_EXCEPTION == 1) 400 : 0 : throw std::out_of_range("Probability value out of range"); // LCOV_EXCL_BR_LINE 401 : : #endif 402 : : } 403 : 56 : } 404 : : 405 : : /*! 406 : : * \brief ensure that the Probability is valid and non zero 407 : : * 408 : : * Throws an std::out_of_range() exception if the Probability 409 : : * in not valid or zero (i.e. isValid() returns false) 410 : : */ 411 : : void ensureValidNonZero() const 412 : : { 413 : : ensureValid(); 414 : : if (operator==(Probability(0.))) // LCOV_EXCL_BR_LINE 415 : : { 416 : : spdlog::info("ensureValid(::ad::physics::Probability)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE 417 : : #if (AD_PHYSICS_PROBABILITY_THROWS_EXCEPTION == 1) 418 : : throw std::out_of_range("Probability value is zero"); // LCOV_EXCL_BR_LINE 419 : : #endif 420 : : } 421 : : } 422 : : 423 : : /*! 424 : : * \brief get minimum valid Probability (i.e. \ref cMinValue) 425 : : */ 426 : 7 : static Probability getMin() 427 : : { 428 : 7 : return Probability(cMinValue); 429 : : } 430 : : 431 : : /*! 432 : : * \brief get maximum valid Probability (i.e. \ref cMaxValue) 433 : : */ 434 : 7 : static Probability getMax() 435 : : { 436 : 7 : return Probability(cMaxValue); 437 : : } 438 : : 439 : : /*! 440 : : * \brief get assumed accuracy of Probability (i.e. \ref cPrecisionValue) 441 : : */ 442 : : static Probability getPrecision() 443 : : { 444 : : return Probability(cPrecisionValue); 445 : : } 446 : : 447 : : private: 448 : : /*! 449 : : * \brief the actual value of the type 450 : : */ 451 : : double mProbability; 452 : : }; 453 : : 454 : : } // namespace physics 455 : : } // namespace ad 456 : : /** 457 : : * \brief standard arithmetic operator 458 : : * 459 : : * \param[in] other Other Probability as double value 460 : : * \param[in] value Probability value 461 : : * 462 : : * \returns Result of arithmetic operation. 463 : : * 464 : : * \note throws a std::out_of_range exception if \c value or the result of 465 : : * the operation is not valid 466 : : */ 467 : : inline ::ad::physics::Probability operator*(const double &other, ::ad::physics::Probability const &value) 468 : : { 469 : : return value.operator*(other); 470 : : } 471 : : 472 : : /*! 473 : : * \brief namespace std 474 : : */ 475 : : namespace std { 476 : : 477 : : /*! 478 : : * \brief overload of the std::fabs for Probability 479 : : */ 480 : : inline ::ad::physics::Probability fabs(const ::ad::physics::Probability other) 481 : : { 482 : : ::ad::physics::Probability const result(std::fabs(static_cast<double>(other))); 483 : : return result; 484 : : } 485 : : 486 : : /*! 487 : : * \brief specialization of the std::numeric_limits for Probability 488 : : * 489 : : * Derived from std::numeric_limits<double> with overloaded functions: 490 : : * std::numeric_limits<Probability>::lowest() (\see Probability::getMin()) 491 : : * std::numeric_limits<Probability>::max() (\see Probability::getMax()) 492 : : * std::numeric_limits<Probability>::epsilon() (\see Probability::getPrecision()) 493 : : */ 494 : : template <> class numeric_limits<::ad::physics::Probability> : public numeric_limits<double> 495 : : { 496 : : public: 497 : : /*! 498 : : * \see std::numeric_limits::lowest() 499 : : */ 500 : 7 : static inline ::ad::physics::Probability lowest() 501 : : { 502 : 7 : return ::ad::physics::Probability::getMin(); 503 : : } 504 : : /*! 505 : : * \see std::numeric_limits::max() 506 : : */ 507 : 7 : static inline ::ad::physics::Probability max() 508 : : { 509 : 7 : return ::ad::physics::Probability::getMax(); 510 : : } 511 : : 512 : : /*! 513 : : * \see std::numeric_limits::epsilon() 514 : : */ 515 : : static inline ::ad::physics::Probability epsilon() 516 : : { 517 : : return ::ad::physics::Probability::getPrecision(); 518 : : } 519 : : }; 520 : : 521 : : } // namespace std 522 : : 523 : : /*! 524 : : * \brief protect the definition of functions from duplicates by typedef usage within other data types 525 : : */ 526 : : #ifndef GEN_GUARD_AD_PHYSICS_PROBABILITY 527 : : #define GEN_GUARD_AD_PHYSICS_PROBABILITY 528 : : /*! 529 : : * @brief namespace ad 530 : : */ 531 : : namespace ad { 532 : : /*! 533 : : * @brief namespace physics 534 : : */ 535 : : namespace physics { 536 : : 537 : : /** 538 : : * \brief standard ostream operator 539 : : * 540 : : * \param[in] os The output stream to write to 541 : : * \param[in] _value Probability value 542 : : * 543 : : * \returns The stream object. 544 : : * 545 : : */ 546 : 14 : inline std::ostream &operator<<(std::ostream &os, Probability const &_value) 547 : : { 548 : 14 : return os << double(_value); 549 : : } 550 : : 551 : : } // namespace physics 552 : : } // namespace ad 553 : : 554 : : namespace std { 555 : : /*! 556 : : * \brief overload of the std::to_string for Probability 557 : : */ 558 : : inline std::string to_string(::ad::physics::Probability const &value) 559 : : { 560 : : return to_string(static_cast<double>(value)); 561 : : } 562 : : } // namespace std 563 : : #endif // GEN_GUARD_AD_PHYSICS_PROBABILITY