blob: 7b2397755cc1bdd0ce60f662b9080d29291bc2b5 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
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 Feist0ffe7142018-11-28 15:25:53 -080018#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070019#include <string>
James Feist3eb82622019-02-08 13:10:22 -080020#include <variant>
James Feist6714a252018-09-10 15:26:18 -070021
James Feist0ffe7142018-11-28 15:25:53 -080022struct VariantToFloatVisitor
James Feist6714a252018-09-10 15:26:18 -070023{
James Feist0ffe7142018-11-28 15:25:53 -080024
James Feist6714a252018-09-10 15:26:18 -070025 template <typename T> float operator()(const T &t) const
26 {
James Feist0ffe7142018-11-28 15:25:53 -080027 if constexpr (std::is_arithmetic_v<T>)
28 {
29 return static_cast<float>(t);
30 }
31 throw std::invalid_argument("Cannot translate type to float");
James Feist6714a252018-09-10 15:26:18 -070032 }
33};
James Feist6714a252018-09-10 15:26:18 -070034
James Feist0ffe7142018-11-28 15:25:53 -080035struct VariantToIntVisitor
James Feist6714a252018-09-10 15:26:18 -070036{
37 template <typename T> int operator()(const T &t) const
38 {
James Feist0ffe7142018-11-28 15:25:53 -080039 if constexpr (std::is_arithmetic_v<T>)
40 {
41 return static_cast<int>(t);
42 }
43 throw std::invalid_argument("Cannot translate type to int");
James Feist6714a252018-09-10 15:26:18 -070044 }
45};
James Feist6714a252018-09-10 15:26:18 -070046
James Feist0ffe7142018-11-28 15:25:53 -080047struct VariantToUnsignedIntVisitor
James Feist6714a252018-09-10 15:26:18 -070048{
49 template <typename T> unsigned int operator()(const T &t) const
50 {
James Feist0ffe7142018-11-28 15:25:53 -080051 if constexpr (std::is_arithmetic_v<T>)
52 {
53 return static_cast<unsigned int>(t);
54 }
55 throw std::invalid_argument("Cannot translate type to unsigned int");
James Feist6714a252018-09-10 15:26:18 -070056 }
57};
James Feist6714a252018-09-10 15:26:18 -070058
James Feist0ffe7142018-11-28 15:25:53 -080059struct VariantToStringVisitor
James Feist6714a252018-09-10 15:26:18 -070060{
61 template <typename T> std::string operator()(const T &t) const
62 {
James Feist0ffe7142018-11-28 15:25:53 -080063 if constexpr (std::is_same_v<T, std::string>)
64 {
65 return t;
66 }
67 else if constexpr (std::is_arithmetic_v<T>)
68 {
69 return std::to_string(t);
70 }
71 throw std::invalid_argument("Cannot translate type to string");
James Feist6714a252018-09-10 15:26:18 -070072 }
73};
James Feist0ffe7142018-11-28 15:25:53 -080074
75struct VariantToDoubleVisitor
James Feist6714a252018-09-10 15:26:18 -070076{
James Feist0ffe7142018-11-28 15:25:53 -080077 template <typename T> double operator()(const T &t) const
78 {
79 if constexpr (std::is_arithmetic_v<T>)
80 {
81 return static_cast<double>(t);
82 }
83 throw std::invalid_argument("Cannot translate type to double");
84 }
85};