blob: 935803ee305f0a15c5299d5e63574b98a63e2d0b [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
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 Feist8f2710a2018-05-09 17:18:55 -070018#include <string>
James Feist3cb5fec2018-01-23 14:41:51 -080019
James Feist8f2710a2018-05-09 17:18:55 -070020struct VariantToFloatVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080021{
James Feista465ccc2019-02-08 12:51:01 -080022 template <typename T>
23 float operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080024 {
25 return static_cast<float>(t);
26 }
27};
28template <>
29inline float VariantToFloatVisitor::
James Feista465ccc2019-02-08 12:51:01 -080030 operator()<std::string>(const std::string& s) const
James Feist3cb5fec2018-01-23 14:41:51 -080031{
32 throw std::invalid_argument("Cannot translate string to float");
33}
34
James Feist8f2710a2018-05-09 17:18:55 -070035struct VariantToIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080036{
James Feista465ccc2019-02-08 12:51:01 -080037 template <typename T>
38 int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080039 {
40 return static_cast<int>(t);
41 }
42};
43template <>
44inline int VariantToIntVisitor::
James Feista465ccc2019-02-08 12:51:01 -080045 operator()<std::string>(const std::string& s) const
James Feist3cb5fec2018-01-23 14:41:51 -080046{
47 throw std::invalid_argument("Cannot translate string to int");
48}
49
James Feist8f2710a2018-05-09 17:18:55 -070050struct VariantToUnsignedIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080051{
James Feista465ccc2019-02-08 12:51:01 -080052 template <typename T>
53 unsigned int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080054 {
55 return static_cast<int>(t);
56 }
57};
58template <>
59inline unsigned int VariantToUnsignedIntVisitor::
James Feista465ccc2019-02-08 12:51:01 -080060 operator()<std::string>(const std::string& s) const
James Feist3cb5fec2018-01-23 14:41:51 -080061{
62 throw std::invalid_argument("Cannot translate string to unsigned int");
63}
James Feist8f2710a2018-05-09 17:18:55 -070064
65struct VariantToStringVisitor
66{
James Feista465ccc2019-02-08 12:51:01 -080067 template <typename T>
68 std::string operator()(const T& t) const
James Feist8f2710a2018-05-09 17:18:55 -070069 {
70 return std::to_string(t);
71 }
72};
73template <>
74inline std::string VariantToStringVisitor::
James Feista465ccc2019-02-08 12:51:01 -080075 operator()<std::string>(const std::string& s) const
James Feist8f2710a2018-05-09 17:18:55 -070076{
77 return s;
78}