scenario_simulator_v2 C++ API
object.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__OBJECT_HPP_
16 #define OPENSCENARIO_INTERPRETER__OBJECT_HPP_
17 
18 #include <list>
21 #include <typeindex>
22 #include <utility>
23 
25 {
27 
28 template <typename T>
29 struct is
30 {
31  auto operator()(const Object & object) const { return object.is<T>(); }
32 };
33 
34 template <>
35 struct is<Object>
36 {
37  auto operator()(const Object &) const noexcept { return true; }
38 };
39 
40 template <typename T>
41 struct is_also
42 {
43  auto operator()(const Object & object) const { return object.is_also<T>(); }
44 };
45 
46 template <>
47 struct is_also<Object>
48 {
49  auto operator()(const Object &) const noexcept { return true; }
50 };
51 
53 
54 using Group = Object;
55 
56 using Elements = std::list<Object>;
57 
58 template <typename T, typename... Ts>
59 constexpr auto make(Ts &&... xs) -> decltype(auto)
60 {
61  return Object::bind<T>(std::forward<decltype(xs)>(xs)...);
62 }
63 
64 template <typename T>
65 constexpr auto make(T && x) -> decltype(auto)
66 {
67  return Object::bind<typename std::decay<decltype(x)>::type>(std::forward<decltype(x)>(x));
68 }
69 
70 extern const Object unspecified;
71 
73 {
74  auto evaluate() const noexcept -> decltype(auto)
75  {
76  return unspecified; // Self-evaluating.
77  }
78 };
79 
80 auto operator<<(std::ostream &, const Unspecified &) -> std::ostream &;
81 } // namespace openscenario_interpreter
82 
83 #define CASE(TYPE) \
84  { \
85  [](auto & datum) { return datum.template is_also<TYPE>(); }, \
86  [&](auto & datum, auto &&... args) { \
87  return function(datum.template as<TYPE>(), std::forward<Args>(args)...); \
88  } \
89  }
90 
91 #define DEFINE_LAZY_VISITOR(TYPE, ...) \
92  template <typename Result, typename Function, typename... Args> \
93  Result apply(Function && function, TYPE & datum, Args &&... args) \
94  { \
95  std::vector<std::pair<std::function<bool(TYPE &)>, std::function<Result(TYPE &, Args &&...)>>> \
96  dispatcher{{__VA_ARGS__}}; \
97  for (auto & p : dispatcher) { \
98  if (p.first(datum)) { \
99  return p.second(datum, std::forward<Args>(args)...); \
100  } \
101  } \
102  throw UNSUPPORTED_SETTING_DETECTED(TYPE, makeTypename(datum.type().name())); \
103  } \
104  static_assert(true, "")
105 
106 #endif // OPENSCENARIO_INTERPRETER__OBJECT_HPP_
static Pointer bind(Ts &&... xs)
Definition: pointer.hpp:72
Definition: hypot.hpp:22
std::list< Object > Elements
Definition: object.hpp:56
Pointer< Expression > Object
Definition: object.hpp:26
const Object unspecified
Definition: object.cpp:19
auto operator<<(std::ostream &, const Unspecified &) -> std::ostream &
Definition: object.cpp:21
constexpr auto make(Ts &&... xs) -> decltype(auto)
Definition: object.hpp:59
Definition: junit5.hpp:25
Definition: object.hpp:73
auto evaluate() const noexcept -> decltype(auto)
Definition: object.hpp:74
auto operator()(const Object &) const noexcept
Definition: object.hpp:37
auto operator()(const Object &) const noexcept
Definition: object.hpp:49
Definition: object.hpp:42
auto operator()(const Object &object) const
Definition: object.hpp:43
Definition: object.hpp:30
auto operator()(const Object &object) const
Definition: object.hpp:31