config/fields/common.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /// @file | ||
| 2 | /// @brief What every configuration field list shares. | ||
| 3 | /// | ||
| 4 | /// Each configuration struct has a `Fields(IO& io, Struct& value)` that calls | ||
| 5 | /// `io(key, member, description, unit)` for each JSON key in order; the same list is read, written | ||
| 6 | /// and described (`ConfigSchema`) by passing a different `IO` (`Reader`, `Writer` or a schema | ||
| 7 | /// writer). Each alternative of a variant also has a `SpecName` next to its `Fields`. | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <cmath> | ||
| 11 | #include <limits> | ||
| 12 | #include <optional> | ||
| 13 | #include <string> | ||
| 14 | #include <string_view> | ||
| 15 | |||
| 16 | namespace terraingen::config { | ||
| 17 | |||
| 18 | /// The JSON "type" of the variant alternative `Spec` (`kName`) and what it is (`kDescription`). | ||
| 19 | template <typename Spec> struct SpecName; | ||
| 20 | |||
| 21 | /// The values a numeric field, or each number of a list, may take, which `ConfigSchema` gives | ||
| 22 | /// editors and the reference; validation still checks them, with the rules that tie fields | ||
| 23 | /// together. Unset bounds are open. | ||
| 24 | struct FieldRange { | ||
| 25 | std::optional<double> minimum; ///< Smallest value, in the field's unit. | ||
| 26 | std::optional<double> maximum; ///< Largest value, in the field's unit. | ||
| 27 | bool exclusiveMinimum = false; ///< `minimum` itself is out. | ||
| 28 | bool exclusiveMaximum = false; ///< `maximum` itself is out. | ||
| 29 | |||
| 30 | /// Whether the range sets a bound. | ||
| 31 | bool Bounded() const { | ||
| 32 | return minimum.has_value() || maximum.has_value(); | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | |||
| 36 | /// Values of at least `minimum`. | ||
| 37 | 7207 | inline FieldRange AtLeast(double minimum) { | |
| 38 | 7207 | return {minimum, std::nullopt}; | |
| 39 | } | ||
| 40 | |||
| 41 | /// Values above `minimum`; `Above(0)` for positive values. | ||
| 42 | 14185 | inline FieldRange Above(double minimum) { | |
| 43 | 14185 | return {minimum, std::nullopt, true, false}; | |
| 44 | } | ||
| 45 | |||
| 46 | /// Values of at most `maximum`. | ||
| 47 | inline FieldRange AtMost(double maximum) { | ||
| 48 | return {std::nullopt, maximum}; | ||
| 49 | } | ||
| 50 | |||
| 51 | /// Values from `minimum` to `maximum`, both included. | ||
| 52 | 9929 | inline FieldRange Between(double minimum, double maximum) { | |
| 53 | 9929 | return {minimum, maximum}; | |
| 54 | } | ||
| 55 | |||
| 56 | /// Values from `minimum`, included, to below `maximum`. | ||
| 57 | 2130 | inline FieldRange FromBelow(double minimum, double maximum) { | |
| 58 | 2130 | return {minimum, maximum, false, true}; | |
| 59 | } | ||
| 60 | |||
| 61 | /// Values above `minimum` up to `maximum`, included. | ||
| 62 | 799 | inline FieldRange AboveUpTo(double minimum, double maximum) { | |
| 63 | 799 | return {minimum, maximum, true, false}; | |
| 64 | } | ||
| 65 | |||
| 66 | /// Values above `minimum` and below `maximum`. | ||
| 67 | 1403 | inline FieldRange Inside(double minimum, double maximum) { | |
| 68 | 1403 | return {minimum, maximum, true, true}; | |
| 69 | } | ||
| 70 | |||
| 71 | /// Shares from 0 to 1. | ||
| 72 | 4386 | inline FieldRange UnitInterval() { | |
| 73 | 4386 | return Between(0.0, 1.0); | |
| 74 | } | ||
| 75 | |||
| 76 | /// Reads or writes a bound that is unset when infinite, as an optional field. | ||
| 77 | /// @param unit The bound's unit symbol, or empty. | ||
| 78 | /// @param unset The bound when the field is unset: +infinity or -infinity. | ||
| 79 | template <typename IO> | ||
| 80 | 4204 | void OptionalBound(IO& io, std::string_view key, double& bound, std::string_view description, | |
| 81 | std::string_view unit = {}, | ||
| 82 | double unset = std::numeric_limits<double>::infinity(), | ||
| 83 | const FieldRange& range = {}) { | ||
| 84 | 4204 | std::optional<double> value; | |
| 85 | 4204 | if (std::isfinite(bound)) { | |
| 86 | 1020 | value = bound; | |
| 87 | } | ||
| 88 | 4204 | io(key, value, description, unit, range); | |
| 89 | 4204 | bound = value.value_or(unset); | |
| 90 | 4204 | } | |
| 91 | |||
| 92 | /// The description of an output file's path: `file`, such as "GeoTIFF file to write", and where | ||
| 93 | /// relative paths resolve. | ||
| 94 | 540 | inline std::string OutputPathHelp(std::string_view file) { | |
| 95 | 1620 | return std::string(file) + "; relative paths resolve against the output folder (the CLI's " | |
| 96 | 1080 | "--output-dir), else the configuration's folder."; | |
| 97 | } | ||
| 98 | |||
| 99 | /// The description of an input file's path: `file`, such as "GeoTIFF to read the heights from", | ||
| 100 | /// and where relative paths resolve. | ||
| 101 | 151 | inline std::string InputPathHelp(std::string_view file) { | |
| 102 | 302 | return std::string(file) + "; relative paths resolve against the configuration's folder."; | |
| 103 | } | ||
| 104 | |||
| 105 | } // namespace terraingen::config | ||
| 106 |