aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/variant.h
blob: e261aa9af39eb31715d94f82a7887a4993e4e169 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

#include <variant>

namespace NUnifiedAgent {
    template<class... Ts> struct TOverloaded : Ts... { using Ts::operator()...; };
    template<class... Ts> TOverloaded(Ts...) -> TOverloaded<Ts...>;

    template <class T, class... U>
    auto Visit(T&& variant, U&&... visitorOverloads) {
        return std::visit(TOverloaded{std::forward<U>(visitorOverloads)...}, std::forward<T>(variant));
    }

    template <typename TTarget, typename... TSourceTypes>
    auto CastTo(std::variant<TSourceTypes...>&& variant) {
        return Visit(variant, [](auto& p) -> TTarget { return std::move(p); });
    }
}