blob: b8ce5b26a456c2bd2a69eec7a0d045edfe78b093 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
James Feist1103e9e2019-06-06 11:26:36 -07002// Copyright (c) 2019 Intel Corporation
James Feist3cb5fec2018-01-23 14:41:51 -08003//
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*/
Brad Bishop1fb9f3f2020-08-28 08:15:13 -040016/// \file VariantVisitors.hpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
18#pragma once
James Feist1103e9e2019-06-06 11:26:36 -070019#include <stdexcept>
James Feist8f2710a2018-05-09 17:18:55 -070020#include <string>
James Feist1103e9e2019-06-06 11:26:36 -070021#include <variant>
James Feist3cb5fec2018-01-23 14:41:51 -080022
James Feist8f2710a2018-05-09 17:18:55 -070023struct VariantToFloatVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080024{
James Feist1103e9e2019-06-06 11:26:36 -070025
James Feista465ccc2019-02-08 12:51:01 -080026 template <typename T>
27 float operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080028 {
James Feist1103e9e2019-06-06 11:26:36 -070029 if constexpr (std::is_arithmetic_v<T>)
30 {
31 return static_cast<float>(t);
32 }
33 throw std::invalid_argument("Cannot translate type to float");
James Feist3cb5fec2018-01-23 14:41:51 -080034 }
35};
James Feist3cb5fec2018-01-23 14:41:51 -080036
James Feist8f2710a2018-05-09 17:18:55 -070037struct VariantToIntVisitor
James Feist3cb5fec2018-01-23 14:41:51 -080038{
James Feista465ccc2019-02-08 12:51:01 -080039 template <typename T>
40 int operator()(const T& t) const
James Feist3cb5fec2018-01-23 14:41:51 -080041 {
James Feist1103e9e2019-06-06 11:26:36 -070042 if constexpr (std::is_arithmetic_v<T>)
43 {
44 return static_cast<int>(t);
45 }
46 throw std::invalid_argument("Cannot translate type to int");
James Feist3cb5fec2018-01-23 14:41:51 -080047 }
48};
James Feist3cb5fec2018-01-23 14:41:51 -080049
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 {
James Feist1103e9e2019-06-06 11:26:36 -070055 if constexpr (std::is_arithmetic_v<T>)
56 {
57 return static_cast<unsigned int>(t);
58 }
59 throw std::invalid_argument("Cannot translate type to unsigned int");
James Feist3cb5fec2018-01-23 14:41:51 -080060 }
61};
James Feist8f2710a2018-05-09 17:18:55 -070062
63struct VariantToStringVisitor
64{
James Feista465ccc2019-02-08 12:51:01 -080065 template <typename T>
66 std::string operator()(const T& t) const
James Feist8f2710a2018-05-09 17:18:55 -070067 {
James Feist1103e9e2019-06-06 11:26:36 -070068 if constexpr (std::is_same_v<T, std::string>)
69 {
70 return t;
71 }
72 else if constexpr (std::is_arithmetic_v<T>)
73 {
74 return std::to_string(t);
75 }
76 throw std::invalid_argument("Cannot translate type to string");
James Feist8f2710a2018-05-09 17:18:55 -070077 }
78};
James Feist1103e9e2019-06-06 11:26:36 -070079
80struct VariantToDoubleVisitor
James Feist8f2710a2018-05-09 17:18:55 -070081{
James Feist1103e9e2019-06-06 11:26:36 -070082 template <typename T>
83 double operator()(const T& t) const
84 {
85 if constexpr (std::is_arithmetic_v<T>)
86 {
87 return static_cast<double>(t);
88 }
89 throw std::invalid_argument("Cannot translate type to double");
90 }
91};