Skip to content

Commit 5547cdf

Browse files
committed
[MISC] Add poison_config
1 parent fc521fe commit 5547cdf

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

include/sharg/parser.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sharg/detail/format_man.hpp>
1919
#include <sharg/detail/format_parse.hpp>
2020
#include <sharg/detail/format_tdl.hpp>
21+
#include <sharg/detail/poison_config.hpp>
2122
#include <sharg/detail/version_check.hpp>
2223

2324
namespace sharg
@@ -254,6 +255,15 @@ class parser
254255
format);
255256
}
256257

258+
//!\cond DEV
259+
//!\brief A poison overload that catches calls to add_option without explicitly passing a sharg::config.
260+
template <typename option_type>
261+
void add_option(option_type &, detail::poison_config const &)
262+
{
263+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
264+
}
265+
//!\endcond
266+
257267
/*!\brief Adds a flag to the sharg::parser.
258268
*
259269
* \param[in, out] value The variable which shows if the flag is turned off (default) or on.
@@ -283,6 +293,15 @@ class parser
283293
format);
284294
}
285295

296+
//!\cond DEV
297+
//!\brief A poison overload that catches calls to add_flag without explicitly passing a sharg::config.
298+
template <typename option_type> // Template needed to prevent instantiation of this function if unused.
299+
void add_flag(option_type &, detail::poison_config const &)
300+
{
301+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
302+
}
303+
//!\endcond
304+
286305
/*!\brief Adds a positional option to the sharg::parser.
287306
*
288307
* \tparam option_type Must have a formatted input function (stream >> value).
@@ -324,6 +343,16 @@ class parser
324343
},
325344
format);
326345
}
346+
347+
//!\cond DEV
348+
//!\brief A poison overload that catches calls to add_positional_option without explicitly passing a sharg::config.
349+
template <typename option_type>
350+
void add_positional_option(option_type &, detail::poison_config const &)
351+
{
352+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
353+
}
354+
//!\endcond
355+
327356
//!\}
328357

329358
/*!\brief Initiates the actual command line parsing.

0 commit comments

Comments
 (0)