YARP  2.3.70.1
Yet Another Robot Platform
ConstString.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 RobotCub Consortium
3  * Authors: Paul Fitzpatrick
4  * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
5  */
6 
7 #ifndef YARP_OS_CONSTSTRING_H
8 #define YARP_OS_CONSTSTRING_H
9 
10 #include <yarp/os/api.h>
11 #include <yarp/conf/system.h>
12 
13 
14 // yarp::os::ConstString == std::string
15 #ifndef YARP_WRAP_STL_STRING
16 #include <string>
17 #define YARP_CONSTSTRING_IS_STD_STRING 1
18 namespace yarp {
19  namespace os {
20  typedef std::string ConstString;
21  }
22 }
23 
24 
25 #else // YARP_WRAP_STL_STRING
26 
27 // yarp::os::ConstString == opaque/inline wrapped version of std::string
28 #include <yarp/conf/numeric.h>
29 #include <string>
30 #include <iostream>
31 
32 namespace yarp {
33  namespace os {
34  class ConstString;
35  }
36 }
37 
44 class YARP_OS_API yarp::os::ConstString {
45 public:
46 
47  static const size_t npos;
48 
49  typedef char value_type;
50  typedef std::char_traits<char> traits_type;
51  typedef std::allocator<char> allocator_type;
52  typedef size_t size_type;
53  typedef ptrdiff_t difference_type;
54 
55  typedef char& reference;
56  typedef const char& const_reference;
57  typedef char* pointer;
58  typedef const char* const_pointer;
59 
60  typedef std::string::iterator iterator;
61  typedef std::string::const_iterator const_iterator;
62  typedef std::reverse_iterator<iterator> reverse_iterator;
63  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
64 
65 #ifndef YARP_WRAP_STL_STRING_INLINE
66 
72  ConstString();
73 
78  ConstString(const ConstString& str);
79 
87  ConstString(const ConstString& str, size_t pos, size_t len = npos);
88 
94  ConstString(const char *str);
95 
102  ConstString(const char *str, size_t len);
103 
110  ConstString(size_t len, char c);
111 
115  ~ConstString();
116 
121  ConstString& operator=(const ConstString& str);
122 
127  ConstString& operator=(const char* str);
128 
133  ConstString& operator=(char c);
134 
135 
136  // ConstString is implemented internally as a std::string
137  // We cannot expose that implementation without coupling
138  // the user's compiler family/version too much with that
139  // used to compile YARP.
140  //
141  // However, we can provide some convenience inlines that use
142  // the definition of std::string in user code. Precedent:
143  // QString::toStdString()
144 
145  inline operator std::string() const
146  { return std::string(c_str(), length()); }
147 
148  inline ConstString(const std::string& str) {
149  init(str.c_str(), str.length());
150  }
158  iterator begin();
159  const_iterator begin() const;
160 
161  iterator end();
162  const_iterator end() const;
163 
164  reverse_iterator rbegin();
165  const_reverse_iterator rbegin() const;
166 
167  reverse_iterator rend();
168  const_reverse_iterator rend() const;
176  size_t size() const;
177  size_t length() const;
178  size_t max_size() const;
179  void resize(size_t n);
180  void resize(size_t n, char c);
181  size_t capacity() const;
182  void reserve(size_t n = 0);
183  void clear();
184  bool empty() const;
192  char& operator[](size_t idx);
193  const char& operator[](size_t idx) const;
194 
195  char& at(size_t pos);
196  const char& at(size_t pos) const;
204  ConstString& operator+=(const ConstString& str);
205  ConstString& operator+=(const char *str);
206  ConstString& operator+=(char ch);
207 
208  void push_back (char c);
209 
210  ConstString& assign(const char *s, size_t n);
211 
212  ConstString& erase(size_t pos = 0, size_t len = npos);
213  iterator erase(iterator p);
214  iterator erase(iterator first, iterator last);
215 
216  void swap(ConstString& str);
228  const char *c_str() const;
229 
233  const char *data() const;
234 
238  allocator_type get_allocator() const;
239 
243  size_t copy(char* str, size_t len, size_t pos = 0) const;
244 
248  size_t find(const ConstString& needle, size_t start = 0) const;
249  size_t find(const char *needle, size_t start = 0) const;
250  size_t find(const char *needle, size_t start, size_t len) const;
251  size_t find(char needle, size_t start = 0) const;
252 
256  size_t rfind(const ConstString& needle, size_t start = npos) const;
257  size_t rfind(const char *needle, size_t start = npos) const;
258  size_t rfind(const char *needle, size_t start, size_t len) const;
259  size_t rfind(char needle, size_t start = npos) const;
260 
264  ConstString substr(size_t start = 0, size_t n = npos) const;
273  unsigned long hash() const;
277 #ifndef DOXYGEN_SHOULD_SKIP_THIS
278 private:
279  void init(const char *str, size_t len);
280  void *implementation;
281 #endif
282 
283 #else // YARP_WRAP_STL_STRING_INLINE
284 
285  inline ConstString() {}
286 
287  inline ConstString(const ConstString& str) : s(str.s) {}
288 
289  inline ConstString(const ConstString& str, size_t pos, size_t len = npos) : s(str.s, pos, len) {}
290 
291  inline ConstString(const char *str) : s(str) {}
292 
293  inline ConstString(const char *str, size_t len) : s(str, len) {}
294 
295  inline ConstString(size_t len, char c) : s(len, c) {}
296 
297  inline ~ConstString() {}
298 
299 
300  inline const char *c_str() const { return s.c_str(); }
301 
302  inline const char *data() const { return s.data(); }
303 
304  inline allocator_type get_allocator() const { return s.get_allocator(); }
305 
306  inline size_t copy(char* str, size_t len, size_t pos = 0) const { return s.copy(str, len, pos); }
307 
308  inline void push_back (char c) { s.push_back(c); }
309 
310  inline ConstString& assign(const char *s, size_t n) {
311  this->s.assign(s, n);
312  return *this;
313  }
314 
315  inline char& operator[](size_t idx) { return s[idx]; }
316  inline const char& operator[](size_t idx) const { return s[idx]; }
317 
318  inline char& at(size_t pos) { return s.at(pos); }
319  inline const char& at(size_t pos) const { return s.at(pos); }
320 
321  inline const ConstString& operator=(const ConstString& str) {
322  if (&str != this) {
323  s = str.s;
324  }
325  return *this;
326  }
327 
328  inline ConstString& operator=(const char* str) {
329  s = str;
330  return *this;
331  }
332 
333  inline ConstString& operator=(char c) {
334  s = c;
335  return *this;
336  }
337 
338  inline const ConstString& operator+=(char ch) {
339  s += ch;
340  return *this;
341  }
342 
343  inline const ConstString& operator+=(const char *str) {
344  s += str;
345  return *this;
346  }
347 
348  inline const ConstString& operator+=(const ConstString& str) {
349  s += str.s;
350  return *this;
351  }
352 
353  inline ConstString& erase(size_t pos = 0, size_t len = npos) {
354  s.erase(pos, len);
355  return *this;
356  }
357 
358  inline iterator erase(iterator p) {
359  return s.erase(p);
360  }
361 
362  inline iterator erase(iterator first, iterator last) {
363  return s.erase(first, last);
364  }
365 
366  inline void swap(ConstString& str) { s.swap(str.s); }
367 
368  inline size_t size() const { return s.length(); }
369 
370  inline size_t length() const { return s.length(); }
371 
372  inline size_t max_size() const { return s.max_size(); }
373 
374  inline void resize(size_t n) { s.resize(n); }
375  inline void resize(size_t n, char c) { s.resize(n, c); }
376 
377  inline size_t capacity() const { return s.capacity(); }
378 
379  inline void reserve(size_t n = 0) { s.reserve(n); }
380 
381  inline size_t find(const ConstString& needle, size_t start = 0) const { return s.find(needle.s, start); }
382  inline size_t find(const char *needle, size_t start = 0) const { return s.find(needle, start); }
383  inline size_t find(const char *needle, size_t start, size_t len) const { return s.find(needle, start, len); }
384  inline size_t find(char needle, size_t start = 0) const { return s.find(needle, start); }
385 
386  inline size_t rfind(const ConstString& needle, size_t start = npos) const { return s.rfind(needle.s, start); }
387  inline size_t rfind(const char *needle, size_t start = npos) const { return s.rfind(needle, start); }
388  inline size_t rfind(const char *needle, size_t start, size_t len) const { return s.rfind(needle, start, len); }
389  inline size_t rfind(char needle, size_t start = npos) const { return s.rfind(needle, start); }
390 
391  inline ConstString substr(size_t start = 0, size_t n = npos) const { return s.substr(start, n); }
392 
393 
394  unsigned long hash() const;
395 
396  inline void clear() { s.clear(); }
397 
398  inline bool empty() const { return s.empty(); }
399 
400  inline iterator begin() { return s.begin(); }
401  inline const_iterator begin() const { return s.begin(); }
402 
403  inline iterator end() { return s.end(); }
404  inline const_iterator end() const { return s.end(); }
405 
406  inline reverse_iterator rbegin() { return s.rbegin(); }
407  inline const_reverse_iterator rbegin() const { return s.rbegin(); }
408 
409  inline reverse_iterator rend() { return s.rend(); }
410  inline const_reverse_iterator rend() const { return s.rend(); }
411 
412  inline operator std::string() const
413  { return s; }
414 
415  inline ConstString(const std::string& str) {
416  s = str;
417  }
418 
419 private:
420  std::string s;
421 
422 #endif // YARP_WRAP_STL_STRING_INLINE
423 };
424 
425 namespace yarp {
426  namespace os {
431  inline std::istream& operator>>(std::istream& stream,
432  yarp::os::ConstString& str) {
433  stream >> (std::string&)str;
434  return stream;
435  }
442  inline std::ostream& operator<<(std::ostream& stream,
443  const yarp::os::ConstString& str) {
444  stream << (const std::string&)str;
445  return stream;
446  }
454  const yarp::os::ConstString& rhs) {
455  return operator+((const std::string&)lhs, (const std::string&)rhs);
456  }
457 
459  const char* rhs) {
460  return operator+((const std::string&)lhs, rhs);
461  }
462 
463  inline yarp::os::ConstString operator+(const char* lhs,
464  const yarp::os::ConstString& rhs) {
465  return operator+(lhs, (const std::string&)rhs);
466  }
467 
469  char rhs) {
470  return operator+((const std::string&)lhs, rhs);
471  }
472 
474  const yarp::os::ConstString& rhs) {
475  return operator+(lhs, (const std::string&)rhs);
476  }
483  inline bool operator==(const yarp::os::ConstString& lhs,
484  const yarp::os::ConstString& rhs) {
485  return operator==((const std::string&)lhs, (const std::string&)rhs);
486  }
487 
488  inline bool operator==(const char* lhs,
489  const yarp::os::ConstString& rhs) {
490  return operator==(lhs, (const std::string&)rhs);
491  }
492 
493  inline bool operator==(const yarp::os::ConstString& lhs,
494  const char* rhs) {
495  return operator==((const std::string&)lhs, rhs);
496  }
497 
498  inline bool operator!=(const yarp::os::ConstString& lhs,
499  const yarp::os::ConstString& rhs) {
500  return operator!=((const std::string&)lhs, (const std::string&)rhs);
501  }
502 
503  inline bool operator!=(const char* lhs,
504  const yarp::os::ConstString& rhs) {
505  return operator!=(lhs, (const std::string&)rhs);
506  }
507 
508  inline bool operator!=(const yarp::os::ConstString& lhs,
509  const char* rhs) {
510  return operator!=((const std::string&)lhs, rhs);
511  }
512 
513  inline bool operator<(const yarp::os::ConstString& lhs,
514  const yarp::os::ConstString& rhs) {
515  return operator<((const std::string&)lhs, (const std::string&)rhs);
516  }
517 
518  inline bool operator<(const char* lhs,
519  const yarp::os::ConstString& rhs) {
520  return operator<(lhs, (const std::string&)rhs);
521  }
522 
523  inline bool operator<(const yarp::os::ConstString& lhs,
524  const char* rhs) {
525  return operator<((const std::string&)lhs, rhs);
526  }
527 
528  inline bool operator<=(const yarp::os::ConstString& lhs,
529  const yarp::os::ConstString& rhs) {
530  return operator<=((const std::string&)lhs, (const std::string&)rhs);
531  }
532 
533  inline bool operator<=(const char* lhs,
534  const yarp::os::ConstString& rhs) {
535  return operator<=(lhs, (const std::string&)rhs);
536  }
537 
538  inline bool operator<=(const yarp::os::ConstString& lhs,
539  const char* rhs) {
540  return operator<=((const std::string&)lhs, rhs);
541  }
542 
543  inline bool operator>(const yarp::os::ConstString& lhs,
544  const yarp::os::ConstString& rhs) {
545  return operator>((const std::string&)lhs, (const std::string&)rhs);
546  }
547 
548  inline bool operator>(const char* lhs,
549  const yarp::os::ConstString& rhs) {
550  return operator>(lhs, (const std::string&)rhs);
551  }
552 
553  inline bool operator>(const yarp::os::ConstString& lhs,
554  const char* rhs) {
555  return operator>((const std::string&)lhs, rhs);
556  }
557 
558  inline bool operator>=(const yarp::os::ConstString& lhs,
559  const yarp::os::ConstString& rhs) {
560  return operator>=((const std::string&)lhs, (const std::string&)rhs);
561  }
562 
563  inline bool operator>=(const char* lhs,
564  const yarp::os::ConstString& rhs) {
565  return operator>=(lhs, (const std::string&)rhs);
566  }
567 
568  inline bool operator>=(const yarp::os::ConstString& lhs,
569  const char* rhs) {
570  return operator>=((const std::string&)lhs, rhs);
571  }
574  using std::swap;
575  }
576 }
577 
578 #ifndef DOXYGEN_SHOULD_SKIP_THIS
579 namespace std {
580  inline void swap(yarp::os::ConstString& x, yarp::os::ConstString& y) {
581  swap((std::string&)x, (std::string&)y);
582  }
583 
584  inline std::istream& getline(std::istream& is, yarp::os::ConstString& str, char delim) {
585  return getline(is, (std::string&)str, delim);
586  }
587 
588  inline std::istream& getline(std::istream& is, yarp::os::ConstString& str) {
589  return getline(is, (std::string&)str);
590  }
591 
592 }
593 #endif // DOXYGEN_SHOULD_SKIP_THIS
594 
595 
596 
597 #endif // YARP_WRAP_STL_STRING
598 
599 #endif // YARP_OS_CONSTSTRING_H
600 
bool operator!=(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:498
ConstString substr(size_t start=0, size_t n=npos) const
Generate a substring.
const char & const_reference
Definition: ConstString.h:56
std::string::const_iterator const_iterator
Definition: ConstString.h:61
bool operator<(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:513
const char * const_pointer
Definition: ConstString.h:58
std::reverse_iterator< iterator > reverse_iterator
Definition: ConstString.h:62
bool operator>(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:543
STL namespace.
bool operator==(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Relational operations for ConstString.
Definition: ConstString.h:483
The main, catch-all namespace for YARP.
A string with almost the same api as std::string.
Definition: ConstString.h:44
static const size_t npos
Definition: ConstString.h:47
yarp::sig::Vector & operator+=(yarp::sig::Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition: math.cpp:35
std::allocator< char > allocator_type
Definition: ConstString.h:51
std::istream & operator>>(std::istream &stream, yarp::os::ConstString &str)
Extract string from stream.
Definition: ConstString.h:431
yarp::os::ConstString operator+(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Concatenate ConstString.
Definition: ConstString.h:453
std::ostream & operator<<(std::ostream &stream, const yarp::os::ConstString &str)
Insert string into stream.
Definition: ConstString.h:442
ConstString & erase(size_t pos=0, size_t len=npos)
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: ConstString.h:63
std::char_traits< char > traits_type
Definition: ConstString.h:50
char & at(size_t pos)
bool operator<=(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:528
void swap(ConstString &str)
const char * c_str() const
Accesses the character sequence stored in this object.
Definition: ConstString.cpp:88
ptrdiff_t difference_type
Definition: ConstString.h:53
std::string::iterator iterator
Definition: ConstString.h:60
bool operator>=(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:558
void * implementation
Definition: Module.h:218
ConstString(const std::string &str)
Definition: ConstString.h:148
ConstString & assign(const char *s, size_t n)