src/util/checks.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "util/checks.h" | ||
| 4 | |||
| 5 | #include <stdexcept> | ||
| 6 | #include <string> | ||
| 7 | |||
| 8 | namespace terraingen { | ||
| 9 | |||
| 10 | namespace { | ||
| 11 | |||
| 12 | 113 | [[noreturn]] void Fail(std::string_view what, std::string_view requirement) { | |
| 13 | 565 | throw std::invalid_argument(std::string(what) + " must be " + std::string(requirement)); | |
| 14 | } | ||
| 15 | |||
| 16 | } // namespace | ||
| 17 | |||
| 18 | 12693733 | void RequirePositive(double value, std::string_view what) { | |
| 19 | 12693733 | if (!IsPositive(value)) { | |
| 20 | 84 | Fail(what, "positive and finite"); | |
| 21 | } | ||
| 22 | 12693649 | } | |
| 23 | |||
| 24 | 4194830 | void RequireNonNegative(double value, std::string_view what) { | |
| 25 | 4194830 | if (!IsNonNegative(value)) { | |
| 26 | 22 | Fail(what, "finite and not negative"); | |
| 27 | } | ||
| 28 | 4194808 | } | |
| 29 | |||
| 30 | 833 | void RequireFinite(double value, std::string_view what) { | |
| 31 | 833 | if (!std::isfinite(value)) { | |
| 32 | 6 | Fail(what, "finite"); | |
| 33 | } | ||
| 34 | 827 | } | |
| 35 | |||
| 36 | 2 | void RequireUnitInterval(double value, std::string_view what) { | |
| 37 | 2 | if (!IsInUnitInterval(value)) { | |
| 38 | 1 | Fail(what, "in [0, 1]"); | |
| 39 | } | ||
| 40 | 1 | } | |
| 41 | |||
| 42 | 3922206 | void RequireDiameter(double diameter) { | |
| 43 | 3922206 | if (!IsPositive(diameter)) { | |
| 44 | 14 | throw std::domain_error("Diameter must be positive and finite"); | |
| 45 | } | ||
| 46 | 3922192 | } | |
| 47 | |||
| 48 | } // namespace terraingen | ||
| 49 |