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 | */ |
Brad Bishop | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame^] | 16 | /// \file VariantVisitors.hpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
| 18 | #pragma once |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 19 | #include <stdexcept> |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 20 | #include <string> |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 21 | #include <variant> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 22 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 23 | struct VariantToFloatVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 24 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 25 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 26 | template <typename T> |
| 27 | float operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 28 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 29 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 34 | } |
| 35 | }; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 36 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 37 | struct VariantToIntVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 38 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 39 | template <typename T> |
| 40 | int operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 41 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 42 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 47 | } |
| 48 | }; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 49 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 50 | struct VariantToUnsignedIntVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 51 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 52 | template <typename T> |
| 53 | unsigned int operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 54 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 55 | 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 Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 60 | } |
| 61 | }; |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 62 | |
| 63 | struct VariantToStringVisitor |
| 64 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 65 | template <typename T> |
| 66 | std::string operator()(const T& t) const |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 67 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 68 | 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 Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 77 | } |
| 78 | }; |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 79 | |
| 80 | struct VariantToDoubleVisitor |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 81 | { |
James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 82 | 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 | }; |