James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [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 | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 18 | #include <string> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 19 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 20 | struct VariantToFloatVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 21 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 22 | template <typename T> |
| 23 | float operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 24 | { |
| 25 | return static_cast<float>(t); |
| 26 | } |
| 27 | }; |
| 28 | template <> |
| 29 | inline float VariantToFloatVisitor:: |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 30 | operator()<std::string>(const std::string& s) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 31 | { |
| 32 | throw std::invalid_argument("Cannot translate string to float"); |
| 33 | } |
| 34 | |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 35 | struct VariantToIntVisitor |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 36 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 37 | template <typename T> |
| 38 | int operator()(const T& t) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 39 | { |
| 40 | return static_cast<int>(t); |
| 41 | } |
| 42 | }; |
| 43 | template <> |
| 44 | inline int VariantToIntVisitor:: |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 45 | operator()<std::string>(const std::string& s) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 46 | { |
| 47 | throw std::invalid_argument("Cannot translate string to int"); |
| 48 | } |
| 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 | { |
| 55 | return static_cast<int>(t); |
| 56 | } |
| 57 | }; |
| 58 | template <> |
| 59 | inline unsigned int VariantToUnsignedIntVisitor:: |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 60 | operator()<std::string>(const std::string& s) const |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 61 | { |
| 62 | throw std::invalid_argument("Cannot translate string to unsigned int"); |
| 63 | } |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 64 | |
| 65 | struct VariantToStringVisitor |
| 66 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 67 | template <typename T> |
| 68 | std::string operator()(const T& t) const |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 69 | { |
| 70 | return std::to_string(t); |
| 71 | } |
| 72 | }; |
| 73 | template <> |
| 74 | inline std::string VariantToStringVisitor:: |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame^] | 75 | operator()<std::string>(const std::string& s) const |
James Feist | 8f2710a | 2018-05-09 17:18:55 -0700 | [diff] [blame] | 76 | { |
| 77 | return s; |
| 78 | } |