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 |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 18 | #include <boost/type_index.hpp> |
| 19 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 20 | #include <stdexcept> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 23 | namespace details |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 25 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 26 | template <typename U> |
| 27 | struct VariantToNumericVisitor |
| 28 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 29 | template <typename T> |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 30 | U operator()(const T& t) const |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 32 | if constexpr (std::is_arithmetic_v<T>) |
| 33 | { |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 34 | return static_cast<U>(t); |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 35 | } |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 36 | throw std::invalid_argument( |
| 37 | "Cannot translate type " + |
| 38 | boost::typeindex::type_id<T>().pretty_name() + " to " + |
| 39 | boost::typeindex::type_id<U>().pretty_name()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | } |
| 41 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 43 | } // namespace details |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 45 | using VariantToFloatVisitor = details::VariantToNumericVisitor<float>; |
| 46 | using VariantToIntVisitor = details::VariantToNumericVisitor<int>; |
| 47 | using VariantToUnsignedIntVisitor = |
| 48 | details::VariantToNumericVisitor<unsigned int>; |
| 49 | using VariantToDoubleVisitor = details::VariantToNumericVisitor<double>; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 51 | struct VariantToStringVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 52 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 53 | template <typename T> |
| 54 | std::string operator()(const T& t) const |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 56 | if constexpr (std::is_same_v<T, std::string>) |
| 57 | { |
| 58 | return t; |
| 59 | } |
| 60 | else if constexpr (std::is_arithmetic_v<T>) |
| 61 | { |
| 62 | return std::to_string(t); |
| 63 | } |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 64 | throw std::invalid_argument( |
| 65 | "Cannot translate type " + |
| 66 | boost::typeindex::type_id<T>().pretty_name() + " to string"); |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 67 | } |
| 68 | }; |