blob: 87ae75090290afa369925042f93fa4ee8774a263 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
James Feist1103e9e2019-06-06 11:26:36 -07002// Copyright (c) 2019 Intel Corporation
James Feist3cb5fec2018-01-23 14:41:51 -08003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file variant_visitors.hpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
18#pragma once
James Feist1103e9e2019-06-06 11:26:36 -070019#include <stdexcept>
James Feist8f2710a2018-05-09 17:18:55 -070020#include <string>
James Feist1103e9e2019-06-06 11:26:36 -070021#include <variant>
James Feist3cb5fec2018-01-23 14:41:51 -080022
James Feist8f2710a2018-05-09 17:18:55 -070023struct VariantToIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080024{
James Feista465ccc2019-02-08 12:51:01 -080025 template <typename T>
26 int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080027 {
James Feist1103e9e2019-06-06 11:26:36 -070028 if constexpr (std::is_arithmetic_v<T>)
29 {
30 return static_cast<int>(t);
31 }
32 throw std::invalid_argument("Cannot translate type to int");
James Feist3cb5fec2018-01-23 14:41:51 -080033 }
34};
James Feist3cb5fec2018-01-23 14:41:51 -080035
James Feist8f2710a2018-05-09 17:18:55 -070036struct VariantToStringVisitor
37{
James Feista465ccc2019-02-08 12:51:01 -080038 template <typename T>
39 std::string operator()(const T& t) const
James Feist8f2710a2018-05-09 17:18:55 -070040 {
James Feist1103e9e2019-06-06 11:26:36 -070041 if constexpr (std::is_same_v<T, std::string>)
42 {
43 return t;
44 }
45 else if constexpr (std::is_arithmetic_v<T>)
46 {
47 return std::to_string(t);
48 }
49 throw std::invalid_argument("Cannot translate type to string");
James Feist8f2710a2018-05-09 17:18:55 -070050 }
51};