Sharg 1.2.3-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
enumeration_names.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026, Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026, Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <iostream>
13#include <string_view>
14#include <unordered_map>
15
16#include <sharg/platform.hpp>
17
18namespace sharg::custom
19{
20
46template <typename t>
47struct parsing
48{}; // forward
49
51template <typename t>
52struct parsing<t const> : parsing<t>
53{};
54
55template <typename t>
56struct parsing<t &> : parsing<t>
57{};
58
59template <typename t>
60struct parsing<t const &> : parsing<t>
61{};
63
64} // namespace sharg::custom
65
66namespace sharg::detail
67{
70template <size_t I>
71struct priority_tag
73 // Doxygen fail
74 : priority_tag<I - 1>
76{};
77
79template <>
80struct priority_tag<0>
81{};
82
83} // namespace sharg::detail
84
85namespace sharg::detail::adl_only
86{
87
89template <typename t>
90std::unordered_map<std::string_view, t> enumeration_names(t) = delete;
91
95template <typename option_t>
96struct enumeration_names_cpo
97{
101 constexpr enumeration_names_cpo() = default;
102 constexpr enumeration_names_cpo(enumeration_names_cpo &&) = default;
103 constexpr enumeration_names_cpo(enumeration_names_cpo const &) = default;
104 constexpr enumeration_names_cpo & operator=(enumeration_names_cpo &&) = default;
105 constexpr enumeration_names_cpo & operator=(enumeration_names_cpo const &) = default;
107
111 template <typename option_type>
112 using option_or_type_identity =
116
120 template <typename option_type = option_t>
121 static constexpr auto cpo_overload(sharg::detail::priority_tag<1>) noexcept(
124 {
126 }
127
137 template <typename option_type = option_t>
138 static constexpr auto cpo_overload(sharg::detail::priority_tag<0>) noexcept(noexcept(enumeration_names(
139 option_or_type_identity<option_type>{}))) -> decltype(enumeration_names(option_or_type_identity<option_type>{}))
140 {
141 return enumeration_names(option_or_type_identity<option_type>{});
142 }
143
155 template <typename... args_t, typename option_type = option_t /*circumvent incomplete types*/>
156 constexpr auto operator()(args_t &&... args) const
157 noexcept(noexcept(cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...)))
158 -> decltype(cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...))
159 {
160 return cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...);
161 }
162};
163
164} // namespace sharg::detail::adl_only
165
166namespace sharg
167{
168
210// clang-format off
211template <typename option_type>
212 requires requires { { detail::adl_only::enumeration_names_cpo<option_type>{}() }; }
213inline auto const enumeration_names = detail::adl_only::enumeration_names_cpo<option_type>{}();
214// clang-format on
216
232// clang-format off
233template <typename option_type>
234concept named_enumeration = requires
235{
236 { sharg::enumeration_names<option_type> };
237};
238// clang-format on
239} // namespace sharg
240
242namespace std
243{
244
249template <typename option_type>
251inline ostream & operator<<(ostream & s, option_type && op)
252{
253 for (auto & [key, value] : sharg::enumeration_names<option_type>)
254 {
255 if (op == value)
256 return s << key;
257 }
258
259 return s << "<UNKNOWN_VALUE>";
260}
261
262} // namespace std
Checks whether the free function sharg::enumeration_names can be called on the type.
Definition enumeration_names.hpp:234
auto const enumeration_names
Return a conversion map from std::string_view to option_type.
Definition enumeration_names.hpp:213
T is_same_v
Provides platform and dependency checks.
A type that can be specialised to provide customisation point implementations for the sharg::parser s...
Definition enumeration_names.hpp:48
Hide me