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