|
| 1 | +// -------------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin |
| 3 | +// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik |
| 4 | +// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License |
| 5 | +// shipped with this file and also available at: https://github.com/seqan/sharg-parser/blob/main/LICENSE.md |
| 6 | +// -------------------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | +/*!\file |
| 9 | + * \author Enrico Seiler <enrico.seiler AT fu-berlin.de> |
| 10 | + * \brief Provides sharg::detail::poison_config. |
| 11 | + */ |
| 12 | + |
| 13 | +#pragma once |
| 14 | + |
| 15 | +#include <sharg/config.hpp> |
| 16 | + |
| 17 | +namespace sharg::detail |
| 18 | +{ |
| 19 | + |
| 20 | +/*!\brief This struct is used to prevent the user from calling sharg::parser::add_option, sharg::parser::add_flag, |
| 21 | + * and sharg::parser::add_positional_option without a sharg::config. |
| 22 | + * \ingroup parser |
| 23 | + * \details |
| 24 | + * This prevents (and produces a legible error message) for calls like: |
| 25 | + * ``` |
| 26 | + * parser.add_option(value, {.short_id = 'i', .long_id = "int", .description = "Desc."}); |
| 27 | + * ``` |
| 28 | + * instead of |
| 29 | + * ``` |
| 30 | + * parser.add_option(value, sharg::config{.short_id = 'i', .long_id = "int", .description = "Desc."}); |
| 31 | + * ``` |
| 32 | + */ |
| 33 | +struct poison_config |
| 34 | +{ |
| 35 | + char short_id{'\0'}; //!< Same as sharg::config::short_id. |
| 36 | + std::string long_id{}; //!< Same as sharg::config::long_id. |
| 37 | + std::string description{}; //!< Same as sharg::config::description. |
| 38 | + std::string default_message{}; //!< Same as sharg::config::default_message. |
| 39 | + bool advanced{false}; //!< Same as sharg::config::advanced. |
| 40 | + bool hidden{false}; //!< Same as sharg::config::hidden. |
| 41 | + bool required{false}; //!< Same as sharg::config::required. |
| 42 | + std::any validator{}; //!< Prevents CTAD inside a function call, which would cause a compiler error. |
| 43 | +}; |
| 44 | + |
| 45 | +/*!\brief A validator used for comparing the size of sharg::config and sharg::poison_config. |
| 46 | + * \ingroup parser |
| 47 | + * \details |
| 48 | + * The `sizeof(std::any)` is typically `16`, while `sizeof(sharg::detail::default_validator)` is `1`. |
| 49 | + * `poison_config_size_comp_validator` provides a validator whose size is the same as the size of `std::any`. |
| 50 | + */ |
| 51 | +struct poison_config_size_comp_validator : public detail::default_validator |
| 52 | +{ |
| 53 | + std::any validator{}; //!< A member such that the sizes of sharg::config and sharg::poison_config are the same. |
| 54 | +}; |
| 55 | + |
| 56 | +/*!\concept sharg::detail::poison_config_valid |
| 57 | + * \ingroup parser |
| 58 | + * \brief Concept that checks that sharg::poison_config has the same members as sharg::config. |
| 59 | + * \tparam validator_t The validator to use. Defaults to sharg::detail::poison_config_size_comp_validator. |
| 60 | + * \details |
| 61 | + * * Sizes of sharg::config and sharg::detail::poison_config are the same. |
| 62 | + * * sharg::detail::poison_config can be constructed with designated initializers from sharg::config's members. |
| 63 | + * * sharg::config can be constructed with designated initializers from sharg::detail::poison_config's members. |
| 64 | + */ |
| 65 | +template <typename validator_t = poison_config_size_comp_validator> |
| 66 | +concept poison_config_valid = (sizeof(poison_config) == sizeof(config<validator_t>)) |
| 67 | + && requires (config<validator_t> cfg, poison_config poison_cfg) { |
| 68 | + { |
| 69 | + poison_config{.short_id = cfg.short_id, |
| 70 | + .long_id = cfg.long_id, |
| 71 | + .description = cfg.description, |
| 72 | + .default_message = cfg.default_message, |
| 73 | + .advanced = cfg.advanced, |
| 74 | + .hidden = cfg.hidden, |
| 75 | + .required = cfg.required, |
| 76 | + .validator = cfg.validator} |
| 77 | + }; |
| 78 | + { |
| 79 | + config<validator_t>{.short_id = poison_cfg.short_id, |
| 80 | + .long_id = poison_cfg.long_id, |
| 81 | + .description = poison_cfg.description, |
| 82 | + .default_message = poison_cfg.default_message, |
| 83 | + .advanced = poison_cfg.advanced, |
| 84 | + .hidden = poison_cfg.hidden, |
| 85 | + .required = poison_cfg.required, |
| 86 | + .validator = std::any_cast<validator_t>(poison_cfg.validator)} |
| 87 | + }; |
| 88 | + }; |
| 89 | + |
| 90 | +static_assert(poison_config_valid<>, "sharg::detail::poison_config must have the same members as sharg::config!"); |
| 91 | + |
| 92 | +} // namespace sharg::detail |
0 commit comments