| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | RABERU - Fancy Parameters Library | ||
| 4 | Copyright : RABERU Project Contributors | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <compare> | ||
| 11 | #include <ostream> | ||
| 12 | #include <type_traits> | ||
| 13 | |||
| 14 | //====================================================================================================================== | ||
| 15 | //! @defgroup main Main RABERU components | ||
| 16 | //! | ||
| 17 | //! @{ | ||
| 18 | //! @defgroup kwds Keywords definitions and handling | ||
| 19 | //! @brief Functions and types to handle RABERU keywords | ||
| 20 | //! @} | ||
| 21 | //====================================================================================================================== | ||
| 22 | namespace rbr | ||
| 23 | { | ||
| 24 | //==================================================================================================================== | ||
| 25 | //! @ingroup kwds | ||
| 26 | //! @brief Callable object wrapper for functional default value | ||
| 27 | //! @tparam Func Callable object to keep | ||
| 28 | //==================================================================================================================== | ||
| 29 | template<typename Func> struct call | ||
| 30 | { | ||
| 31 | 21 | constexpr call(Func f) : callable(f) {} | |
| 32 | 11 | constexpr auto perform() const { return callable(); } | |
| 33 | Func callable; | ||
| 34 | }; | ||
| 35 | |||
| 36 | //==================================================================================================================== | ||
| 37 | //! @ingroup stng | ||
| 38 | //! @brief Callable object wrapper for functional default value | ||
| 39 | //! @tparam Keyword Keyword for the option | ||
| 40 | //! @tparam Value Value stored in the option | ||
| 41 | //==================================================================================================================== | ||
| 42 | template<concepts::keyword Keyword, typename Value> struct option | ||
| 43 | { | ||
| 44 | using stored_value_type = std::decay_t<Value>; | ||
| 45 | using keyword_type = Keyword; | ||
| 46 | |||
| 47 | 227 | constexpr stored_value_type operator()(keyword_type const&) const noexcept { return contents; } | |
| 48 | stored_value_type contents; | ||
| 49 | }; | ||
| 50 | |||
| 51 | //==================================================================================================================== | ||
| 52 | //! @ingroup kwds | ||
| 53 | //! @brief Base class for keyword definition | ||
| 54 | //! | ||
| 55 | //! rbr::as_keyword provides an CRTP base class for keyword-like types. It is internally used | ||
| 56 | //! to provide specific keyword constructors but can be used to define user-defined keyword. | ||
| 57 | //! | ||
| 58 | //! @tparam Keyword Keyword type being defined | ||
| 59 | //==================================================================================================================== | ||
| 60 | template<typename Keyword> struct as_keyword | ||
| 61 | { | ||
| 62 | /// Derived keyword type | ||
| 63 | using tag_type = Keyword; | ||
| 64 | |||
| 65 | /// Keyword comparison | ||
| 66 | inline constexpr auto operator<=>(as_keyword const&) const noexcept = default; | ||
| 67 | |||
| 68 | //================================================================================================================== | ||
| 69 | //! @brief Compile-time validation of value | ||
| 70 | //! | ||
| 71 | //! When a value is bound to a [Keyword](@ref rbr::concepts::keyword) to form an | ||
| 72 | //! [Option](@ref rbr::concepts::option), one can validate this binding by checking arbitrary | ||
| 73 | //! properties on the value type. This is done by the accept() member. | ||
| 74 | //! | ||
| 75 | //! If `Keyword` defines a `check` static template member function, it will be called to provide | ||
| 76 | //! the return value of accept(). Otherwise, true is returned, thus automatically validating any | ||
| 77 | //! value type. | ||
| 78 | //! | ||
| 79 | //! @tparam T Type to validate | ||
| 80 | //! @return `true` if T is accepted by current keyword, `false` otherwise. | ||
| 81 | //! | ||
| 82 | //! ## Example: | ||
| 83 | //! @snippet doc/accept.cpp Custom Accept | ||
| 84 | //================================================================================================================== | ||
| 85 | |||
| 86 | template<typename T> static constexpr bool accept() | ||
| 87 | { | ||
| 88 | if constexpr (stdfix::same_as<std::remove_cvref_t<T>, as_keyword>) return true; | ||
| 89 | else if constexpr (_::checks_for<Keyword, T>) return Keyword::template check<T>(); | ||
| 90 | else return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | //================================================================================================================== | ||
| 94 | //! @brief Assignment of a value to a keyword | ||
| 95 | //! | ||
| 96 | //! Bind a value to current [Keyword](@ref rbr::concepts::keyword) and returns an instance of | ||
| 97 | //! an [Option](@ref rbr::concepts::option). | ||
| 98 | //! | ||
| 99 | //! @param v Bound value | ||
| 100 | //! @return An rbr::option binding the keyword to `v`. | ||
| 101 | //================================================================================================================== | ||
| 102 | template<typename Type> | ||
| 103 | 299 | constexpr auto operator=(Type&& v) const noexcept | |
| 104 | requires(accept<Type>()) | ||
| 105 | { | ||
| 106 | 299 | return option<Keyword, Type>{RBR_FWD(v)}; | |
| 107 | } | ||
| 108 | |||
| 109 | //================================================================================================================== | ||
| 110 | //! @brief Stream insertion function | ||
| 111 | //! | ||
| 112 | //! Display a textual description of current keyword and bound value over an output stream. | ||
| 113 | //! | ||
| 114 | //! If `Keyword` defines a `display` member variable, it will be used to perform this display. | ||
| 115 | //! Otherwise, its value will be displayed along with either a user-defined identifier or its | ||
| 116 | //! stringified typename. | ||
| 117 | //! | ||
| 118 | //! @param os Output stream | ||
| 119 | //! @param v Value bound to current keyword | ||
| 120 | //! @return The up-to-date output stream | ||
| 121 | //! | ||
| 122 | //! ## Example: | ||
| 123 | //! @snippet doc/show.cpp Custom Show | ||
| 124 | //================================================================================================================== | ||
| 125 | 16 | template<typename V> std::ostream& show(std::ostream& os, V const& v) const | |
| 126 | { | ||
| 127 |
1/1✓ Branch 1 taken 11 times.
|
16 | if constexpr (_::displayable<Keyword, V>) return Keyword{}.display(os, v); |
| 128 | else | ||
| 129 | { | ||
| 130 | if constexpr (_::identifiable<Keyword>) os << Keyword::identifier; | ||
| 131 | else os << '[' << _::type<Keyword>.name() << ']'; | ||
| 132 | |||
| 133 | return os << " : " << v << " (" << _::type<V>.name() << ')'; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | /// Specify a default value for the keyword | ||
| 138 | template<typename Type> | ||
| 139 | 16 | constexpr auto operator|(Type&& v) const noexcept | |
| 140 | requires(accept<Type>()) | ||
| 141 | { | ||
| 142 | 16 | return _::type_or_<Keyword, std::remove_cvref_t<Type>>{RBR_FWD(v)}; | |
| 143 | } | ||
| 144 | |||
| 145 | /// Specify a Callable object as a default value for the keyword | ||
| 146 | 11 | template<typename Func> constexpr auto operator|(call<Func>&& v) const noexcept | |
| 147 | { | ||
| 148 | 11 | return _::type_or_<Keyword, call<Func>>{RBR_FWD(v)}; | |
| 149 | } | ||
| 150 | |||
| 151 | //================================================================================================================== | ||
| 152 | //! @brief Keyword fetching from options set | ||
| 153 | //! | ||
| 154 | //! @param o Set of options to inspect | ||
| 155 | //! @return f current keyword is present in `o...`, return its bound value. Otherwise, | ||
| 156 | //! returns an instance of rbr::unknown_key. | ||
| 157 | //! | ||
| 158 | //! ## Example: | ||
| 159 | //! @include doc/keyword_fetch.cpp | ||
| 160 | //================================================================================================================== | ||
| 161 | 10 | template<concepts::option... Os> constexpr decltype(auto) operator()(Os&&... o) const | |
| 162 | { | ||
| 163 |
1/1✓ Branch 1 taken 5 times.
|
10 | return fetch(Keyword{}, RBR_FWD(o)...); |
| 164 | } | ||
| 165 | }; | ||
| 166 | |||
| 167 | //==================================================================================================================== | ||
| 168 | //! @ingroup kwds | ||
| 169 | //! @brief Checked keyword | ||
| 170 | //! | ||
| 171 | //! A Checked keyword is a keyword that verify if a value's type satisfies a predicates before | ||
| 172 | //! binding it | ||
| 173 | //! | ||
| 174 | //! @tparam ID Unique identifier for the keyword | ||
| 175 | //! @tparam Checker Unary template meta-function acting as predicate | ||
| 176 | //==================================================================================================================== | ||
| 177 | template<typename ID, template<class> class Checker> struct checked_keyword : as_keyword<checked_keyword<ID, Checker>> | ||
| 178 | { | ||
| 179 | using as_keyword<checked_keyword<ID, Checker>>::operator=; | ||
| 180 | template<typename T> static constexpr bool check() { return Checker<T>::value; } | ||
| 181 | |||
| 182 | 1 | template<typename V> std::ostream& display(std::ostream& os, V const& v) const | |
| 183 | { | ||
| 184 |
1/1✓ Branch 1 taken 1 times.
|
1 | if constexpr (_::self_identifiable<ID>) os << ID{}; |
| 185 | else | ||
| 186 | { | ||
| 187 | if constexpr (_::identifiable<ID>) os << ID::identifier; | ||
| 188 | else os << '[' << _::type<ID>.name() << ']'; | ||
| 189 | } | ||
| 190 | |||
| 191 | 1 | os << " ::: " << v << " (" << _::type<V>.name() << ") checked by '"; | |
| 192 | 1 | return os << _::type<Checker<V>>.name() << '\''; | |
| 193 | } | ||
| 194 | }; | ||
| 195 | |||
| 196 | //==================================================================================================================== | ||
| 197 | //! @ingroup kwds | ||
| 198 | //! @brief Typed keyword | ||
| 199 | //! | ||
| 200 | //! A Typed keyword is a keyword that verify if a value's type is exactly matching a type. | ||
| 201 | //! | ||
| 202 | //! @tparam ID Unique identifier for the keyword | ||
| 203 | //! @tparam Type Type to accept | ||
| 204 | //==================================================================================================================== | ||
| 205 | template<typename ID, typename Type> struct typed_keyword : as_keyword<typed_keyword<ID, Type>> | ||
| 206 | { | ||
| 207 | using as_keyword<typed_keyword<ID, Type>>::operator=; | ||
| 208 | template<typename T> static constexpr bool check() { return std::is_same_v<std::remove_cvref_t<T>, Type>; } | ||
| 209 | |||
| 210 | template<typename V> std::ostream& display(std::ostream& os, V const& v) const | ||
| 211 | { | ||
| 212 | if constexpr (_::self_identifiable<ID>) os << ID{}; | ||
| 213 | else | ||
| 214 | { | ||
| 215 | if constexpr (_::identifiable<ID>) os << ID::identifier; | ||
| 216 | else os << '[' << _::type<ID>.name() << ']'; | ||
| 217 | } | ||
| 218 | |||
| 219 | return os << " : " << v << " of type '" << _::type<V>.name() << '\''; | ||
| 220 | } | ||
| 221 | }; | ||
| 222 | |||
| 223 | //==================================================================================================================== | ||
| 224 | //! @ingroup kwds | ||
| 225 | //! @brief Regular keyword | ||
| 226 | //! | ||
| 227 | //! A Regular keyword is a keyword that accepts any types. | ||
| 228 | //! | ||
| 229 | //! @tparam ID Unique identifier for the keyword | ||
| 230 | //==================================================================================================================== | ||
| 231 | template<typename ID> struct any_keyword : as_keyword<any_keyword<ID>> | ||
| 232 | { | ||
| 233 | using as_keyword<any_keyword<ID>>::operator=; | ||
| 234 | |||
| 235 | /// ID type associated to the keyword | ||
| 236 | using id_type = ID; | ||
| 237 | |||
| 238 | 13 | template<typename V> std::ostream& display(std::ostream& os, V const& v) const | |
| 239 | { | ||
| 240 |
1/1✓ Branch 1 taken 8 times.
|
13 | if constexpr (_::self_identifiable<ID>) os << ID{}; |
| 241 | else | ||
| 242 | { | ||
| 243 | if constexpr (_::identifiable<ID>) os << ID::identifier; | ||
| 244 | else os << '[' << _::type<ID>.name() << ']'; | ||
| 245 | } | ||
| 246 | |||
| 247 | 13 | return os << " : " << v << " (" << _::type<V>.name() << ')'; | |
| 248 | } | ||
| 249 | }; | ||
| 250 | |||
| 251 | //==================================================================================================================== | ||
| 252 | //! @ingroup kwds | ||
| 253 | //! @brief Flag keyword | ||
| 254 | //! | ||
| 255 | //! A Flag keyword is a keyword which value is given by its mere presence. It accepts no binding | ||
| 256 | //! and return a value convertible to `bool` when set in a rbr::settings. | ||
| 257 | //! | ||
| 258 | //! By design, a flag is also its own rbr::option. | ||
| 259 | //! | ||
| 260 | //! @tparam ID Unique identifier for the keyword | ||
| 261 | //==================================================================================================================== | ||
| 262 | template<typename ID> struct flag_keyword | ||
| 263 | { | ||
| 264 | 189 | constexpr flag_keyword() {} | |
| 265 | constexpr flag_keyword(ID const&) {} | ||
| 266 | |||
| 267 | /// ID type associated to the keyword | ||
| 268 | using id_type = ID; | ||
| 269 | |||
| 270 | template<typename T> static constexpr bool accept() { return std::is_same_v<std::true_type, T>; } | ||
| 271 | |||
| 272 | 11 | std::ostream& show(std::ostream& os, bool) const | |
| 273 | { | ||
| 274 | if constexpr (_::identifiable<ID>) os << ID::identifier; | ||
| 275 |
1/1✓ Branch 1 taken 6 times.
|
11 | else if constexpr (_::self_identifiable<ID>) os << ID{}; |
| 276 | else os << '[' << _::type<ID>.name() << ']'; | ||
| 277 | |||
| 278 | 11 | return os << " : set"; | |
| 279 | } | ||
| 280 | |||
| 281 | using tag_type = ID; | ||
| 282 | using keyword_type = flag_keyword; | ||
| 283 | using stored_value_type = bool; | ||
| 284 | |||
| 285 | 15 | template<typename Type> constexpr auto operator=(Type&&) const noexcept { return *this; } | |
| 286 | |||
| 287 | 8 | template<typename Type> constexpr auto operator|(Type&& v) const noexcept | |
| 288 | { | ||
| 289 | 8 | return _::type_or_<flag_keyword, std::remove_cvref_t<Type>>{RBR_FWD(v)}; | |
| 290 | } | ||
| 291 | |||
| 292 | 10 | template<typename Func> constexpr auto operator|(call<Func>&& v) const noexcept | |
| 293 | { | ||
| 294 | 10 | return _::type_or_<flag_keyword, call<Func>>{RBR_FWD(v)}; | |
| 295 | } | ||
| 296 | |||
| 297 | constexpr auto operator()(keyword_type const&) const noexcept { return true; } | ||
| 298 | |||
| 299 | 1 | template<typename O0, typename O1, typename... Os> constexpr decltype(auto) operator()(O0&&, O1&&, Os&&...) const | |
| 300 | { | ||
| 301 | return stdfix::same_as<keyword_type, typename std::remove_cvref_t<O0>::keyword_type> || | ||
| 302 | 1 | stdfix::same_as<keyword_type, typename std::remove_cvref_t<O1>::keyword_type> || | |
| 303 | 1 | (stdfix::same_as<keyword_type, typename std::remove_cvref_t<Os>::keyword_type> || ...); | |
| 304 | } | ||
| 305 | }; | ||
| 306 | |||
| 307 | //==================================================================================================================== | ||
| 308 | //! @ingroup kwds | ||
| 309 | //! @related rbr::flag_keyword | ||
| 310 | //! @brief Create a flag keyword for reuse | ||
| 311 | //! @param id Unique rbr::id_ for the keyword being built | ||
| 312 | //! @return An instance of rbr::flag_keyword | ||
| 313 | //! ## Example: | ||
| 314 | //! @include doc/flag.cpp | ||
| 315 | //==================================================================================================================== | ||
| 316 | template<typename Tag> constexpr flag_keyword<Tag> flag([[maybe_unused]] Tag id) noexcept | ||
| 317 | { | ||
| 318 | return {}; | ||
| 319 | } | ||
| 320 | |||
| 321 | //==================================================================================================================== | ||
| 322 | //! @ingroup kwds | ||
| 323 | //! @related rbr::any_keyword | ||
| 324 | //! @brief Create a regular keyword for reuse | ||
| 325 | //! @param id Unique rbr::id_ for the keyword being built | ||
| 326 | //! @return An instance of rbr::any_keyword | ||
| 327 | //! ## Example: | ||
| 328 | //! @include doc/regular.cpp | ||
| 329 | //==================================================================================================================== | ||
| 330 | template<typename ID> constexpr any_keyword<ID> keyword([[maybe_unused]] ID id) noexcept | ||
| 331 | { | ||
| 332 | return {}; | ||
| 333 | } | ||
| 334 | |||
| 335 | //==================================================================================================================== | ||
| 336 | //! @ingroup kwds | ||
| 337 | //! @related rbr::checked_keyword | ||
| 338 | //! @brief Create a checked keyword for reuse | ||
| 339 | //! @tparam Checker Unary template meta-function to use for validation | ||
| 340 | //! @param id Unique rbr::id_ for the keyword being built | ||
| 341 | //! @return An instance of rbr::checked_keyword | ||
| 342 | //! ## Example: | ||
| 343 | //! @include doc/checked.cpp | ||
| 344 | //==================================================================================================================== | ||
| 345 | template<template<class> class Checker, typename ID> | ||
| 346 | constexpr checked_keyword<ID, Checker> keyword([[maybe_unused]] ID id) noexcept | ||
| 347 | { | ||
| 348 | return {}; | ||
| 349 | } | ||
| 350 | |||
| 351 | //==================================================================================================================== | ||
| 352 | //! @ingroup kwds | ||
| 353 | //! @related rbr::typed_keyword | ||
| 354 | //! @brief Create a typed Keyword for reuse | ||
| 355 | //! @tparam Type Type accepted by the keyword | ||
| 356 | //! @param id Unique rbr::id_ for the keyword being built | ||
| 357 | //! @return An instance of rbr::checked_keyword | ||
| 358 | //! ## Example: | ||
| 359 | //! @include doc/checked.cpp | ||
| 360 | //==================================================================================================================== | ||
| 361 | template<typename Type, typename ID> constexpr typed_keyword<ID, Type> keyword([[maybe_unused]] ID id) noexcept | ||
| 362 | { | ||
| 363 | return {}; | ||
| 364 | } | ||
| 365 | |||
| 366 | namespace literals | ||
| 367 | { | ||
| 368 | //================================================================================================================== | ||
| 369 | //! @ingroup udls | ||
| 370 | //! @related rbr::any_keyword | ||
| 371 | //! @brief Forms an instance of rbr::any_keyword from a literal string | ||
| 372 | //! @return An instance of rbr::any_keyword using the specified string as ID | ||
| 373 | //================================================================================================================== | ||
| 374 | 212 | template<str ID> constexpr auto operator""_kw() noexcept | |
| 375 | { | ||
| 376 | 212 | return any_keyword<id_<ID>>{}; | |
| 377 | } | ||
| 378 | |||
| 379 | //================================================================================================================== | ||
| 380 | //! @ingroup udls | ||
| 381 | //! @related rbr::flag_keyword | ||
| 382 | //! @brief Forms an instance of rbr::flag_keyword from a literal string | ||
| 383 | //! @return An instance of rbr::flag_keyword using the specified string as ID | ||
| 384 | //================================================================================================================== | ||
| 385 | 58 | template<str ID> constexpr auto operator""_fl() noexcept | |
| 386 | { | ||
| 387 | 58 | return flag_keyword<id_<ID>>{}; | |
| 388 | } | ||
| 389 | } | ||
| 390 | } | ||
| 391 |