15 #ifndef OPENSCENARIO_INTERPRETER__SCOPE_HPP_
16 #define OPENSCENARIO_INTERPRETER__SCOPE_HPP_
18 #include <boost/filesystem.hpp>
19 #include <boost/lexical_cast.hpp>
20 #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 boost::range::for_each(frame->variables.equal_range(name), [&](
auto && name_and_value) {
81 return result.push_back(name_and_value.second);
87 switch (boost::range::count_if(objects,
is_also<T>())) {
90 std::vector<const EnvironmentFrame *> result;
91 for (
auto && current_frame : frames) {
92 boost::range::copy(current_frame->unnamed_inner_frames, std::back_inserter(result));
98 return *boost::range::find_if(objects,
is_also<T>());
100 throw AmbiguousReferenceTo<T>(name);
104 return isOutermost() ?
throw NoSuchVariableNamed<T>(name) : outer_frame->
find<T>(name);
107 template <
typename T>
110 if (not prefixed_name.prefixes.empty()) {
111 const auto found = resolvePrefix(prefixed_name);
112 switch (found.size()) {
114 throw NoSuchVariableNamed<T>(boost::lexical_cast<std::string>(prefixed_name));
116 return found.front()->find<T>(prefixed_name.strip<1>());
118 throw AmbiguousReferenceTo<T>(boost::lexical_cast<std::string>(prefixed_name));
121 return find<T>(prefixed_name.name);
125 template <
typename T>
128 if (prefixed_name.absolute) {
129 return outermostFrame().find<T>(prefixed_name);
130 }
else if (prefixed_name.prefixes.empty()) {
131 return find<T>(prefixed_name.name);
133 return lookupFrame(prefixed_name)->find<T>(prefixed_name.strip<1>());
147 inline namespace syntax
178 struct ScenarioDefinition
180 const Entities * entities =
nullptr;
187 const std::shared_ptr<EnvironmentFrame> frame;
189 const std::shared_ptr<ScenarioDefinition> scenario_definition;
208 auto dirname() const ->
std::
string;
210 template <typename... Ts>
211 auto
ref(Ts &&...
xs) const -> decltype(auto)
213 return frame->ref<
Object>(std::forward<decltype(xs)>(
xs)...);
216 template <
typename T,
typename... Ts>
217 auto ref(Ts &&...
xs)
const -> decltype(
auto)
219 return frame->ref<T>(std::forward<decltype(xs)>(
xs)...).template as<T>();
226 auto local() const noexcept -> const
Scope &;
228 auto local() noexcept ->
Scope &;
230 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:108
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:126
Definition: scope.hpp:155
auto ref(Ts &&... xs) const -> decltype(auto)
Definition: scope.hpp:217
std::list< Entity > actors
Definition: scope.hpp:194
double seed
Definition: scope.hpp:196
const std::string name
Definition: scope.hpp:192
Scope(const Scope &)=default
Definition: lanelet_wrapper.hpp:40
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