src/util/checks.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Predicates and argument checks for numeric parameters; each check's `what` names the | ||
| 5 | /// value and starts its error message. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include <cmath> | ||
| 9 | #include <string_view> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | /// Whether a value is finite and greater than zero. | ||
| 14 | 45093314 | inline bool IsPositive(double value) { | |
| 15 | 45281844 | return std::isfinite(value) && value > 0.0; | |
| 16 | } | ||
| 17 | |||
| 18 | /// Whether a value is finite and not negative. | ||
| 19 | 5105045 | inline bool IsNonNegative(double value) { | |
| 20 | 5196961 | return std::isfinite(value) && value >= 0.0; | |
| 21 | } | ||
| 22 | |||
| 23 | /// Whether a value lies in [0, 1]; false for NaN. | ||
| 24 | 296 | inline bool IsInUnitInterval(double value) { | |
| 25 | 150 | return value >= 0.0 && value <= 1.0; | |
| 26 | } | ||
| 27 | |||
| 28 | /// Checks that a value is positive and finite. | ||
| 29 | /// @throws std::invalid_argument "<what> must be positive and finite" otherwise. | ||
| 30 | void RequirePositive(double value, std::string_view what); | ||
| 31 | |||
| 32 | /// Checks that a value is finite and not negative. | ||
| 33 | /// @throws std::invalid_argument "<what> must be finite and not negative" otherwise. | ||
| 34 | void RequireNonNegative(double value, std::string_view what); | ||
| 35 | |||
| 36 | /// Checks that a value is finite. | ||
| 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 | /// @throws std::invalid_argument "<what> must be in [0, 1]" otherwise. | ||
| 42 | void RequireUnitInterval(double value, std::string_view what); | ||
| 43 | |||
| 44 | /// Checks a feature's diameter, the argument of a model defined by size. | ||
| 45 | /// @throws std::domain_error "Diameter must be positive and finite" otherwise. | ||
| 46 | void RequireDiameter(double diameter); | ||
| 47 | |||
| 48 | } // namespace terraingen | ||
| 49 |