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 | |
Unive Tien | bd815c7 | 2025-05-28 09:28:54 +0800 | [diff] [blame^] | 20 | #include <concepts> |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 21 | #include <stdexcept> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | #include <string> |
Unive Tien | bd815c7 | 2025-05-28 09:28:54 +0800 | [diff] [blame^] | 23 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 25 | namespace details |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | { |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 27 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 28 | template <typename U> |
| 29 | struct VariantToNumericVisitor |
| 30 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 31 | template <typename T> |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 32 | U operator()(const T& t) const |
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 | if constexpr (std::is_arithmetic_v<T>) |
| 35 | { |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 36 | return static_cast<U>(t); |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 37 | } |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 38 | throw std::invalid_argument( |
| 39 | "Cannot translate type " + |
| 40 | boost::typeindex::type_id<T>().pretty_name() + " to " + |
| 41 | boost::typeindex::type_id<U>().pretty_name()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | } |
| 43 | }; |
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 | } // namespace details |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 47 | using VariantToFloatVisitor = details::VariantToNumericVisitor<float>; |
| 48 | using VariantToIntVisitor = details::VariantToNumericVisitor<int>; |
| 49 | using VariantToUnsignedIntVisitor = |
| 50 | details::VariantToNumericVisitor<unsigned int>; |
| 51 | using VariantToDoubleVisitor = details::VariantToNumericVisitor<double>; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 52 | |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 53 | struct VariantToStringVisitor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 54 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 55 | template <typename T> |
| 56 | std::string operator()(const T& t) const |
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 | if constexpr (std::is_same_v<T, std::string>) |
| 59 | { |
| 60 | return t; |
| 61 | } |
| 62 | else if constexpr (std::is_arithmetic_v<T>) |
| 63 | { |
| 64 | return std::to_string(t); |
| 65 | } |
Josh Lehan | 129de0a | 2021-03-24 18:29:50 -0700 | [diff] [blame] | 66 | throw std::invalid_argument( |
| 67 | "Cannot translate type " + |
| 68 | boost::typeindex::type_id<T>().pretty_name() + " to string"); |
James Feist | 0ffe714 | 2018-11-28 15:25:53 -0800 | [diff] [blame] | 69 | } |
| 70 | }; |
Unive Tien | bd815c7 | 2025-05-28 09:28:54 +0800 | [diff] [blame^] | 71 | |
| 72 | template <std::integral V, std::integral U> |
| 73 | struct VariantToNumArrayVisitor |
| 74 | { |
| 75 | template <typename T> |
| 76 | std::vector<V> operator()(const T& t) const |
| 77 | { |
| 78 | if constexpr (std::is_same_v<T, std::vector<U>>) |
| 79 | { |
| 80 | std::vector<V> output; |
| 81 | output.reserve(t.size()); |
| 82 | |
| 83 | for (const auto& value : t) |
| 84 | { |
| 85 | output.push_back(static_cast<V>(value)); |
| 86 | } |
| 87 | |
| 88 | return output; |
| 89 | } |
| 90 | throw std::invalid_argument( |
| 91 | "Cannot handle type " + |
| 92 | boost::typeindex::type_id<T>().pretty_name() + " to vector<U>"); |
| 93 | } |
| 94 | }; |