blob: 4b4adf40a1fa8b885e537a00938f74671945fecb [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{
22 template <typename T> float operator()(const T &t) const
23 {
24 return static_cast<float>(t);
25 }
26};
27template <>
28inline float VariantToFloatVisitor::
James Feist8f2710a2018-05-09 17:18:55 -070029 operator()<std::string>(const std::string &s) const
James Feist3cb5fec2018-01-23 14:41:51 -080030{
31 throw std::invalid_argument("Cannot translate string to float");
32}
33
James Feist8f2710a2018-05-09 17:18:55 -070034struct VariantToIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080035{
36 template <typename T> int operator()(const T &t) const
37 {
38 return static_cast<int>(t);
39 }
40};
41template <>
42inline int VariantToIntVisitor::
James Feist8f2710a2018-05-09 17:18:55 -070043 operator()<std::string>(const std::string &s) const
James Feist3cb5fec2018-01-23 14:41:51 -080044{
45 throw std::invalid_argument("Cannot translate string to int");
46}
47
James Feist8f2710a2018-05-09 17:18:55 -070048struct VariantToUnsignedIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080049{
50 template <typename T> unsigned int operator()(const T &t) const
51 {
52 return static_cast<int>(t);
53 }
54};
55template <>
56inline unsigned int VariantToUnsignedIntVisitor::
James Feist8f2710a2018-05-09 17:18:55 -070057 operator()<std::string>(const std::string &s) const
James Feist3cb5fec2018-01-23 14:41:51 -080058{
59 throw std::invalid_argument("Cannot translate string to unsigned int");
60}
James Feist8f2710a2018-05-09 17:18:55 -070061
62struct VariantToStringVisitor
63{
64 template <typename T> std::string operator()(const T &t) const
65 {
66 return std::to_string(t);
67 }
68};
69template <>
70inline std::string VariantToStringVisitor::
71 operator()<std::string>(const std::string &s) const
72{
73 return s;
74}