GCC Code Coverage Report


Directory: include/raberu/impl/
File: include/raberu/impl/settings.hpp
Date: 2026-11-21 10:39:38
Exec Total Coverage
Lines: 22 22 100.0%
Functions: 317 317 100.0%
Branches: 10 13 76.9%

Line Branch Exec Source
1 //======================================================================================================================
2 /*
3 RABERU - Fancy Parameters Library
4 Copyright : RABERU Project Contributors
5 SPDX-License-Identifier: BSL-1.0
6 */
7 //======================================================================================================================
8 #pragma once
9
10 #include <cstddef>
11 #include <ostream>
12 #include <type_traits>
13
14 //==================================================================================================
15 //! @ingroup main
16 //! @{
17 //! @defgroup stng Settings definitions and handling
18 //! @brief Functions and types to handle RABERU settings
19 //! @}
20 //==================================================================================================
21
22 namespace rbr
23 {
24 /// Type indicating that a [Keyword](@ref rbr::concepts::keyword) is not available
25 struct unknown_key
26 {
27 using type = unknown_key;
28 };
29
30 // Option calls aggregator
31 template<concepts::option... Ts> struct aggregator : Ts...
32 {
33 154 constexpr aggregator(Ts const&... t) noexcept : Ts(t)... {}
34 using Ts::operator()...;
35
36 template<concepts::keyword K> constexpr auto operator()(K const&) const noexcept { return unknown_key{}; }
37 };
38
39 //================================================================================================
40 //! @ingroup stng
41 //! @brief Defines a group of options for processing
42 //!
43 //! rbr::settings acts as an aggregation of [Options](@ref rbr::concepts::option) (ie pair of
44 //! [Keyword](@ref rbr::concepts::keyword)/value) that provides an user-interface for accessing
45 //! or managing said [Options](@ref rbr::concepts::option) and to gather informations.
46 //!
47 //! @tparam Opts List of [options](@ref rbr::concepts::option) aggregated
48 //================================================================================================
49 template<concepts::option... Opts> struct settings
50 {
51 using rbr_settings = void;
52 using base = aggregator<Opts...>;
53
54 /// Constructor from a variadic pack of rbr::concepts::option
55 154 constexpr settings(Opts const&... opts) : content_(opts...) {}
56
57 /// Number of options in current rbr::settings
58 34 static constexpr std::ptrdiff_t size() noexcept { return sizeof...(Opts); }
59
60 //==============================================================================================
61 //! @brief Checks if a given rbr::keyword is stored inside rbr::settings
62 //! @param kw Keyword to check
63 //! @return An instance of `std::true_type` if current setting contains an option based on `kw`.
64 //! Otherwise, return an instance of `std::false_type`.
65 //! ## Example:
66 //! @include doc/contains.cpp
67 //==============================================================================================
68 161 template<concepts::keyword Key> static constexpr auto contains([[maybe_unused]] Key const& kw) noexcept
69 {
70 using found = decltype((std::declval<base>())(Key{}));
71 161 return !stdfix::same_as<found, unknown_key>;
72 }
73
74 //==============================================================================================
75 //! @brief Checks if rbr::settings contains at least one of maybe keyword
76 //! @param ks Keywords to check
77 //! @return An instance of `std::true_type` if current setting contains at least one option
78 //! based on any of the `ks`. Otherwise, return an instance of `std::false_type`.
79 //! ## Example:
80 //! @include doc/contains_any.cpp
81 //==============================================================================================
82 8 template<concepts::keyword... Keys> static constexpr auto contains_any(Keys... ks) noexcept
83 {
84
3/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
8 return (contains(ks) || ...);
85 }
86
87 //==============================================================================================
88 //! @brief Checks if rbr::settings contains options based only on selected keywords
89 //! @param ks Keywords to check
90 //! @return An instance of `std::true_type` if current setting contains only options
91 //! based on any of the `ks`. Otherwise, return an instance of `std::false_type`.
92 //! ## Example:
93 //! @include doc/contains_only.cpp
94 //==============================================================================================
95 14 template<concepts::keyword... Keys> static constexpr auto contains_only([[maybe_unused]] Keys const&... ks) noexcept
96 {
97 using current_keys = _::keys<typename Opts::keyword_type...>;
98 using acceptable_keys = _::keys<Keys...>;
99 using unique_set = typename _::uniques<current_keys, acceptable_keys>::type;
100 14 return _::is_equivalent<unique_set, acceptable_keys>::value;
101 }
102
103 //==============================================================================================
104 //! @brief Checks if rbr::settings contains no options based on any of the selected keywords
105 //! @param ks Keywords to check
106 //! @return An instance of `std::true_type` if current setting contains no options
107 //! based on any of the `ks`. Otherwise, return an instance of `std::false_type`.
108 //! ## Example:
109 //! @include doc/contains_none.cpp
110 //==============================================================================================
111 4 template<concepts::keyword... Keys> static constexpr auto contains_none(Keys... ks) noexcept
112 {
113 4 return !contains_any(ks...);
114 }
115
116 //==============================================================================================
117 //! @brief Retrieved a value via a keyword
118 //!
119 //! Retrieve the value bound to a given keyword `k` inside current rbr::settings instance.
120 //! If such a keyword is not present, either an instance of rbr::unknown_key is returned or
121 //! a default value or function call will be returned.
122 //!
123 //! @param k Keywords to check
124 //! @return If any, the value bound to `k`.
125 //! ## Example:
126 //! @include doc/subscript.cpp
127 //==============================================================================================
128 229 template<concepts::keyword Key> constexpr auto operator[](Key const& k) const noexcept { return content_(k); }
129
130 //! @overload
131 84 template<typename Keyword> constexpr auto operator[](flag_keyword<Keyword> const&) const noexcept
132 {
133 84 return contains(flag_keyword<Keyword>{});
134 }
135
136 //! @overload
137 51 template<concepts::keyword Key, typename Value> constexpr auto operator[](_::type_or_<Key, Value> const& tgt) const
138 {
139 20 if constexpr (contains(Key{})) return (*this)[Key{}];
140 14 else if constexpr (requires(Value t) { t.perform(); }) return tgt.value.perform();
141 17 else return tgt.value;
142 }
143
144 //! @related rbr::settings
145 //! @brief Output stream insertion
146 13 friend std::ostream& operator<<(std::ostream& os, settings const& s)
147 {
148 23 auto show = [&]<typename T, typename V>(T t, V const& v) -> std::ostream& { return t.show(os, v) << "\n"; };
149
150
7/7
✓ Branch 2 taken 9 times.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 2 times.
✓ Branch 8 taken 1 times.
✓ Branch 12 taken 2 times.
✓ Branch 14 taken 1 times.
✓ Branch 18 taken 1 times.
13 (show(typename Opts::keyword_type{}, s[typename Opts::keyword_type{}]), ...);
151
152 13 return os;
153 }
154
155 base content_;
156 };
157
158 /// rbr::settings deduction guide
159 template<concepts::option... Opts> settings(Opts const&... opts) -> settings<Opts...>;
160 }
161