scenario_simulator_v2 C++ API
entity.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__ENTITY_HPP_
16 #define OPENSCENARIO_INTERPRETER__SYNTAX__ENTITY_HPP_
17 
18 #include <algorithm>
19 #include <cstddef>
20 #include <functional>
21 #include <iterator>
26 #include <pugixml.hpp>
28 #include <set>
29 #include <type_traits>
30 #include <valarray>
31 #include <vector>
32 
34 {
35 struct Scope;
36 
37 inline namespace syntax
38 {
39 struct Entities;
40 
41 struct ScenarioObject;
42 
43 struct EntitySelection;
44 
45 struct Entity : public Object
46 {
47  Entity() = default;
48 
49  Entity(const ScenarioObject &);
50 
51  Entity(const EntitySelection &);
52 
53  Entity(const String &, const Scope &);
54 
55  Entity(const pugi::xml_node &, const Scope &);
56 
57  auto name() const -> String;
58 
59  auto objects() const -> std::set<Entity>;
60 
61  auto objectTypes() const -> std::set<ObjectType::value_type>;
62 
63  template <typename Function>
64  auto apply(const Function & function) const
65  {
66  using Result = std::invoke_result_t<Function, Entity>;
67  auto objects = this->objects();
68  if constexpr (std::is_same_v<Result, void>) {
69  std::for_each(std::begin(objects), std::end(objects), function);
70  } else {
71  auto results = std::valarray<Result>(objects.size());
72  std::transform(std::begin(objects), std::end(objects), std::begin(results), function);
73  return results;
74  }
75  }
76 
83  operator EntityRef() const;
84 };
85 
86 auto operator==(const Entity &, const Entity &) -> bool;
87 
88 auto operator<<(std::ostream &, const Entity &) -> std::ostream &;
89 } // namespace syntax
90 } // namespace openscenario_interpreter
91 
92 template <>
93 struct std::hash<openscenario_interpreter::Entity>
94 {
95  auto operator()(const openscenario_interpreter::Entity & entity) const -> std::size_t
96  {
97  return std::hash<void *>{}(entity.get());
98  }
99 };
100 
101 #endif // OPENSCENARIO_INTERPRETER__SYNTAX__ENTITY_HPP_
Definition: scope.hpp:154
Result
Definition: cpp_scenario_node.hpp:28
auto operator==(const Entity &, const Entity &) -> bool
Definition: entity.cpp:49
auto operator<<(std::ostream &, const Boolean &) -> std::ostream &
Definition: boolean.cpp:46
std::string String
Definition: string.hpp:24
Definition: escape_sequence.hpp:22
Definition: cache.hpp:27
Definition: entity_ref.hpp:35
Definition: entity_selection.hpp:40
Definition: entity.hpp:46
auto name() const -> String
Definition: entity.cpp:59
auto objects() const -> std::set< Entity >
Definition: entity.cpp:61
auto apply(const Function &function) const
Definition: entity.hpp:64
auto objectTypes() const -> std::set< ObjectType::value_type >
Definition: entity.cpp:72
Definition: object_type.hpp:44
Definition: scenario_object.hpp:41
auto operator()(const openscenario_interpreter::Entity &entity) const -> std::size_t
Definition: entity.hpp:95