util/checks.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @file | ||
| 2 | /// @brief Predicates and argument checks for numeric parameters. | ||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <string_view> | ||
| 7 | |||
| 8 | namespace terraingen { | ||
| 9 | |||
| 10 | /// Whether a value is finite and greater than zero. | ||
| 11 | 99359305 | inline bool IsPositive(double value) { | |
| 12 | 99359305 | return std::isfinite(value) && value > 0.0; | |
| 13 | } | ||
| 14 | |||
| 15 | /// Whether a value is finite and not negative. | ||
| 16 | 19927997 | inline bool IsNonNegative(double value) { | |
| 17 | 19927997 | return std::isfinite(value) && value >= 0.0; | |
| 18 | } | ||
| 19 | |||
| 20 | /// Whether a value lies in [0, 1]; false for NaN. | ||
| 21 | 1012949 | inline bool IsInUnitInterval(double value) { | |
| 22 | 1012949 | return value >= 0.0 && value <= 1.0; | |
| 23 | } | ||
| 24 | |||
| 25 | /// Checks that a value is positive and finite. | ||
| 26 | /// @param what The value's name, which starts the error message. | ||
| 27 | /// @throws std::invalid_argument "<what> must be positive and finite" otherwise. | ||
| 28 | void RequirePositive(double value, std::string_view what); | ||
| 29 | |||
| 30 | /// Checks that a value is finite and not negative. | ||
| 31 | /// @param what The value's name, which starts the error message. | ||
| 32 | /// @throws std::invalid_argument "<what> must be finite and not negative" otherwise. | ||
| 33 | void RequireNonNegative(double value, std::string_view what); | ||
| 34 | |||
| 35 | /// Checks that a value is finite. | ||
| 36 | /// @param what The value's name, which starts the error message. | ||
| 37 | /// @throws std::invalid_argument "<what> must be finite" for an infinite or NaN `value`. | ||
| 38 | void RequireFinite(double value, std::string_view what); | ||
| 39 | |||
| 40 | /// Checks that a value lies in [0, 1]. | ||
| 41 | /// @param what The value's name, which starts the error message. | ||
| 42 | /// @throws std::invalid_argument "<what> must be in [0, 1]" otherwise. | ||
| 43 | void RequireUnitInterval(double value, std::string_view what); | ||
| 44 | |||
| 45 | } // namespace terraingen | ||
| 46 |