ome-common 5.3.2

String.h
1 /*
2 * #%L
3 * OME-XERCES C++ library for working with Xerces C++.
4 * %%
5 * Copyright © 2006 - 2016 Open Microscopy Environment:
6 * - Massachusetts Institute of Technology
7 * - National Institutes of Health
8 * - University of Dundee
9 * - Board of Regents of the University of Wisconsin-Madison
10 * - Glencoe Software, Inc.
11 * %%
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * The views and conclusions contained in the software and documentation are
34 * those of the authors and should not be interpreted as representing official
35 * policies, either expressed or implied, of any organization.
36 * #L%
37 */
38
39 #ifndef OME_COMMON_XML_STRING_H
40 #define OME_COMMON_XML_STRING_H
41
42 #include <cassert>
43 #include <string>
44 #include <ostream>
45
46 #include <xercesc/util/XMLString.hpp>
47
48 namespace ome
49 {
50 namespace common
51 {
52 namespace xml
53 {
54
74 class String
75 {
76 public:
85 inline
86 String(const XMLCh *str):
87 narrow(transcode(str)),
88 wide(xercesc::XMLString::replicate(str))
89 {
90 assert(this->wide != 0);
91 assert(this->narrow != 0);
92 }
93
101 inline
102 String(const char *str):
103 narrow(xercesc::XMLString::replicate(str)),
104 wide(transcode(str))
105 {
106 assert(this->narrow != 0);
107 assert(this->wide != 0);
108 }
109
117 inline
118 String(std::string const& str):
119 narrow(xercesc::XMLString::replicate(str.c_str())),
120 wide(transcode(str.c_str()))
121 {
122 assert(this->narrow != 0);
123 assert(this->wide != 0);
124 }
125
129 inline
130 ~String()
131 {
132 if (narrow)
133 xercesc::XMLString::release(&narrow);
134 if (wide)
135 xercesc::XMLString::release(&wide);
136 }
137
143 inline
144 operator const XMLCh *() const
145 {
146 assert(this->wide != 0);
147
148 return wide;
149 }
150
156 inline
157 operator ::std::string() const
158 {
159 assert(this->narrow != 0);
160
161 return narrow;
162 }
163
169 inline
170 ::std::string
171 str() const
172 {
173 assert(this->narrow != 0);
174
175 return narrow;
176 }
177
184 bool
185 operator== (const char *rhs)
186 {
187 return this->narrow != 0 && strcmp(this->narrow, rhs) == 0;
188 }
189
196 bool
197 operator== (const std::string& rhs)
198 {
199 return this->narrow != 0 && strcmp(this->narrow, rhs.c_str()) == 0;
200 }
201
208 bool
209 operator== (const String& rhs)
210 {
211 return this->narrow != 0 && rhs.narrow != 0 &&
212 strcmp(this->narrow, rhs.narrow) == 0;
213 }
214
221 bool
222 operator!= (const char *rhs)
223 {
224 return !(*this == rhs);
225 }
226
233 bool
234 operator!= (const std::string& rhs)
235 {
236 return !(*this == rhs);
237 }
238
245 bool
246 operator!= (const String& rhs)
247 {
248 return !(*this == rhs);
249 }
250
258 static
259 char *
260 transcode(const XMLCh *str);
261
269 static
270 XMLCh *
271 transcode(const char *str);
272
273 private:
275 char *narrow;
277 XMLCh *wide;
278 };
279
287 inline ::std::ostream&
288 operator<< (::std::ostream& os,
289 const String& str)
290 {
291 return os << static_cast<std::string>(str);
292 }
293
294 }
295 }
296 }
297
298 #endif // OME_COMMON_XML_STRING_H
299
300 /*
301 * Local Variables:
302 * mode:C++
303 * End:
304 */
ome::common::xml::String::wide
XMLCh * wide
The XMLCh * string representation.
Definition: String.h:277
ome::common::xml::String::String
String(const char *str)
Construct a String from a NUL-terminated string.
Definition: String.h:102
ome::common::xml::String::str
inline ::std::string str() const
Get the String content as a std::string.
Definition: String.h:171
ome::common::xml::String::transcode
static char * transcode(const XMLCh *str)
Transcode UTF-16 string to UTF-8 string.
Definition: String.cpp:54
ome::common::xml::String::operator==
bool operator==(const char *rhs)
Compare a String for equality with a C string.
Definition: String.h:185
ome
Open Microscopy Environment C++.
Definition: base64.h:49
ome::common::xml::String::String
String(std::string const &str)
Construct a String from a std::string.
Definition: String.h:118
ome::common::xml::String::operator!=
bool operator!=(const char *rhs)
Compare a String for inequality with a C string.
Definition: String.h:222
ome::common::xml::String::String
String(const XMLCh *str)
Construct a String from an XMLCh * string.
Definition: String.h:86
ome::common::xml::String
Xerces string wrapper.
Definition: String.h:74
ome::common::xml::String::narrow
char * narrow
The char * string representation.
Definition: String.h:275
ome::common::xml::String::~String
~String()
Destructor.
Definition: String.h:130
ome::common::xml::operator<<
inline ::std::ostream & operator<<(::std::ostream &os, const String &str)
Output String to output stream.
Definition: String.h:288