1 /*
2 // Copyright (c) 2019 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 /// \file variant_visitors.hpp
17 
18 #pragma once
19 #include <stdexcept>
20 #include <string>
21 #include <variant>
22 
23 struct VariantToIntVisitor
24 {
25     template <typename T>
26     int operator()(const T& t) const
27     {
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");
33     }
34 };
35 
36 struct VariantToStringVisitor
37 {
38     template <typename T>
39     std::string operator()(const T& t) const
40     {
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");
50     }
51 };
52