YARP  2.3.70.1
Yet Another Robot Platform
ConstString.cpp
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 
8 #include <yarp/os/ConstString.h>
9 
10 #ifndef YARP_WRAP_STL_STRING
11 int __dummy_symbol_yarp_os_conststring = 1;
12 #else
13 
14 
15 #include <yarp/os/Log.h>
16 #include <yarp/os/NetType.h>
17 
18 #include <string>
19 
20 using namespace yarp::os;
21 
22 
23 
24 const size_t ConstString::npos = std::string::npos;
25 
26 #ifndef YARP_WRAP_STL_STRING_INLINE
27 
28  // implementation is a std::string
29 #define HELPER(x) (*((std::string*)(x)))
30 
32  implementation = new std::string();
33  yAssert(implementation != YARP_NULLPTR);
34 }
35 
37  implementation = new std::string(HELPER(str.implementation));
38  yAssert(implementation != YARP_NULLPTR);
39 }
40 
41 ConstString::ConstString(const ConstString& str, size_t pos, size_t len) {
42  implementation = new std::string(HELPER(str.implementation), pos, len);
43 }
44 
45 ConstString::ConstString(const char *str) {
46  implementation = new std::string(str);
47  yAssert(implementation != YARP_NULLPTR);
48 }
49 
50 ConstString::ConstString(const char *str, size_t len) {
51  implementation = new std::string(str, len);
52  yAssert(implementation != YARP_NULLPTR);
53 }
54 
55 void ConstString::init(const char *str, size_t len) {
56  implementation = new std::string(str, len);
57  yAssert(implementation != YARP_NULLPTR);
58 }
59 
60 ConstString::ConstString(size_t len, char c) {
61  implementation = new std::string(len, c);
62  yAssert(implementation != YARP_NULLPTR);
63 }
64 
66  if (implementation != YARP_NULLPTR) {
67  delete (std::string*)implementation;
68  implementation = YARP_NULLPTR;
69  }
70 }
71 
72 
74  HELPER(implementation) = HELPER(str.implementation);
75  return (*this);
76 }
77 
79  HELPER(implementation) = str;
80  return (*this);
81 }
82 
85  return (*this);
86 }
87 
88 const char *ConstString::c_str() const {
89  return HELPER(implementation).c_str();
90 }
91 
92 const char *ConstString::data() const {
93  return HELPER(implementation).data();
94 }
95 
97  return HELPER(implementation).get_allocator();
98 }
99 
100 size_t ConstString::copy(char* str, size_t len, size_t pos) const {
101 #ifdef _MSC_VER
102  #pragma warning(push)
103  #pragma warning(disable : 4996)
104 #endif
105  return HELPER(implementation).copy(str, len, pos);
106 #ifdef _MSC_VER
107  #pragma warning(pop)
108 #endif
109 }
110 
111 size_t ConstString::length() const {
112  return HELPER(implementation).length();
113 }
114 
115 size_t ConstString::size() const {
116  return HELPER(implementation).size();
117 }
118 
119 size_t ConstString::max_size() const {
120  return HELPER(implementation).max_size();
121 }
122 
123 void ConstString::resize(size_t n) {
124  HELPER(implementation).resize(n);
125 }
126 
127 void ConstString::resize(size_t n, char c) {
128  HELPER(implementation).resize(n, c);
129 }
130 
131 size_t ConstString::capacity() const {
132  return HELPER(implementation).capacity();
133 }
134 
135 void ConstString::reserve(size_t n) {
136  HELPER(implementation).reserve(n);
137 }
138 
140  HELPER(implementation) += ch;
141  return *this;
142 }
143 
145  HELPER(implementation) += str;
146  return *this;
147 }
148 
150  HELPER(implementation) += HELPER(str.implementation);
151  return *this;
152 }
153 
154 void ConstString::push_back (char c) {
155  HELPER(implementation).push_back(c);
156 }
157 
158 ConstString& ConstString::erase(size_t pos, size_t len) {
159  HELPER(implementation).erase(pos, len);
160  return *this;
161 }
162 
164  return HELPER(implementation).erase(p);
165 }
166 
168  return HELPER(implementation).erase(first, last);
169 }
170 
172  HELPER(implementation).swap(HELPER(str.implementation));
173 }
174 
175 size_t ConstString::find(const ConstString& needle, size_t start) const {
176  return HELPER(implementation).find(HELPER(needle.implementation), start);
177 }
178 
179 size_t ConstString::find(const char *needle, size_t start) const {
180  return HELPER(implementation).find(needle, start);
181 }
182 
183 size_t ConstString::find(const char *needle, size_t start, size_t len) const {
184  return HELPER(implementation).find(needle, start, len);
185 }
186 
187 size_t ConstString::find(char needle, size_t start) const {
188  return HELPER(implementation).find(needle, start);
189 }
190 
191 size_t ConstString::rfind(const ConstString& needle, size_t start) const {
192  return HELPER(implementation).rfind(HELPER(needle.implementation), start);
193 }
194 
195 size_t ConstString::rfind(const char *needle, size_t start) const {
196  return HELPER(implementation).rfind(needle, start);
197 }
198 
199 size_t ConstString::rfind(const char *needle, size_t start, size_t len) const {
200  return HELPER(implementation).rfind(needle, start, len);
201 }
202 
203 size_t ConstString::rfind(char needle, size_t start) const {
204  return HELPER(implementation).rfind(needle, start);
205 }
206 
207 ConstString ConstString::substr(size_t start, size_t n) const {
208  std::string result = HELPER(implementation).substr(start, n);
209  return ConstString(result.c_str(), result.length());
210 }
211 
213  HELPER(implementation).clear();
214 }
215 
216 bool ConstString::empty() const {
217  return HELPER(implementation).empty();
218 }
219 
221  return HELPER(implementation).begin();
222 }
223 
225  return HELPER(implementation).begin();
226 }
227 
229  return HELPER(implementation).end();
230 }
231 
233  return HELPER(implementation).end();
234 }
235 
237  return HELPER(implementation).rbegin();
238 }
239 
241  return HELPER(implementation).rbegin();
242 }
243 
245  return HELPER(implementation).rend();
246 }
247 
249  return HELPER(implementation).rend();
250 }
251 
252 char& ConstString::operator[](size_t idx) {
253  return HELPER(implementation)[idx];
254 }
255 
256 const char& ConstString::operator[](size_t idx) const {
257  return HELPER(implementation)[idx];
258 }
259 
260 char& ConstString::at(size_t pos) {
261  return HELPER(implementation).at(pos);
262 }
263 
264 const char& ConstString::at(size_t pos) const {
265  return HELPER(implementation).at(pos);
266 }
267 
268 ConstString& ConstString::assign(const char *s, size_t n) {
269  HELPER(implementation).assign(s, n);
270  return *this;
271 }
272 
273 #endif
274 
275 unsigned long ConstString::hash() const {
276 #ifndef YARP_WRAP_STL_STRING_INLINE
277  std::string& x = HELPER(implementation);
278 #else
279  const std::string& x = s;
280 #endif
281  unsigned long h = 0;
282  for (size_t i=0; i<x.length(); i++) {
283  unsigned char ch = x[i];
284  h = (h << 4) + (ch * 13);
285  unsigned long g = h & 0xf0000000;
286  if (g) {
287  h ^= (g>>24);
288  h ^= g;
289  }
290  }
291  return h;
292 }
293 
294 #endif
reverse_iterator rend()
ConstString substr(size_t start=0, size_t n=npos) const
Generate a substring.
size_t size() const
std::string::const_iterator const_iterator
Definition: ConstString.h:61
size_t rfind(const ConstString &needle, size_t start=npos) const
Find the last occurrence of content in string.
ConstString & operator=(const ConstString &str)
Assignment operator.
Definition: ConstString.cpp:73
size_t max_size() const
std::reverse_iterator< iterator > reverse_iterator
Definition: ConstString.h:62
ConstString & operator+=(const ConstString &str)
A string with almost the same api as std::string.
Definition: ConstString.h:44
static const size_t npos
Definition: ConstString.h:47
char & operator[](size_t idx)
ConstString()
Constructor.
Definition: ConstString.cpp:31
std::allocator< char > allocator_type
Definition: ConstString.h:51
ConstString & erase(size_t pos=0, size_t len=npos)
allocator_type get_allocator() const
Get allocator.
Definition: ConstString.cpp:96
size_t capacity() const
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: ConstString.h:63
char & at(size_t pos)
dest g
Definition: ImageCopy.cpp:91
void reserve(size_t n=0)
#define yAssert(x)
Definition: Log.h:118
unsigned long hash() const
size_t length() const
~ConstString()
Destructor.
Definition: ConstString.cpp:65
void push_back(char c)
#define HELPER(x)
Definition: ConstString.cpp:29
void resize(size_t n)
void swap(ConstString &str)
const char * c_str() const
Accesses the character sequence stored in this object.
Definition: ConstString.cpp:88
dest h
Definition: ImageCopy.cpp:63
size_t copy(char *str, size_t len, size_t pos=0) const
Copy sequence of characters from string.
std::string::iterator iterator
Definition: ConstString.h:60
size_t find(const ConstString &needle, size_t start=0) const
Find content in string.
An interface to the operating system, including Port based communication.
void * implementation
Definition: Module.h:218
reverse_iterator rbegin()
ConstString & assign(const char *s, size_t n)
const char * data() const
Get string data.
Definition: ConstString.cpp:92