blob: 7a8677f78004ff53f8ac1f0d6fbdd428adb27dc6 [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>
20
James Feist0ffe7142018-11-28 15:25:53 -080021struct VariantToFloatVisitor
James Feist6714a252018-09-10 15:26:18 -070022{
James Feist0ffe7142018-11-28 15:25:53 -080023
James Feist6714a252018-09-10 15:26:18 -070024 template <typename T> float operator()(const T &t) const
25 {
James Feist0ffe7142018-11-28 15:25:53 -080026 if constexpr (std::is_arithmetic_v<T>)
27 {
28 return static_cast<float>(t);
29 }
30 throw std::invalid_argument("Cannot translate type to float");
James Feist6714a252018-09-10 15:26:18 -070031 }
32};
James Feist6714a252018-09-10 15:26:18 -070033
James Feist0ffe7142018-11-28 15:25:53 -080034struct VariantToIntVisitor
James Feist6714a252018-09-10 15:26:18 -070035{
36 template <typename T> int operator()(const T &t) const
37 {
James Feist0ffe7142018-11-28 15:25:53 -080038 if constexpr (std::is_arithmetic_v<T>)
39 {
40 return static_cast<int>(t);
41 }
42 throw std::invalid_argument("Cannot translate type to int");
James Feist6714a252018-09-10 15:26:18 -070043 }
44};
James Feist6714a252018-09-10 15:26:18 -070045
James Feist0ffe7142018-11-28 15:25:53 -080046struct VariantToUnsignedIntVisitor
James Feist6714a252018-09-10 15:26:18 -070047{
48 template <typename T> unsigned int operator()(const T &t) const
49 {
James Feist0ffe7142018-11-28 15:25:53 -080050 if constexpr (std::is_arithmetic_v<T>)
51 {
52 return static_cast<unsigned int>(t);
53 }
54 throw std::invalid_argument("Cannot translate type to unsigned int");
James Feist6714a252018-09-10 15:26:18 -070055 }
56};
James Feist6714a252018-09-10 15:26:18 -070057
James Feist0ffe7142018-11-28 15:25:53 -080058struct VariantToStringVisitor
James Feist6714a252018-09-10 15:26:18 -070059{
60 template <typename T> std::string operator()(const T &t) const
61 {
James Feist0ffe7142018-11-28 15:25:53 -080062 if constexpr (std::is_same_v<T, std::string>)
63 {
64 return t;
65 }
66 else if constexpr (std::is_arithmetic_v<T>)
67 {
68 return std::to_string(t);
69 }
70 throw std::invalid_argument("Cannot translate type to string");
James Feist6714a252018-09-10 15:26:18 -070071 }
72};
James Feist0ffe7142018-11-28 15:25:53 -080073
74struct VariantToDoubleVisitor
James Feist6714a252018-09-10 15:26:18 -070075{
James Feist0ffe7142018-11-28 15:25:53 -080076 template <typename T> double operator()(const T &t) const
77 {
78 if constexpr (std::is_arithmetic_v<T>)
79 {
80 return static_cast<double>(t);
81 }
82 throw std::invalid_argument("Cannot translate type to double");
83 }
84};