-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathmatch_variant.h
More file actions
40 lines (36 loc) · 1.53 KB
/
match_variant.h
File metadata and controls
40 lines (36 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Copyright (c) 2008-2025 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#pragma once
#include <variant>
// Utility for pattern-matching style over std::variant.
// - `overload` builds an overload set from multiple lambdas (or function objects).
// - `match_v` wraps std::visit, forwarding the variant and visitors.
// It returns whatever the selected lambda returns.
// Example:
// std::variant<int, std::string> v = 42;
// auto s = MR::match_v(v,
// [](int i) { return std::to_string(i); },
// [](const std::string& s) { return s; }); // check_syntax off
// // s == "42"
namespace MR {
template <class... Ts> struct overload : Ts... {
using Ts::operator()...;
};
template <class... Ts> overload(Ts...) -> overload<Ts...>;
template <class var_t, class... Func> decltype(auto) match_v(var_t &&variant, Func &&...funcs) {
return std::visit(overload{std::forward<Func>(funcs)...}, std::forward<var_t>(variant));
}
} // namespace MR