scenario_simulator_v2 C++ API
properties.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__PROPERTIES_HPP_
16 #define OPENSCENARIO_INTERPRETER__SYNTAX__PROPERTIES_HPP_
17 
21 #include <pugixml.hpp>
22 #include <unordered_map>
23 
25 {
26 inline namespace syntax
27 {
28 /* ---- Properties -------------------------------------------------------------
29  *
30  * Container for one or more properties. Properties encloses multiple property
31  * instances and/or multiple file references.
32  *
33  * <xsd:complexType name="Properties">
34  * <xsd:sequence>
35  * <xsd:element name="Property" minOccurs="0" maxOccurs="unbounded" type="Property"/>
36  * <xsd:element name="File" type="File" minOccurs="0" maxOccurs="unbounded"/>
37  * </xsd:sequence>
38  * </xsd:complexType>
39  *
40  * -------------------------------------------------------------------------- */
41 struct Properties
42 {
43  /*
44  A name-value pair. The semantic of the name/values are subject of a
45  contract between the provider of a simulation environment and the author
46  of a scenario.
47  */
48  std::unordered_map<String, Property> properties;
49 
50  /*
51  A list of arbitrary files attached to an object that owns the properties.
52  The semantic and the file formats are subject of a contract between the
53  provider of a simulation environment and the author of a scenario.
54 
55  NOTE: currently ignored.
56  */
57  std::list<File> files;
58 
59  Properties() = default;
60 
61  explicit Properties(const pugi::xml_node &, Scope &);
62 
63  template <typename T>
64  auto get(const String & name, const T & default_value) const -> auto
65  {
66  if (auto iter = properties.find(name); iter != std::end(properties)) {
67  if (const auto [name, property] = *iter; not property.value.empty()) {
68  return boost::lexical_cast<T>(property.value);
69  } else {
70  return default_value;
71  }
72  } else {
73  return default_value;
74  }
75  }
76 
77  template <typename T>
78  auto get(const String & name) const -> auto
79  {
80  return get<T>(name, T());
81  }
82 };
83 } // namespace syntax
84 } // namespace openscenario_interpreter
85 
86 #endif // OPENSCENARIO_INTERPRETER__SYNTAX__PROPERTIES_HPP_
Definition: scope.hpp:154
std::string String
Definition: string.hpp:24
Definition: escape_sequence.hpp:22
Definition: properties.hpp:42
auto get(const String &name, const T &default_value) const -> auto
Definition: properties.hpp:64
auto get(const String &name) const -> auto
Definition: properties.hpp:78
std::unordered_map< String, Property > properties
Definition: properties.hpp:48
std::list< File > files
Definition: properties.hpp:57