GCC Code Coverage Report


Directory: include/raberu/impl/
File: include/raberu/impl/algo/merge.hpp
Date: 2026-11-21 10:39:38
Exec Total Coverage
Lines: 7 7 100.0%
Functions: 10 10 100.0%
Branches: 0 0 -%

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 namespace rbr
11 {
12 //================================================================================================
13 //! @ingroup stng
14 //! @related rbr::settings
15 //! @brief Merge two instances of rbr::settings
16 //!
17 //! Merge all options of `opts` and `defs`. If an options is present in both arguments, the one
18 //! from `opts` is used.
19 //!
20 //! @param opts rbr::settings to merge
21 //! @param defs rbr::settings acting as default value
22 //! @return An instance of rbr::settings containing all options from `opts` and any options
23 //! from `defs` not present in `opts`.
24 //! ## Example:
25 //! @include doc/merge.cpp
26 //================================================================================================
27 template<concepts::option... K1s, concepts::option... K2s>
28 9 constexpr auto merge(settings<K1s...> const& opts, settings<K2s...> const& defs) noexcept
29 {
30 22 auto selector = []<typename K, typename Opts>(K const&, Opts const& o, auto const& d) {
31 constexpr K key;
32 10 if constexpr (Opts::contains(key)) return (key = o[key]);
33 8 else return (key = d[key]);
34 };
35
36 14 auto select = [&]<typename... Ks>(_::keys<Ks...> const&, auto const& os, auto const& ds) {
37 5 return settings(selector(Ks{}, os, ds)...);
38 };
39
40 return select(
41 typename _::uniques<_::keys<typename K1s::keyword_type...>, _::keys<typename K2s::keyword_type...>>::type{}, opts,
42 9 defs);
43 }
44
45 namespace result
46 {
47 template<concepts::settings S1, concepts::settings S2> struct merge
48 {
49 using type = decltype(rbr::merge(std::declval<S1>(), std::declval<S2>()));
50 };
51
52 template<concepts::settings S1, concepts::settings S2> using merge_t = typename merge<S1, S2>::type;
53 }
54 }
55