blob: 87991a4556f4b34ca5e39c671656cabf5fd4d63e [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*/
16
17#pragma once
James Feist1103e9e2019-06-06 11:26:36 -070018#include <stdexcept>
James Feist8f2710a2018-05-09 17:18:55 -070019#include <string>
James Feist1103e9e2019-06-06 11:26:36 -070020#include <variant>
James Feist3cb5fec2018-01-23 14:41:51 -080021
James Feist8f2710a2018-05-09 17:18:55 -070022struct VariantToFloatVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080023{
James Feist1103e9e2019-06-06 11:26:36 -070024
James Feista465ccc2019-02-08 12:51:01 -080025 template <typename T>
26 float 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<float>(t);
31 }
32 throw std::invalid_argument("Cannot translate type to float");
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 VariantToIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080037{
James Feista465ccc2019-02-08 12:51:01 -080038 template <typename T>
39 int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080040 {
James Feist1103e9e2019-06-06 11:26:36 -070041 if constexpr (std::is_arithmetic_v<T>)
42 {
43 return static_cast<int>(t);
44 }
45 throw std::invalid_argument("Cannot translate type to int");
James Feist3cb5fec2018-01-23 14:41:51 -080046 }
47};
James Feist3cb5fec2018-01-23 14:41:51 -080048
James Feist8f2710a2018-05-09 17:18:55 -070049struct VariantToUnsignedIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080050{
James Feista465ccc2019-02-08 12:51:01 -080051 template <typename T>
52 unsigned int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080053 {
James Feist1103e9e2019-06-06 11:26:36 -070054 if constexpr (std::is_arithmetic_v<T>)
55 {
56 return static_cast<unsigned int>(t);
57 }
58 throw std::invalid_argument("Cannot translate type to unsigned int");
James Feist3cb5fec2018-01-23 14:41:51 -080059 }
60};
James Feist8f2710a2018-05-09 17:18:55 -070061
62struct VariantToStringVisitor
63{
James Feista465ccc2019-02-08 12:51:01 -080064 template <typename T>
65 std::string operator()(const T& t) const
James Feist8f2710a2018-05-09 17:18:55 -070066 {
James Feist1103e9e2019-06-06 11:26:36 -070067 if constexpr (std::is_same_v<T, std::string>)
68 {
69 return t;
70 }
71 else if constexpr (std::is_arithmetic_v<T>)
72 {
73 return std::to_string(t);
74 }
75 throw std::invalid_argument("Cannot translate type to string");
James Feist8f2710a2018-05-09 17:18:55 -070076 }
77};
James Feist1103e9e2019-06-06 11:26:36 -070078
79struct VariantToDoubleVisitor
James Feist8f2710a2018-05-09 17:18:55 -070080{
James Feist1103e9e2019-06-06 11:26:36 -070081 template <typename T>
82 double operator()(const T& t) const
83 {
84 if constexpr (std::is_arithmetic_v<T>)
85 {
86 return static_cast<double>(t);
87 }
88 throw std::invalid_argument("Cannot translate type to double");
89 }
90};