From 656adde4974783b552159178e9dbf2bc8d9f9746 Mon Sep 17 00:00:00 2001 From: Daljit Singh Date: Wed, 26 Nov 2025 10:59:45 +0000 Subject: [PATCH 1/3] New utility for pattern-matching style over std::variant --- cpp/core/match_variant.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 cpp/core/match_variant.h diff --git a/cpp/core/match_variant.h b/cpp/core/match_variant.h new file mode 100644 index 0000000000..c739e59201 --- /dev/null +++ b/cpp/core/match_variant.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +// 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 v = 42; +// auto s = MR::match_v(v, +// [](int i) { return std::to_string(i); }, +// [](const std::string& s) { return s; }); +// // s == "42" + +namespace MR { +template struct overload : Ts... { + using Ts::operator()...; +}; +template overload(Ts...) -> overload; +template decltype(auto) match_v(var_t &&variant, Func &&...funcs) { + return std::visit(overload{std::forward(funcs)...}, std::forward(variant)); +} +} // namespace MR From 5bb299355e42f93e459edf3bb495fabe93225d59 Mon Sep 17 00:00:00 2001 From: Daljit Singh Date: Wed, 26 Nov 2025 11:14:24 +0000 Subject: [PATCH 2/3] Add copyright --- cpp/core/match_variant.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cpp/core/match_variant.h b/cpp/core/match_variant.h index c739e59201..0eb8ba6908 100644 --- a/cpp/core/match_variant.h +++ b/cpp/core/match_variant.h @@ -1,3 +1,19 @@ +/* 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 From a1e03431908fec1d9df6f362d2b5a6cd8e760c41 Mon Sep 17 00:00:00 2001 From: Daljit Singh <129752264+daljit46@users.noreply.github.com> Date: Thu, 27 Nov 2025 08:50:39 +0000 Subject: [PATCH 3/3] Disable check_syntax in comment Co-authored-by: Robert Smith --- cpp/core/match_variant.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/core/match_variant.h b/cpp/core/match_variant.h index 0eb8ba6908..aa49c9e37a 100644 --- a/cpp/core/match_variant.h +++ b/cpp/core/match_variant.h @@ -26,7 +26,7 @@ // std::variant v = 42; // auto s = MR::match_v(v, // [](int i) { return std::to_string(i); }, -// [](const std::string& s) { return s; }); +// [](const std::string& s) { return s; }); // check_syntax off // // s == "42" namespace MR {