blob: 6549a321f3f24adad7c720fa81a8e80ed907c5fd [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 Feistd8705872019-02-08 13:26:09 -080025 template <typename T>
26 float operator()(const T& t) const
James Feist6714a252018-09-10 15:26:18 -070027 {
James Feist0ffe7142018-11-28 15:25:53 -080028 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 Feist6714a252018-09-10 15:26:18 -070033 }
34};
James Feist6714a252018-09-10 15:26:18 -070035
James Feist0ffe7142018-11-28 15:25:53 -080036struct VariantToIntVisitor
James Feist6714a252018-09-10 15:26:18 -070037{
James Feistd8705872019-02-08 13:26:09 -080038 template <typename T>
39 int operator()(const T& t) const
James Feist6714a252018-09-10 15:26:18 -070040 {
James Feist0ffe7142018-11-28 15:25:53 -080041 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 Feist6714a252018-09-10 15:26:18 -070046 }
47};
James Feist6714a252018-09-10 15:26:18 -070048
James Feist0ffe7142018-11-28 15:25:53 -080049struct VariantToUnsignedIntVisitor
James Feist6714a252018-09-10 15:26:18 -070050{
James Feistd8705872019-02-08 13:26:09 -080051 template <typename T>
52 unsigned int operator()(const T& t) const
James Feist6714a252018-09-10 15:26:18 -070053 {
James Feist0ffe7142018-11-28 15:25:53 -080054 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 Feist6714a252018-09-10 15:26:18 -070059 }
60};
James Feist6714a252018-09-10 15:26:18 -070061
James Feist0ffe7142018-11-28 15:25:53 -080062struct VariantToStringVisitor
James Feist6714a252018-09-10 15:26:18 -070063{
James Feistd8705872019-02-08 13:26:09 -080064 template <typename T>
65 std::string operator()(const T& t) const
James Feist6714a252018-09-10 15:26:18 -070066 {
James Feist0ffe7142018-11-28 15:25:53 -080067 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 Feist6714a252018-09-10 15:26:18 -070076 }
77};
James Feist0ffe7142018-11-28 15:25:53 -080078
79struct VariantToDoubleVisitor
James Feist6714a252018-09-10 15:26:18 -070080{
James Feistd8705872019-02-08 13:26:09 -080081 template <typename T>
82 double operator()(const T& t) const
James Feist0ffe7142018-11-28 15:25:53 -080083 {
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};