Path.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 // Header
25 #include "Path.h"
26 
27 // ArmarX
29 
30 namespace armarx::aron
31 {
32  Path::Path() : rootIdentifier(DEFAULT_ROOT_IDENTIFIER), delimeter(DEFAULT_DELIMETER)
33  {
34  }
35 
36  Path::Path(const std::vector<std::string>& p) :
37  rootIdentifier(DEFAULT_ROOT_IDENTIFIER), delimeter(DEFAULT_DELIMETER), path(p)
38  {
39  }
40 
41  Path::Path(const Path& p) :
42  rootIdentifier(p.getRootIdentifier()), delimeter(p.getDelimeter()), path(p.getPath())
43  {
44  }
45 
46  Path::Path(const Path& pa, const std::vector<std::string>& p) : Path(pa)
47  {
48  for (const std::string& s : p)
49  {
50  append(s);
51  }
52  }
53 
54  void
55  Path::setRootIdentifier(const std::string& s)
56  {
57  rootIdentifier = s;
58  }
59 
60  std::string
62  {
63  return rootIdentifier;
64  }
65 
66  void
67  Path::setDelimeter(const std::string& d)
68  {
69  delimeter = d;
70  }
71 
72  std::string
74  {
75  return delimeter;
76  }
77 
78  void
79  Path::append(const std::string& str)
80  {
81  path.push_back(str);
82  }
83 
84  std::vector<std::string>
85  Path::getPath() const
86  {
87  return path;
88  }
89 
90  std::string
92  {
93  if (!hasElement())
94  {
95  throw error::AronException(__PRETTY_FUNCTION__,
96  "Try to access last element of empty vector.");
97  }
98  return path.back();
99  }
100 
101  std::string
103  {
104  if (!hasElement())
105  {
106  throw error::AronException(__PRETTY_FUNCTION__,
107  "Try to access last element of empty vector.");
108  }
109  return path[0];
110  }
111 
112  bool
114  {
115  return path.size() > 0;
116  }
117 
118  size_t
119  Path::size() const
120  {
121  return path.size();
122  }
123 
124  std::string
126  {
127  std::stringstream ss;
128  ss << rootIdentifier;
129  for (const std::string& s : path)
130  {
131  ss << delimeter << s;
132  }
133  return ss.str();
134  }
135 
136  Path
137  Path::FromString(const std::string& s,
138  const std::string& rootIdentifier,
139  const std::string& delimeter)
140  {
141  std::vector<std::string> elements = simox::alg::split(s, delimeter);
142  if (elements.size())
143  {
144  elements[0] = simox::alg::remove_prefix(elements[0], rootIdentifier);
145  }
146  return Path(elements);
147  }
148 
149  Path
150  Path::withIndex(int i, bool escape) const
151  {
152  std::string el = std::to_string(i);
153  if (escape)
154  {
155  el = "\"" + el + "\"";
156  }
157  return Path(*this, {el});
158  }
159 
160  Path
161  Path::withElement(const std::string& s, bool escape) const
162  {
163  std::string el = s;
164  if (escape)
165  {
166  el = "\"" + el + "\"";
167  }
168  return Path(*this, {el});
169  }
170 
171  Path
172  Path::withAcceptedType(bool escape) const
173  {
174  std::string el = "::accepted_type";
175  if (escape)
176  {
177  el = "\"" + el + "\"";
178  }
179  return Path(*this, {el});
180  }
181 
182  Path
183  Path::withAcceptedTypeIndex(int i, bool escape) const
184  {
185  std::string el = "::accepted_type_" + std::to_string(i);
186  if (escape)
187  {
188  el = "\"" + el + "\"";
189  }
190  return Path(*this, {el});
191  }
192 
193  Path
195  {
196  std::vector<std::string> p = path;
197  p.pop_back();
198  auto ret = Path(p);
199  ret.setRootIdentifier(rootIdentifier);
200  ret.setDelimeter(delimeter);
201  return ret;
202  }
203 
204  Path
206  {
207  std::vector<std::string> p = path;
208  p.erase(p.begin());
209  auto ret = Path(p);
210  ret.setRootIdentifier(rootIdentifier);
211  ret.setDelimeter(delimeter);
212  return ret;
213  }
214 
215  Path
216  Path::getWithoutPrefix(const Path& pref) const
217  {
218  unsigned int firstWithoutMatch = 0;
219  for (const std::string& el : pref.getPath())
220  {
221  if (path.size() <= firstWithoutMatch || el != path[firstWithoutMatch])
222  {
223  break;
224  }
225  else
226  {
227  firstWithoutMatch++;
228  }
229  }
230  std::vector<std::string> elementsWithoutPrefix(path.begin() + firstWithoutMatch,
231  path.end());
232 
233  auto ret = Path(elementsWithoutPrefix);
234  ret.setRootIdentifier(rootIdentifier);
235  ret.setDelimeter(delimeter);
236  return ret;
237  }
238 
239  bool
240  Path::hasPrefix(const Path& e) const
241  {
242  const auto& p = e.getPath();
243 
244  if (path.size() < p.size())
245  {
246  // prefix is impossible
247  return false;
248  }
249 
250  int i = 0;
251  for (const auto& el : path)
252  {
253  if (el != path[i++])
254  {
255  return false;
256  }
257  }
258 
259  return true;
260  }
261 
262  bool
263  Path::hasDirectPrefix(const Path& e) const
264  {
265  const auto& p = e.getPath();
266  if (p.size() + 1 != path.size())
267  {
268  return false;
269  }
270  return hasPrefix(e);
271  }
272 } // namespace armarx::aron
armarx::aron::Path::withElement
Path withElement(const std::string &, bool escape=false) const
Definition: Path.cpp:161
armarx::aron::Path::getFirstElement
std::string getFirstElement() const
Definition: Path.cpp:102
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::Path::hasElement
bool hasElement() const
Definition: Path.cpp:113
Path.h
armarx::aron::Path::withAcceptedTypeIndex
Path withAcceptedTypeIndex(int, bool escape=false) const
Definition: Path.cpp:183
armarx::aron::Path::size
size_t size() const
Definition: Path.cpp:119
armarx::aron::Path::getDelimeter
std::string getDelimeter() const
Definition: Path.cpp:73
armarx::aron::Path::withIndex
Path withIndex(int, bool escape=false) const
Definition: Path.cpp:150
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::Path::hasPrefix
bool hasPrefix(const Path &) const
Definition: Path.cpp:240
armarx::aron::Path::setRootIdentifier
void setRootIdentifier(const std::string &)
Definition: Path.cpp:55
armarx::aron::Path::setDelimeter
void setDelimeter(const std::string &)
Definition: Path.cpp:67
armarx::aron
Definition: DataDisplayVisitor.cpp:5
armarx::aron::Path::FromString
static Path FromString(const std::string &, const std::string &rootIdentifier=DEFAULT_ROOT_IDENTIFIER, const std::string &delimeter=DEFAULT_DELIMETER)
Definition: Path.cpp:137
armarx::aron::Path::Path
Path()
default constructor
Definition: Path.cpp:32
armarx::aron::Path::hasDirectPrefix
bool hasDirectPrefix(const Path &) const
Definition: Path.cpp:263
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::Path::getLastElement
std::string getLastElement() const
Definition: Path.cpp:91
armarx::aron::Path::withAcceptedType
Path withAcceptedType(bool escape=false) const
Definition: Path.cpp:172
Exception.h
armarx::aron::Path::withDetachedLastElement
Path withDetachedLastElement() const
Definition: Path.cpp:194
armarx::aron::Path::withDetachedFirstElement
Path withDetachedFirstElement() const
Definition: Path.cpp:205
armarx::aron::Path::getRootIdentifier
std::string getRootIdentifier() const
Definition: Path.cpp:61
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::aron::Path::getWithoutPrefix
Path getWithoutPrefix(const Path &) const
Definition: Path.cpp:216
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:85
armarx::aron::Path::toString
std::string toString() const
Definition: Path.cpp:125
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36