scenario_simulator_v2 C++ API
name.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__NAME_HPP_
16 #define OPENSCENARIO_INTERPRETER__NAME_HPP_
17 
18 #include <iostream>
19 #include <iterator>
20 #include <list>
22 #include <string>
23 
25 {
26 struct Name : public std::string
27 {
28  template <typename... Ts>
29  Name(Ts &&... xs) : std::string(std::forward<decltype(xs)>(xs)...)
30  {
31  for (const auto each : ":") {
32  if (find(each) != std::string::npos) {
33  throw SyntaxError("Invalid name reference ", std::quoted(*this), ".");
34  }
35  }
36  }
37 };
38 
39 template <typename Name>
40 struct Prefixed // NOTE: 1.4.5. Naming conventions for OpenSCENARIO references
41 {
42  const bool absolute;
43 
44  const std::list<std::string> prefixes;
45 
46  const Name name;
47 
48  Prefixed() = delete;
49 
50  Prefixed(Prefixed &&) = default;
51 
52  Prefixed(const Prefixed &) = delete;
53 
54  explicit Prefixed(bool absolute, const std::list<std::string> & prefixes, const Name & name)
56  {
57  }
58 
59  explicit Prefixed(const std::vector<std::string> given)
60  : absolute(not given.empty() and given.front().empty()),
61  prefixes(
62  absolute ? std::next(std::begin(given)) : std::begin(given), std::prev(std::end(given))),
63  name(given.back())
64  {
65  }
66 
67  Prefixed(const std::string & given) : Prefixed(separate(given)) {}
68 
69  template <std::size_t N>
70  auto strip() const
71  {
72  return Prefixed(false, {std::next(std::begin(prefixes), N), std::end(prefixes)}, name);
73  }
74 
75  static auto separate(const std::string & name) -> std::vector<std::string>
76  {
77  const std::string separator = "::";
78 
79  std::vector<std::string> result;
80 
81  std::size_t prev_pos = 0;
82 
83  std::size_t pos = 0;
84 
85  while ((pos = name.find(separator, prev_pos)) != std::string::npos) {
86  result.push_back(name.substr(prev_pos, pos - prev_pos));
87  prev_pos = pos + separator.size();
88  }
89 
90  result.push_back(name.substr(prev_pos, pos));
91 
92  return result;
93  }
94 
95  friend auto operator<<(std::ostream & os, const Prefixed & prefixed_name) -> std::ostream &
96  {
97  if (prefixed_name.absolute) {
98  os << "{root}::";
99  }
100 
101  for (const auto & prefix : prefixed_name.prefixes) {
102  os << (prefix.empty() ? "{anonymous}" : prefix.c_str()) << "::";
103  }
104 
105  return os << prefixed_name.name;
106  }
107 };
108 } // namespace openscenario_interpreter
109 
110 namespace std
111 {
112 template <>
113 class hash<openscenario_interpreter::Name> : public hash<std::string>
114 {
115 };
116 } // namespace std
117 
118 #endif // OPENSCENARIO_INTERPRETER__NAME_HPP_
Definition: escape_sequence.hpp:22
Definition: cache.hpp:27
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26
Definition: name.hpp:27
Name(Ts &&... xs)
Definition: name.hpp:29
Definition: name.hpp:41
Prefixed(const std::string &given)
Definition: name.hpp:67
Prefixed(bool absolute, const std::list< std::string > &prefixes, const Name &name)
Definition: name.hpp:54
static auto separate(const std::string &name) -> std::vector< std::string >
Definition: name.hpp:75
const bool absolute
Definition: name.hpp:42
Prefixed(const Prefixed &)=delete
auto strip() const
Definition: name.hpp:70
const std::list< std::string > prefixes
Definition: name.hpp:44
Prefixed(const std::vector< std::string > given)
Definition: name.hpp:59
friend auto operator<<(std::ostream &os, const Prefixed &prefixed_name) -> std::ostream &
Definition: name.hpp:95
const Name name
Definition: name.hpp:46