scenario_simulator_v2 C++ API
pointer.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__POINTER_HPP_
16 #define OPENSCENARIO_INTERPRETER__POINTER_HPP_
17 
18 #include <cstddef>
19 #include <memory>
25 #include <type_traits>
26 #include <typeinfo>
27 #include <utility>
28 
30 {
31 template <typename T>
32 class Pointer : public std::shared_ptr<T>
33 {
34  template <typename Bound>
35  struct Binder : public T, public Bound
36  {
37  template <typename... Ts>
38  explicit constexpr Binder(Ts &&... xs) : Bound(std::forward<decltype(xs)>(xs)...)
39  {
40  }
41 
42  virtual ~Binder() = default;
43 
44  auto type() const noexcept -> const std::type_info & override { return typeid(Bound); }
45 
46  private:
47  bool accomplished() override //
48  {
50  }
51 
52  auto description() const -> std::string override
53  {
55  }
56 
57  auto evaluate(const Pointer & else_) -> Pointer override
58  {
59  return IfHasMemberFunctionEvaluate<Bound>::invoke(static_cast<Bound &>(*this), else_);
60  }
61 
62  auto write(std::ostream & os) const -> std::ostream & override
63  {
65  }
66  };
67 
68 public:
69  using std::shared_ptr<T>::shared_ptr;
70 
71  template <typename U, typename... Ts>
72  static Pointer bind(Ts &&... xs)
73  {
74  using Binding = Binder<U>;
75  return static_cast<Pointer>(std::make_shared<Binding>(std::forward<decltype(xs)>(xs)...));
76  }
77 
78  template <typename U, typename... Ts>
79  decltype(auto) rebind(Ts &&... xs)
80  {
81  return *this = bind<U>(std::forward<decltype(xs)>(xs)...);
82  }
83 
84  decltype(auto) binding() const
85  {
86  if (*this) {
88  } else {
89  throw SemanticError(
90  "Dereferencing null-pointer. This is likely due to improper implementation");
91  }
92  }
93 
94  auto type() const -> const std::type_info & { return *this ? binding().type() : typeid(nullptr); }
95 
96  template <typename U>
97  auto is() const -> bool
98  {
99  return type() == typeid(U);
100  }
101 
102  template <typename U>
103  auto is_also() const
104  {
105  return static_cast<bool>(std::dynamic_pointer_cast<U>(*this));
106  }
107 
108  template <typename U>
109  auto as() const -> U &
110  {
111  if (const auto bound = std::dynamic_pointer_cast<U>(*this)) {
112  return *bound;
113  } else {
114  throw SemanticError(
115  "Can't treat ", makeTypename(binding().type()), " as ", makeTypename(typeid(U)));
116  }
117  }
118 
119  template <typename... Ts>
120  decltype(auto) evaluate(Ts &&... xs) const
121  {
122  return binding().evaluate(*this, std::forward<decltype(xs)>(xs)...);
123  }
124 
125  template <typename... Ts>
126  decltype(auto) accomplished(Ts &&... xs) const
127  {
128  return binding().accomplished(std::forward<decltype(xs)>(xs)...);
129  }
130 
131  template <typename... Ts>
132  decltype(auto) description(Ts &&... xs) const
133  {
134  return binding().description(std::forward<decltype(xs)>(xs)...);
135  }
136 };
137 
138 template <typename T>
139 std::ostream & operator<<(std::ostream & os, const Pointer<T> & pointer)
140 {
141  return (pointer ? pointer.binding().write(os) : (os << faint << "<TODO/>")) << reset;
142 }
143 } // namespace openscenario_interpreter
144 
145 #endif // OPENSCENARIO_INTERPRETER__POINTER_HPP_
Definition: pointer.hpp:33
decltype(auto) evaluate(Ts &&... xs) const
Definition: pointer.hpp:120
decltype(auto) binding() const
Definition: pointer.hpp:84
auto as() const -> U &
Definition: pointer.hpp:109
static Pointer bind(Ts &&... xs)
Definition: pointer.hpp:72
auto is_also() const
Definition: pointer.hpp:103
auto is() const -> bool
Definition: pointer.hpp:97
decltype(auto) accomplished(Ts &&... xs) const
Definition: pointer.hpp:126
decltype(auto) rebind(Ts &&... xs)
Definition: pointer.hpp:79
auto type() const -> const std::type_info &
Definition: pointer.hpp:94
decltype(auto) description(Ts &&... xs) const
Definition: pointer.hpp:132
auto operator*(const T &a, const U &b)
Definition: operator.hpp:57
auto reset
Definition: escape_sequence.hpp:34
auto faint
Definition: escape_sequence.hpp:36
auto makeTypename(Ts &&... xs)
Definition: demangle.hpp:30
Definition: hypot.hpp:22
auto operator<<(std::ostream &, const Unspecified &) -> std::ostream &
Definition: object.cpp:21
Definition: cache.hpp:27
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Definition: if_has_member_function_accomplished.hpp:26
Definition: if_has_member_function_description.hpp:26
Definition: if_has_member_function_evaluate.hpp:27
Definition: if_has_stream_output_operator.hpp:28