James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 18 | #include <stdexcept> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | #include <string> |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame^] | 20 | #include <variant> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 22 | struct VariantToFloatVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 24 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | template <typename T> float operator()(const T &t) const |
| 26 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 27 | if constexpr (std::is_arithmetic_v<T>) |
| 28 | { |
| 29 | return static_cast<float>(t); |
| 30 | } |
| 31 | throw std::invalid_argument("Cannot translate type to float"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | } |
| 33 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 34 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 35 | struct VariantToIntVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | { |
| 37 | template <typename T> int operator()(const T &t) const |
| 38 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 39 | if constexpr (std::is_arithmetic_v<T>) |
| 40 | { |
| 41 | return static_cast<int>(t); |
| 42 | } |
| 43 | throw std::invalid_argument("Cannot translate type to int"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | } |
| 45 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 47 | struct VariantToUnsignedIntVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 48 | { |
| 49 | template <typename T> unsigned int operator()(const T &t) const |
| 50 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 51 | if constexpr (std::is_arithmetic_v<T>) |
| 52 | { |
| 53 | return static_cast<unsigned int>(t); |
| 54 | } |
| 55 | throw std::invalid_argument("Cannot translate type to unsigned int"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | } |
| 57 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 58 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 59 | struct VariantToStringVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 60 | { |
| 61 | template <typename T> std::string operator()(const T &t) const |
| 62 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 63 | if constexpr (std::is_same_v<T, std::string>) |
| 64 | { |
| 65 | return t; |
| 66 | } |
| 67 | else if constexpr (std::is_arithmetic_v<T>) |
| 68 | { |
| 69 | return std::to_string(t); |
| 70 | } |
| 71 | throw std::invalid_argument("Cannot translate type to string"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 72 | } |
| 73 | }; |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 74 | |
| 75 | struct VariantToDoubleVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 77 | template <typename T> double operator()(const T &t) const |
| 78 | { |
| 79 | if constexpr (std::is_arithmetic_v<T>) |
| 80 | { |
| 81 | return static_cast<double>(t); |
| 82 | } |
| 83 | throw std::invalid_argument("Cannot translate type to double"); |
| 84 | } |
| 85 | }; |