15 #ifndef OPENSCENARIO_INTERPRETER__SCOPE_HPP_
16 #define OPENSCENARIO_INTERPRETER__SCOPE_HPP_
18 #include <boost/lexical_cast.hpp>
19 #include <boost/range/algorithm.hpp>
28 #include <unordered_map>
38 std::multimap<std::string, Object> variables;
42 std::multimap<std::string, EnvironmentFrame *> inner_frames;
44 std::vector<EnvironmentFrame *> unnamed_inner_frames;
46 #define DEFINE_SYNTAX_ERROR(TYPENAME, ...) \
47 template <typename T> \
48 struct TYPENAME : public SyntaxError \
50 explicit TYPENAME(const std::string & variable) \
51 : SyntaxError(__VA_ARGS__, std::quoted(variable), " of type ", makeTypename(typeid(T)), ".") \
59 #undef DEFINE_SYNTAX_ERROR
61 EnvironmentFrame() =
default;
63 explicit EnvironmentFrame(EnvironmentFrame &,
const std::string &);
76 for (std::vector<const EnvironmentFrame *> frames{
this}; not frames.empty();) {
77 auto objects = [&]() {
78 std::vector<Object> result;
79 for (
auto && frame : frames) {
80 const auto it = frame->variables.equal_range(name);
81 std::for_each(it.first, it.second, [&](
auto && name_and_value) {
82 return result.push_back(name_and_value.second);
88 switch (std::count_if(objects.begin(), objects.end(),
is_also<T>())) {
91 std::vector<const EnvironmentFrame *> result;
92 for (
auto && current_frame : frames) {
94 current_frame->unnamed_inner_frames.begin(),
95 current_frame->unnamed_inner_frames.end(), std::back_inserter(result));
101 return *std::find_if(objects.begin(), objects.end(),
is_also<T>());
103 throw AmbiguousReferenceTo<T>(name);
107 return isOutermost() ?
throw NoSuchVariableNamed<T>(name) : outer_frame->
find<T>(name);
110 template <
typename T>
113 if (not prefixed_name.prefixes.empty()) {
114 const auto found = resolvePrefix(prefixed_name);
115 switch (found.size()) {
117 throw NoSuchVariableNamed<T>(boost::lexical_cast<std::string>(prefixed_name));
119 return found.front()->find<T>(prefixed_name.strip<1>());
121 throw AmbiguousReferenceTo<T>(boost::lexical_cast<std::string>(prefixed_name));
124 return find<T>(prefixed_name.name);
128 template <
typename T>
131 if (prefixed_name.absolute) {
132 return outermostFrame().find<T>(prefixed_name);
133 }
else if (prefixed_name.prefixes.empty()) {
134 return find<T>(prefixed_name.name);
136 return lookupFrame(prefixed_name)->find<T>(prefixed_name.strip<1>());
150 inline namespace syntax
181 struct ScenarioDefinition
183 const Entities * entities =
nullptr;
190 const std::shared_ptr<EnvironmentFrame> frame;
192 const std::shared_ptr<ScenarioDefinition> scenario_definition;
211 auto dirname() const ->
std::
string;
213 template <typename... Ts>
214 auto
ref(Ts &&...
xs) const -> decltype(auto)
216 return frame->ref<
Object>(std::forward<decltype(xs)>(
xs)...);
219 template <
typename T,
typename... Ts>
220 auto ref(Ts &&...
xs)
const -> decltype(
auto)
222 return frame->ref<T>(std::forward<decltype(xs)>(
xs)...).template as<T>();
229 auto local() const noexcept -> const
Scope &;
231 auto local() noexcept ->
Scope &;
233 auto insert(const
Name &, const
Object &) ->
void;
EnvironmentFrame(const EnvironmentFrame &)=delete
auto define(const Name &, const Object &) -> void
Definition: scope.cpp:36
EnvironmentFrame(EnvironmentFrame &&)=delete
auto find(const Prefixed< Name > &prefixed_name) const -> Object
Definition: scope.hpp:111
auto find(const Name &name) const -> Object
Definition: scope.hpp:73
auto isOutermost() const noexcept -> bool
Definition: scope.cpp:41
friend struct Scope
Definition: scope.hpp:36
auto ref(const Prefixed< Name > &prefixed_name) const -> Object
Definition: scope.hpp:129
Definition: scope.hpp:158
auto ref(Ts &&... xs) const -> decltype(auto)
Definition: scope.hpp:220
std::list< Entity > actors
Definition: scope.hpp:197
double seed
Definition: scope.hpp:199
const std::string name
Definition: scope.hpp:195
Scope(const Scope &)=default
Definition: lanelet_wrapper.hpp:43
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
#define DEFINE_SYNTAX_ERROR(TYPENAME,...)
Definition: scope.hpp:46
Definition: object.hpp:42
Definition: catalog_locations.hpp:44
Definition: entities.hpp:41
Definition: open_scenario.hpp:40
Definition: scenario_definition.hpp:44