YARP
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 ConstString& str);
211  ConstString& assign (const ConstString& str, size_t subpos, size_t sublen);
212  ConstString& assign (const char* s);
213  ConstString& assign(const char *s, size_t n);
214 
215  ConstString& erase(size_t pos = 0, size_t len = npos);
216  iterator erase(iterator p);
217  iterator erase(iterator first, iterator last);
218 
219  void swap(ConstString& str);
231  const char *c_str() const;
232 
236  const char *data() const;
237 
241  allocator_type get_allocator() const;
242 
246  size_t copy(char* str, size_t len, size_t pos = 0) const;
247 
251  size_t find(const ConstString& needle, size_t start = 0) const;
252  size_t find(const char *needle, size_t start = 0) const;
253  size_t find(const char *needle, size_t start, size_t len) const;
254  size_t find(char needle, size_t start = 0) const;
255 
259  size_t rfind(const ConstString& needle, size_t start = npos) const;
260  size_t rfind(const char *needle, size_t start = npos) const;
261  size_t rfind(const char *needle, size_t start, size_t len) const;
262  size_t rfind(char needle, size_t start = npos) const;
263 
267  ConstString substr(size_t start = 0, size_t n = npos) const;
276  unsigned long hash() const;
280 #ifndef DOXYGEN_SHOULD_SKIP_THIS
281 private:
282  void init(const char *str, size_t len);
283  void *implementation;
284 #endif
285 
286 #else // YARP_WRAP_STL_STRING_INLINE
287 
288  inline ConstString() {}
289 
290  inline ConstString(const ConstString& str) : s(str.s) {}
291 
292  inline ConstString(const ConstString& str, size_t pos, size_t len = npos) : s(str.s, pos, len) {}
293 
294  inline ConstString(const char *str) : s(str) {}
295 
296  inline ConstString(const char *str, size_t len) : s(str, len) {}
297 
298  inline ConstString(size_t len, char c) : s(len, c) {}
299 
300  inline ~ConstString() {}
301 
302 
303  inline const char *c_str() const { return s.c_str(); }
304 
305  inline const char *data() const { return s.data(); }
306 
307  inline allocator_type get_allocator() const { return s.get_allocator(); }
308 
309  inline size_t copy(char* str, size_t len, size_t pos = 0) const { return s.copy(str, len, pos); }
310 
311  inline void push_back (char c) { s.push_back(c); }
312 
313 
314  inline ConstString& assign (const ConstString& str) {
315  this->s.assign(str);
316  return *this;
317  }
318 
319  inline ConstString& assign (const ConstString& str, size_t subpos, size_t sublen) {
320  this->s.assign(str, subpos, sublen);
321  return *this;
322  }
323 
324  inline ConstString& assign (const char* s) {
325  this->s.assign(s);
326  return *this;
327  }
328 
329  inline ConstString& assign(const char *s, size_t n) {
330  this->s.assign(s, n);
331  return *this;
332  }
333 
334  inline char& operator[](size_t idx) { return s[idx]; }
335  inline const char& operator[](size_t idx) const { return s[idx]; }
336 
337  inline char& at(size_t pos) { return s.at(pos); }
338  inline const char& at(size_t pos) const { return s.at(pos); }
339 
340  inline const ConstString& operator=(const ConstString& str) {
341  if (&str != this) {
342  s = str.s;
343  }
344  return *this;
345  }
346 
347  inline ConstString& operator=(const char* str) {
348  s = str;
349  return *this;
350  }
351 
352  inline ConstString& operator=(char c) {
353  s = c;
354  return *this;
355  }
356 
357  inline const ConstString& operator+=(char ch) {
358  s += ch;
359  return *this;
360  }
361 
362  inline const ConstString& operator+=(const char *str) {
363  s += str;
364  return *this;
365  }
366 
367  inline const ConstString& operator+=(const ConstString& str) {
368  s += str.s;
369  return *this;
370  }
371 
372  inline ConstString& erase(size_t pos = 0, size_t len = npos) {
373  s.erase(pos, len);
374  return *this;
375  }
376 
377  inline iterator erase(iterator p) {
378  return s.erase(p);
379  }
380 
381  inline iterator erase(iterator first, iterator last) {
382  return s.erase(first, last);
383  }
384 
385  inline void swap(ConstString& str) { s.swap(str.s); }
386 
387  inline size_t size() const { return s.length(); }
388 
389  inline size_t length() const { return s.length(); }
390 
391  inline size_t max_size() const { return s.max_size(); }
392 
393  inline void resize(size_t n) { s.resize(n); }
394  inline void resize(size_t n, char c) { s.resize(n, c); }
395 
396  inline size_t capacity() const { return s.capacity(); }
397 
398  inline void reserve(size_t n = 0) { s.reserve(n); }
399 
400  inline size_t find(const ConstString& needle, size_t start = 0) const { return s.find(needle.s, start); }
401  inline size_t find(const char *needle, size_t start = 0) const { return s.find(needle, start); }
402  inline size_t find(const char *needle, size_t start, size_t len) const { return s.find(needle, start, len); }
403  inline size_t find(char needle, size_t start = 0) const { return s.find(needle, start); }
404 
405  inline size_t rfind(const ConstString& needle, size_t start = npos) const { return s.rfind(needle.s, start); }
406  inline size_t rfind(const char *needle, size_t start = npos) const { return s.rfind(needle, start); }
407  inline size_t rfind(const char *needle, size_t start, size_t len) const { return s.rfind(needle, start, len); }
408  inline size_t rfind(char needle, size_t start = npos) const { return s.rfind(needle, start); }
409 
410  inline ConstString substr(size_t start = 0, size_t n = npos) const { return s.substr(start, n); }
411 
412 
413  unsigned long hash() const;
414 
415  inline void clear() { s.clear(); }
416 
417  inline bool empty() const { return s.empty(); }
418 
419  inline iterator begin() { return s.begin(); }
420  inline const_iterator begin() const { return s.begin(); }
421 
422  inline iterator end() { return s.end(); }
423  inline const_iterator end() const { return s.end(); }
424 
425  inline reverse_iterator rbegin() { return s.rbegin(); }
426  inline const_reverse_iterator rbegin() const { return s.rbegin(); }
427 
428  inline reverse_iterator rend() { return s.rend(); }
429  inline const_reverse_iterator rend() const { return s.rend(); }
430 
431  inline operator std::string() const
432  { return s; }
433 
434  inline ConstString(const std::string& str) {
435  s = str;
436  }
437 
438 private:
439  std::string s;
440 
441 #endif // YARP_WRAP_STL_STRING_INLINE
442 };
443 
444 namespace yarp {
445  namespace os {
450  inline std::istream& operator>>(std::istream& stream,
451  yarp::os::ConstString& str) {
452  stream >> (std::string&)str;
453  return stream;
454  }
461  inline std::ostream& operator<<(std::ostream& stream,
462  const yarp::os::ConstString& str) {
463  stream << (const std::string&)str;
464  return stream;
465  }
473  const yarp::os::ConstString& rhs) {
474  return operator+((const std::string&)lhs, (const std::string&)rhs);
475  }
476 
478  const char* rhs) {
479  return operator+((const std::string&)lhs, rhs);
480  }
481 
482  inline yarp::os::ConstString operator+(const char* lhs,
483  const yarp::os::ConstString& rhs) {
484  return operator+(lhs, (const std::string&)rhs);
485  }
486 
488  char rhs) {
489  return operator+((const std::string&)lhs, rhs);
490  }
491 
493  const yarp::os::ConstString& rhs) {
494  return operator+(lhs, (const std::string&)rhs);
495  }
502  inline bool operator==(const yarp::os::ConstString& lhs,
503  const yarp::os::ConstString& rhs) {
504  return operator==((const std::string&)lhs, (const std::string&)rhs);
505  }
506 
507  inline bool operator==(const char* lhs,
508  const yarp::os::ConstString& rhs) {
509  return operator==(lhs, (const std::string&)rhs);
510  }
511 
512  inline bool operator==(const yarp::os::ConstString& lhs,
513  const char* rhs) {
514  return operator==((const std::string&)lhs, rhs);
515  }
516 
517  inline bool operator!=(const yarp::os::ConstString& lhs,
518  const yarp::os::ConstString& rhs) {
519  return operator!=((const std::string&)lhs, (const std::string&)rhs);
520  }
521 
522  inline bool operator!=(const char* lhs,
523  const yarp::os::ConstString& rhs) {
524  return operator!=(lhs, (const std::string&)rhs);
525  }
526 
527  inline bool operator!=(const yarp::os::ConstString& lhs,
528  const char* rhs) {
529  return operator!=((const std::string&)lhs, rhs);
530  }
531 
532  inline bool operator<(const yarp::os::ConstString& lhs,
533  const yarp::os::ConstString& rhs) {
534  return operator<((const std::string&)lhs, (const std::string&)rhs);
535  }
536 
537  inline bool operator<(const char* lhs,
538  const yarp::os::ConstString& rhs) {
539  return operator<(lhs, (const std::string&)rhs);
540  }
541 
542  inline bool operator<(const yarp::os::ConstString& lhs,
543  const char* rhs) {
544  return operator<((const std::string&)lhs, rhs);
545  }
546 
547  inline bool operator<=(const yarp::os::ConstString& lhs,
548  const yarp::os::ConstString& rhs) {
549  return operator<=((const std::string&)lhs, (const std::string&)rhs);
550  }
551 
552  inline bool operator<=(const char* lhs,
553  const yarp::os::ConstString& rhs) {
554  return operator<=(lhs, (const std::string&)rhs);
555  }
556 
557  inline bool operator<=(const yarp::os::ConstString& lhs,
558  const char* rhs) {
559  return operator<=((const std::string&)lhs, rhs);
560  }
561 
562  inline bool operator>(const yarp::os::ConstString& lhs,
563  const yarp::os::ConstString& rhs) {
564  return operator>((const std::string&)lhs, (const std::string&)rhs);
565  }
566 
567  inline bool operator>(const char* lhs,
568  const yarp::os::ConstString& rhs) {
569  return operator>(lhs, (const std::string&)rhs);
570  }
571 
572  inline bool operator>(const yarp::os::ConstString& lhs,
573  const char* rhs) {
574  return operator>((const std::string&)lhs, rhs);
575  }
576 
577  inline bool operator>=(const yarp::os::ConstString& lhs,
578  const yarp::os::ConstString& rhs) {
579  return operator>=((const std::string&)lhs, (const std::string&)rhs);
580  }
581 
582  inline bool operator>=(const char* lhs,
583  const yarp::os::ConstString& rhs) {
584  return operator>=(lhs, (const std::string&)rhs);
585  }
586 
587  inline bool operator>=(const yarp::os::ConstString& lhs,
588  const char* rhs) {
589  return operator>=((const std::string&)lhs, rhs);
590  }
593  using std::swap;
594  }
595 }
596 
597 #ifndef DOXYGEN_SHOULD_SKIP_THIS
598 namespace std {
599  inline void swap(yarp::os::ConstString& x, yarp::os::ConstString& y) {
600  swap((std::string&)x, (std::string&)y);
601  }
602 
603  inline std::istream& getline(std::istream& is, yarp::os::ConstString& str, char delim) {
604 #ifdef YARP_WRAP_STL_STRING_INLINE
605  return getline(is, (std::string&)str, delim);
606 #else
607  std::string line;
608  getline(is, line, delim);
609  str = line;
610  return is;
611 #endif
612  }
613 
614  inline std::istream& getline(std::istream& is, yarp::os::ConstString& str) {
615 #ifdef YARP_WRAP_STL_STRING_INLINE
616  return getline(is, (std::string&)str);
617 #else
618  std::string line;
619  getline(is, line);
620  str = line;
621  return is;
622 #endif
623  }
624 
625 }
626 #endif // DOXYGEN_SHOULD_SKIP_THIS
627 
628 
629 
630 #endif // YARP_WRAP_STL_STRING
631 
632 #endif // YARP_OS_CONSTSTRING_H
633 
bool operator!=(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:517
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:532
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:562
STL namespace.
bool operator==(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Relational operations for ConstString.
Definition: ConstString.h:502
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:450
yarp::os::ConstString operator+(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Concatenate ConstString.
Definition: ConstString.h:472
std::ostream & operator<<(std::ostream &stream, const yarp::os::ConstString &str)
Insert string into stream.
Definition: ConstString.h:461
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:547
void swap(ConstString &str)
ConstString & assign(const 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
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:18
std::string::iterator iterator
Definition: ConstString.h:60
bool operator>=(const yarp::os::ConstString &lhs, const yarp::os::ConstString &rhs)
Definition: ConstString.h:577
ConstString(const std::string &str)
Definition: ConstString.h:148