James Feist | 139cb57 | 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 |
| 18 | #include <boost/variant.hpp> |
| 19 | #include <string> |
| 20 | |
| 21 | struct VariantToFloatVisitor : public boost::static_visitor<float> |
| 22 | { |
| 23 | template <typename T> float operator()(const T &t) const |
| 24 | { |
| 25 | return static_cast<float>(t); |
| 26 | } |
| 27 | }; |
| 28 | template <> |
| 29 | inline float VariantToFloatVisitor:: |
| 30 | operator()<std::string>(const std::string &s) const |
| 31 | { |
| 32 | throw std::invalid_argument("Cannot translate string to float"); |
| 33 | } |
| 34 | |
| 35 | struct VariantToIntVisitor : public boost::static_visitor<int> |
| 36 | { |
| 37 | template <typename T> int operator()(const T &t) const |
| 38 | { |
| 39 | return static_cast<int>(t); |
| 40 | } |
| 41 | }; |
| 42 | template <> |
| 43 | inline int VariantToIntVisitor:: |
| 44 | operator()<std::string>(const std::string &s) const |
| 45 | { |
| 46 | throw std::invalid_argument("Cannot translate string to int"); |
| 47 | } |
| 48 | |
| 49 | struct VariantToUnsignedIntVisitor : public boost::static_visitor<unsigned int> |
| 50 | { |
| 51 | template <typename T> unsigned int operator()(const T &t) const |
| 52 | { |
| 53 | return static_cast<int>(t); |
| 54 | } |
| 55 | }; |
| 56 | template <> |
| 57 | inline unsigned int VariantToUnsignedIntVisitor:: |
| 58 | operator()<std::string>(const std::string &s) const |
| 59 | { |
| 60 | throw std::invalid_argument("Cannot translate string to unsigned int"); |
| 61 | } |
| 62 | |
| 63 | struct VariantToStringVisitor : public boost::static_visitor<std::string> |
| 64 | { |
| 65 | template <typename T> std::string operator()(const T &t) const |
| 66 | { |
| 67 | return std::to_string(t); |
| 68 | } |
| 69 | }; |
| 70 | template <> |
| 71 | inline std::string VariantToStringVisitor:: |
| 72 | operator()<std::string>(const std::string &s) const |
| 73 | { |
| 74 | return s; |
| 75 | } |