Sharg 1.1.2-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-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
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(
139 noexcept(enumeration_names(option_or_type_identity<option_type>{})))
140 -> decltype(enumeration_names(option_or_type_identity<option_type>{}))
141 {
142 return enumeration_names(option_or_type_identity<option_type>{});
143 }
144
156 template <typename... args_t, typename option_type = option_t /*circumvent incomplete types*/>
157 constexpr auto operator()(args_t &&... args) const
158 noexcept(noexcept(cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...)))
159 -> decltype(cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...))
160 {
161 return cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...);
162 }
163};
164
165} // namespace sharg::detail::adl_only
166
167namespace sharg
168{
169
211// clang-format off
212template <typename option_type>
213 requires requires { { detail::adl_only::enumeration_names_cpo<option_type>{}() }; }
214inline auto const enumeration_names = detail::adl_only::enumeration_names_cpo<option_type>{}();
215// clang-format on
217
233// clang-format off
234template <typename option_type>
235concept named_enumeration = requires
236{
237 { sharg::enumeration_names<option_type> };
238};
239// clang-format on
240} // namespace sharg
241
243namespace std
244{
245
250template <typename option_type>
252inline ostream & operator<<(ostream & s, option_type && op)
253{
254 for (auto & [key, value] : sharg::enumeration_names<option_type>)
255 {
256 if (op == value)
257 return s << key;
258 }
259
260 return s << "<UNKNOWN_VALUE>";
261}
262
263} // namespace std
Checks whether the free function sharg::enumeration_names can be called on the type.
Definition enumeration_names.hpp:235
auto const enumeration_names
Return a conversion map from std::string_view to option_type.
Definition enumeration_names.hpp:214
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