James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 2 | // Copyright (c) 2019 Intel Corporation |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 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 Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 18 | #include <stdexcept> |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 19 | #include <string> |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 20 | #include <variant> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 21 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 22 | struct VariantToFloatVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 23 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 24 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 25 | template <typename T> |
| 26 | float operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 27 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 28 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 33 | } |
| 34 | }; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 35 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 36 | struct VariantToIntVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 37 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 38 | template <typename T> |
| 39 | int operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 40 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 41 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 46 | } |
| 47 | }; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 48 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 49 | struct VariantToUnsignedIntVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 50 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 51 | template <typename T> |
| 52 | unsigned int operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 53 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 54 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 59 | } |
| 60 | }; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 61 | |
| 62 | struct VariantToStringVisitor |
| 63 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 64 | template <typename T> |
| 65 | std::string operator()(const T& t) const |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 66 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 67 | 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 Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 76 | } |
| 77 | }; |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 78 | |
| 79 | struct VariantToDoubleVisitor |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 80 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame^] | 81 | template <typename T> |
| 82 | double operator()(const T& t) const |
| 83 | { |
| 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 | }; |