scenario_simulator_v2 C++ API
rule.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__SYNTAX__RULE_HPP_
16 #define OPENSCENARIO_INTERPRETER__SYNTAX__RULE_HPP_
17 
18 #include <functional>
20 #include <string>
21 #include <utility>
22 #include <valarray>
23 
25 {
26 inline namespace syntax
27 {
28 template <typename T, typename U>
30 {
31  using type = bool;
32 };
33 
34 template <typename T, typename U>
35 struct RuleResultDeduction<std::valarray<T>, U>
36 {
37  using type = std::valarray<bool>;
38 };
39 /*
40  Rule (OpenSCENARIO XML 1.3)
41 
42  <xsd:simpleType name="Rule">
43  <xsd:union>
44  <xsd:simpleType>
45  <xsd:restriction base="xsd:string">
46  <xsd:enumeration value="equalTo"/>
47  <xsd:enumeration value="greaterThan"/>
48  <xsd:enumeration value="lessThan"/>
49  <xsd:enumeration value="greaterOrEqual"/>
50  <xsd:enumeration value="lessOrEqual"/>
51  <xsd:enumeration value="notEqualTo"/>
52  </xsd:restriction>
53  </xsd:simpleType>
54  <xsd:simpleType>
55  <xsd:restriction base="parameter"/>
56  </xsd:simpleType>
57  </xsd:union>
58  </xsd:simpleType>
59 */
60 struct Rule
61 {
62  enum value_type {
69  } value;
70 
71  Rule() = default;
72 
73  explicit Rule(value_type value) : value(value) {}
74 
75  constexpr operator value_type() const noexcept { return value; }
76 
77  template <typename T, typename U = T>
78  constexpr auto operator()(const T & lhs, const U & rhs) const noexcept ->
80  {
81  switch (value) {
82  case equalTo:
83  return equal_to<T>()(std::forward<decltype(lhs)>(lhs), std::forward<decltype(rhs)>(rhs));
84  case greaterThan:
85  return std::greater()(std::forward<decltype(lhs)>(lhs), std::forward<decltype(rhs)>(rhs));
86  case lessThan:
87  return std::less()(std::forward<decltype(lhs)>(lhs), std::forward<decltype(rhs)>(rhs));
88  case greaterOrEqual:
89  return std::greater_equal()(
90  std::forward<decltype(lhs)>(lhs), std::forward<decltype(rhs)>(rhs));
91  case lessOrEqual:
92  return std::less_equal()(
93  std::forward<decltype(lhs)>(lhs), std::forward<decltype(rhs)>(rhs));
94  case notEqualTo:
95  return std::not_equal_to()(
96  std::forward<decltype(lhs)>(lhs), std::forward<decltype(rhs)>(rhs));
97  default:
98  return {};
99  }
100  }
101 };
102 
103 auto operator>>(std::istream &, Rule &) -> std::istream &;
104 
105 auto operator<<(std::ostream &, const Rule &) -> std::ostream &;
106 
107 } // namespace syntax
108 } // namespace openscenario_interpreter
109 
110 #endif // OPENSCENARIO_INTERPRETER__SYNTAX__RULE_HPP_
auto operator>>(std::istream &, Boolean &) -> std::istream &
Definition: boolean.cpp:52
auto operator<<(std::ostream &, const Boolean &) -> std::ostream &
Definition: boolean.cpp:46
Definition: escape_sequence.hpp:22
Definition: cache.hpp:27
Definition: rule.hpp:61
Rule(value_type value)
Definition: rule.hpp:73
@ lessThan
Definition: rule.hpp:65
@ notEqualTo
Definition: rule.hpp:68
@ greaterOrEqual
Definition: rule.hpp:66
@ lessOrEqual
Definition: rule.hpp:67
@ equalTo
Definition: rule.hpp:63
@ greaterThan
Definition: rule.hpp:64
enum openscenario_interpreter::syntax::Rule::value_type value
constexpr auto operator()(const T &lhs, const U &rhs) const noexcept -> typename RuleResultDeduction< T, U >::type
Definition: rule.hpp:78