scenario_simulator_v2 C++ API
variant.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef OPENSCENARIO_INTERPRETER__VARIANT__VISIBILITY_H_
16 #define OPENSCENARIO_INTERPRETER__VARIANT__VISIBILITY_H_
17 
18 #if __cplusplus >= 201606
19 #include <variant>
20 #else
21 #include <boost/variant.hpp>
22 #endif
23 
25 {
26 #if __cplusplus >= 201606
27 
28 template <typename... Ts>
29 using Variant = std::variant<Ts...>;
30 
31 using std::visit;
32 
33 #else
34 
35 template <typename... Ts>
36 using Variant = boost::variant<Ts...>;
37 
38 template <typename Lambda, typename... Ts>
40 : boost::static_visitor<
41  typename std::common_type<decltype(std::declval<Lambda>()(std::declval<Ts>()))...>::type>
42 {
43  explicit lambda_visitor(const Lambda & visitor) : visitor(visitor) {}
44  explicit lambda_visitor(Lambda && visitor) : visitor(std::move(visitor)) {}
45 
46  template <typename T>
47  auto operator()(T && arg)
48  {
49  return visitor(std::forward<T>(arg));
50  }
51 
52  template <typename T>
53  auto operator()(T && arg) const
54  {
55  return visitor(std::forward<T>(arg));
56  }
57 
58 private:
59  Lambda visitor;
60 };
61 
62 template <typename Visitor, typename... Ts>
63 auto visit(Visitor && visitor, const boost::variant<Ts...> & arg0)
64 {
65  lambda_visitor<std::decay_t<Visitor>, Ts...> f{std::forward<Visitor>(visitor)};
66  return boost::apply_visitor(f, arg0);
67 }
68 
69 template <typename Visitor, typename... Ts>
70 auto visit(Visitor && visitor, boost::variant<Ts...> && arg0)
71 {
72  lambda_visitor<std::decay_t<Visitor>, Ts...> f{std::forward<Visitor>(visitor)};
73  return boost::apply_visitor(f, std::move(arg0));
74 }
75 
76 template <typename Visitor, typename... Ts, typename... Variants>
77 auto visit(Visitor && visitor, const boost::variant<Ts...> & var0, Variants &&... vars)
78 {
79  return visit(
80  [&visitor, &vars...](auto && arg0) {
81  return visit(
82  [&visitor, &arg0](auto &&... args) {
83  return visitor(arg0, std::forward<decltype(args)>(args)...);
84  },
85  std::forward<decltype(vars)>(vars)...);
86  },
87  var0);
88 }
89 
90 template <typename Visitor, typename... Ts, typename... Variants>
91 auto visit(Visitor && visitor, boost::variant<Ts...> && var0, Variants &&... vars)
92 {
93  return visit(
94  [&visitor, &vars...](auto && arg0) {
95  return visit(
96  [&visitor, &arg0](auto &&... args) {
97  return visitor(arg0, std::forward<decltype(args)>(args)...);
98  },
99  std::forward<decltype(vars)>(vars)...);
100  },
101  std::move(var0));
102 }
103 #endif
104 } // namespace openscenario_interpreter
105 
106 #endif // OPENSCENARIO_INTERPRETER__VARIANT__VISIBILITY_H_
Definition: escape_sequence.hpp:22
auto visit(Visitor &&visitor, const boost::variant< Ts... > &arg0)
Definition: variant.hpp:63
auto visit(Visitor &&visitor, boost::variant< Ts... > &&var0, Variants &&... vars)
Definition: variant.hpp:91
boost::variant< Ts... > Variant
Definition: variant.hpp:36
Definition: cache.hpp:27
auto operator()(T &&arg) const
Definition: variant.hpp:53
lambda_visitor(const Lambda &visitor)
Definition: variant.hpp:43
auto operator()(T &&arg)
Definition: variant.hpp:47
lambda_visitor(Lambda &&visitor)
Definition: variant.hpp:44