sharg 1.0.0
THE argument parser for bio-c++ tools.
enumeration_names.hpp
Go to the documentation of this file.
1// --------------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, 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
13#pragma once
14
15#include <iostream>
16#include <string_view>
17#include <unordered_map>
18
19#include <sharg/platform.hpp>
20
21namespace sharg::custom
22{
23
49template <typename t>
50struct parsing
51{}; // forward
52
54template <typename t>
55struct parsing<t const> : parsing<t>
56{};
57
58template <typename t>
59struct parsing<t &> : parsing<t>
60{};
61
62template <typename t>
63struct parsing<t const &> : parsing<t>
64{};
66
67} // namespace sharg::custom
68
69namespace sharg::detail
70{
73template <size_t I>
74struct priority_tag
76 // Doxygen fail
77 : priority_tag<I - 1>
79{};
80
82template <>
83struct priority_tag<0>
84{};
85
86} // namespace sharg::detail
87
88namespace sharg::detail::adl_only
89{
90
92template <typename t>
94
98template <typename option_t>
99struct enumeration_names_cpo
100{
104 constexpr enumeration_names_cpo() = default;
105 constexpr enumeration_names_cpo(enumeration_names_cpo &&) = default;
106 constexpr enumeration_names_cpo(enumeration_names_cpo const &) = default;
107 constexpr enumeration_names_cpo & operator=(enumeration_names_cpo &&) = default;
108 constexpr enumeration_names_cpo & operator=(enumeration_names_cpo const &) = default;
110
114 template <typename option_type>
115 using option_or_type_identity =
119
123 template <typename option_type = option_t>
124 static constexpr auto cpo_overload(sharg::detail::priority_tag<1>) noexcept(
127 {
129 }
130
140 template <typename option_type = option_t>
141 static constexpr auto cpo_overload(sharg::detail::priority_tag<0>) noexcept(
142 noexcept(enumeration_names(option_or_type_identity<option_type>{})))
143 -> decltype(enumeration_names(option_or_type_identity<option_type>{}))
144 {
145 return enumeration_names(option_or_type_identity<option_type>{});
146 }
147
159 template <typename... args_t, typename option_type = option_t /*circumvent incomplete types*/>
160 constexpr auto operator()(args_t &&... args) const
161 noexcept(noexcept(cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...)))
162 -> decltype(cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...))
163 {
164 return cpo_overload(sharg::detail::priority_tag<1>{}, std::forward<args_t>(args)...);
165 }
166};
167
168} // namespace sharg::detail::adl_only
169
170namespace sharg
171{
172
214// clang-format off
215template <typename option_type>
216 requires requires { { detail::adl_only::enumeration_names_cpo<option_type>{}() }; }
217inline auto const enumeration_names = detail::adl_only::enumeration_names_cpo<option_type>{}();
218// clang-format on
220
236// clang-format off
237template <typename option_type>
238concept named_enumeration = requires
239{
240 { sharg::enumeration_names<option_type> };
241};
242// clang-format on
243} // namespace sharg
244
246
250template <typename option_type>
252inline std::ostream & std::operator<<(std::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}
Checks whether the free function sharg::enumeration_names can be called on the type.
Definition: enumeration_names.hpp:238
auto const enumeration_names
Return a conversion map from std::string_view to option_type.
Definition: enumeration_names.hpp:217
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:51