blob: 5aa530155ef3fb0e52d636dd8a127d40abea517b [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
James Feist3cb5fec2018-01-23 14:41:51 -08003
4#pragma once
James Feist1103e9e2019-06-06 11:26:36 -07005#include <stdexcept>
James Feist8f2710a2018-05-09 17:18:55 -07006#include <string>
James Feist1103e9e2019-06-06 11:26:36 -07007#include <variant>
James Feist3cb5fec2018-01-23 14:41:51 -08008
James Feist8f2710a2018-05-09 17:18:55 -07009struct VariantToIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080010{
James Feista465ccc2019-02-08 12:51:01 -080011 template <typename T>
12 int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080013 {
James Feist1103e9e2019-06-06 11:26:36 -070014 if constexpr (std::is_arithmetic_v<T>)
15 {
16 return static_cast<int>(t);
17 }
18 throw std::invalid_argument("Cannot translate type to int");
James Feist3cb5fec2018-01-23 14:41:51 -080019 }
20};
James Feist3cb5fec2018-01-23 14:41:51 -080021
James Feist8f2710a2018-05-09 17:18:55 -070022struct VariantToStringVisitor
23{
James Feista465ccc2019-02-08 12:51:01 -080024 template <typename T>
25 std::string operator()(const T& t) const
James Feist8f2710a2018-05-09 17:18:55 -070026 {
James Feist1103e9e2019-06-06 11:26:36 -070027 if constexpr (std::is_same_v<T, std::string>)
28 {
29 return t;
30 }
31 else if constexpr (std::is_arithmetic_v<T>)
32 {
33 return std::to_string(t);
34 }
35 throw std::invalid_argument("Cannot translate type to string");
James Feist8f2710a2018-05-09 17:18:55 -070036 }
37};