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