| 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 | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file variant_visitors.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 VariantToIntVisitor | 
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 24 | { | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 25 | template <typename T> | 
|  | 26 | int operator()(const T& t) const | 
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 27 | { | 
| James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 28 | if constexpr (std::is_arithmetic_v<T>) | 
|  | 29 | { | 
|  | 30 | return static_cast<int>(t); | 
|  | 31 | } | 
|  | 32 | throw std::invalid_argument("Cannot translate type to int"); | 
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 33 | } | 
|  | 34 | }; | 
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 35 |  | 
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 36 | struct VariantToStringVisitor | 
|  | 37 | { | 
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 38 | template <typename T> | 
|  | 39 | std::string operator()(const T& t) const | 
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 40 | { | 
| James Feist | 1103e9e | 2019-06-06 11:26:36 -0700 | [diff] [blame] | 41 | if constexpr (std::is_same_v<T, std::string>) | 
|  | 42 | { | 
|  | 43 | return t; | 
|  | 44 | } | 
|  | 45 | else if constexpr (std::is_arithmetic_v<T>) | 
|  | 46 | { | 
|  | 47 | return std::to_string(t); | 
|  | 48 | } | 
|  | 49 | throw std::invalid_argument("Cannot translate type to string"); | 
| James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 50 | } | 
|  | 51 | }; |