|
| 1 | +// SPDX-FileCopyrightText: 2006-2024, Knut Reinert & Freie Universität Berlin |
| 2 | +// SPDX-FileCopyrightText: 2016-2024, Knut Reinert & MPI für molekulare Genetik |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +/*!\file |
| 6 | + * \author Enrico Seiler <enrico.seiler AT fu-berlin.de> |
| 7 | + * \brief Provides sharg::detail::poison_config. |
| 8 | + */ |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <sharg/config.hpp> |
| 13 | + |
| 14 | +namespace sharg::detail |
| 15 | +{ |
| 16 | + |
| 17 | +/*!\brief This struct is used to prevent the user from calling sharg::parser::add_option, sharg::parser::add_flag, |
| 18 | + * and sharg::parser::add_positional_option without a sharg::config. |
| 19 | + * \ingroup parser |
| 20 | + * \details |
| 21 | + * This prevents (and produces a legible error message) for calls like: |
| 22 | + * ``` |
| 23 | + * parser.add_option(value, {.short_id = 'i', .long_id = "int", .description = "Desc."}); |
| 24 | + * ``` |
| 25 | + * instead of |
| 26 | + * ``` |
| 27 | + * parser.add_option(value, sharg::config{.short_id = 'i', .long_id = "int", .description = "Desc."}); |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +struct poison_config |
| 31 | +{ |
| 32 | + char short_id{'\0'}; //!< Same as sharg::config::short_id. |
| 33 | + std::string long_id{}; //!< Same as sharg::config::long_id. |
| 34 | + std::string description{}; //!< Same as sharg::config::description. |
| 35 | + std::string default_message{}; //!< Same as sharg::config::default_message. |
| 36 | + bool advanced{false}; //!< Same as sharg::config::advanced. |
| 37 | + bool hidden{false}; //!< Same as sharg::config::hidden. |
| 38 | + bool required{false}; //!< Same as sharg::config::required. |
| 39 | + std::any validator{}; //!< Prevents CTAD inside a function call, which would cause a compiler error. |
| 40 | +}; |
| 41 | + |
| 42 | +/*!\brief A validator used for comparing the size of sharg::config and sharg::poison_config. |
| 43 | + * \ingroup parser |
| 44 | + * \details |
| 45 | + * The `sizeof(std::any)` is typically `16`, while `sizeof(sharg::detail::default_validator)` is `1`. |
| 46 | + * `poison_config_size_comp_validator` provides a validator whose size is the same as the size of `std::any`. |
| 47 | + */ |
| 48 | +struct poison_config_size_comp_validator : public detail::default_validator |
| 49 | +{ |
| 50 | + std::any validator{}; //!< A member such that the sizes of sharg::config and sharg::poison_config are the same. |
| 51 | +}; |
| 52 | + |
| 53 | +/*!\concept sharg::detail::poison_config_valid |
| 54 | + * \ingroup parser |
| 55 | + * \brief Concept that checks that sharg::poison_config has the same members as sharg::config. |
| 56 | + * \tparam validator_t The validator to use. Defaults to sharg::detail::poison_config_size_comp_validator. |
| 57 | + * \details |
| 58 | + * * Sizes of sharg::config and sharg::detail::poison_config are the same. |
| 59 | + * * sharg::config and sharg::detail::poison_config have the same member types (except validator). |
| 60 | + * * sharg::detail::poison_config can be constructed with designated initializers from sharg::config's members. |
| 61 | + * * sharg::config can be constructed with designated initializers from sharg::detail::poison_config's members. |
| 62 | + * The latter two ensure that the order of the members is the same. |
| 63 | + */ |
| 64 | +template <typename validator_t = poison_config_size_comp_validator> |
| 65 | +concept poison_config_valid = |
| 66 | + (sizeof(poison_config) == sizeof(config<validator_t>)) |
| 67 | + && std::same_as<decltype(poison_config{}.short_id), decltype(config<validator_t>{}.short_id)> |
| 68 | + && std::same_as<decltype(poison_config{}.long_id), decltype(config<validator_t>{}.long_id)> |
| 69 | + && std::same_as<decltype(poison_config{}.description), decltype(config<validator_t>{}.description)> |
| 70 | + && std::same_as<decltype(poison_config{}.default_message), decltype(config<validator_t>{}.default_message)> |
| 71 | + && std::same_as<decltype(poison_config{}.advanced), decltype(config<validator_t>{}.advanced)> |
| 72 | + && std::same_as<decltype(poison_config{}.hidden), decltype(config<validator_t>{}.hidden)> |
| 73 | + && std::same_as<decltype(poison_config{}.required), decltype(config<validator_t>{}.required)> |
| 74 | + && requires (config<validator_t> cfg, poison_config poison_cfg) { |
| 75 | + { |
| 76 | + poison_config{.short_id = cfg.short_id, |
| 77 | + .long_id = cfg.long_id, |
| 78 | + .description = cfg.description, |
| 79 | + .default_message = cfg.default_message, |
| 80 | + .advanced = cfg.advanced, |
| 81 | + .hidden = cfg.hidden, |
| 82 | + .required = cfg.required, |
| 83 | + .validator = cfg.validator} |
| 84 | + }; |
| 85 | + { |
| 86 | + config<validator_t>{.short_id = poison_cfg.short_id, |
| 87 | + .long_id = poison_cfg.long_id, |
| 88 | + .description = poison_cfg.description, |
| 89 | + .default_message = poison_cfg.default_message, |
| 90 | + .advanced = poison_cfg.advanced, |
| 91 | + .hidden = poison_cfg.hidden, |
| 92 | + .required = poison_cfg.required, |
| 93 | + .validator = std::any_cast<validator_t>(poison_cfg.validator)} |
| 94 | + }; |
| 95 | + }; |
| 96 | + |
| 97 | +static_assert(poison_config_valid<>, "sharg::detail::poison_config must have the same members as sharg::config!"); |
| 98 | + |
| 99 | +/*!\brief This is a workaround for compilers that do not implement CWG2518 (GCC 11, GCC 12). |
| 100 | + * \ingroup parser |
| 101 | + * \sa https://en.cppreference.com/w/cpp/language/if#Constexpr_if |
| 102 | + * \sa https://cplusplus.github.io/CWG/issues/2518.html |
| 103 | + * \details |
| 104 | + * Before CWG2518, a (discarded) statement couldn't be false in every case, e.g. |
| 105 | + * ```cpp |
| 106 | + * template <typename option_type> |
| 107 | + * void add_option(option_type) |
| 108 | + * { |
| 109 | + * if constexpr (std::is_same_v<option_type, int>) |
| 110 | + * { |
| 111 | + * return; |
| 112 | + * } |
| 113 | + * else |
| 114 | + * { |
| 115 | + * static_assert(false, "Should never happen"); // invalid before CWG2518 |
| 116 | + * static_assert(dependent_false_v<option_type>, "Should never happen"); // valid |
| 117 | + * } |
| 118 | + * } |
| 119 | + * ``` |
| 120 | + */ |
| 121 | +template <typename> |
| 122 | +inline constexpr bool dependent_false_v = false; |
| 123 | + |
| 124 | +} // namespace sharg::detail |
0 commit comments