blob: 27185cae9d22e51eb9372392f822e463034af46d [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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 Lehan129de0a2021-03-24 18:29:50 -070018#include <boost/type_index.hpp>
19
James Feist0ffe7142018-11-28 15:25:53 -080020#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070021#include <string>
James Feist3eb82622019-02-08 13:10:22 -080022#include <variant>
James Feist6714a252018-09-10 15:26:18 -070023
Josh Lehan129de0a2021-03-24 18:29:50 -070024namespace details
James Feist6714a252018-09-10 15:26:18 -070025{
James Feist0ffe7142018-11-28 15:25:53 -080026
Josh Lehan129de0a2021-03-24 18:29:50 -070027template <typename U>
28struct VariantToNumericVisitor
29{
James Feistd8705872019-02-08 13:26:09 -080030 template <typename T>
Josh Lehan129de0a2021-03-24 18:29:50 -070031 U operator()(const T& t) const
James Feist6714a252018-09-10 15:26:18 -070032 {
James Feist0ffe7142018-11-28 15:25:53 -080033 if constexpr (std::is_arithmetic_v<T>)
34 {
Josh Lehan129de0a2021-03-24 18:29:50 -070035 return static_cast<U>(t);
James Feist0ffe7142018-11-28 15:25:53 -080036 }
Josh Lehan129de0a2021-03-24 18:29:50 -070037 throw std::invalid_argument(
38 "Cannot translate type " +
39 boost::typeindex::type_id<T>().pretty_name() + " to " +
40 boost::typeindex::type_id<U>().pretty_name());
James Feist6714a252018-09-10 15:26:18 -070041 }
42};
James Feist6714a252018-09-10 15:26:18 -070043
Josh Lehan129de0a2021-03-24 18:29:50 -070044} // namespace details
James Feist6714a252018-09-10 15:26:18 -070045
Josh Lehan129de0a2021-03-24 18:29:50 -070046using VariantToFloatVisitor = details::VariantToNumericVisitor<float>;
47using VariantToIntVisitor = details::VariantToNumericVisitor<int>;
48using VariantToUnsignedIntVisitor =
49 details::VariantToNumericVisitor<unsigned int>;
50using VariantToDoubleVisitor = details::VariantToNumericVisitor<double>;
James Feist6714a252018-09-10 15:26:18 -070051
James Feist0ffe7142018-11-28 15:25:53 -080052struct VariantToStringVisitor
James Feist6714a252018-09-10 15:26:18 -070053{
James Feistd8705872019-02-08 13:26:09 -080054 template <typename T>
55 std::string operator()(const T& t) const
James Feist6714a252018-09-10 15:26:18 -070056 {
James Feist0ffe7142018-11-28 15:25:53 -080057 if constexpr (std::is_same_v<T, std::string>)
58 {
59 return t;
60 }
61 else if constexpr (std::is_arithmetic_v<T>)
62 {
63 return std::to_string(t);
64 }
Josh Lehan129de0a2021-03-24 18:29:50 -070065 throw std::invalid_argument(
66 "Cannot translate type " +
67 boost::typeindex::type_id<T>().pretty_name() + " to string");
James Feist0ffe7142018-11-28 15:25:53 -080068 }
69};