bioformats  5.1.6
String.h
1 /*
2  * #%L
3  * OME-XERCES C++ library for working with Xerces C++.
4  * %%
5  * Copyright © 2006 - 2015 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(xercesc::XMLString::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(xercesc::XMLString::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(xercesc::XMLString::transcode(str.c_str()))
121  {
122  assert(this->narrow != 0);
123  assert(this->wide != 0);
124  }
125 
129  inline
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 
251  private:
253  char *narrow;
255  XMLCh *wide;
256  };
257 
265  inline ::std::ostream&
266  operator<< (::std::ostream& os,
267  const String& str)
268  {
269  return os << static_cast<std::string>(str);
270  }
271 
272  }
273  }
274 }
275 
276 #endif // OME_COMMON_XML_STRING_H
277 
278 /*
279  * Local Variables:
280  * mode:C++
281  * End:
282  */
XMLCh * wide
The XMLCh * string representation.
Definition: String.h:255
String(const char *str)
Construct a String from a NUL-terminated string.
Definition: String.h:102
bool operator==(const char *rhs)
Compare a String for equality with a C string.
Definition: String.h:185
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
inline::std::string str() const
Get the String content as a std::string.
Definition: String.h:171
String(std::string const &str)
Construct a String from a std::string.
Definition: String.h:118
bool operator!=(const char *rhs)
Compare a String for inequality with a C string.
Definition: String.h:222
String(const XMLCh *str)
Construct a String from an XMLCh * string.
Definition: String.h:86
Xerces string wrapper.
Definition: String.h:74
char * narrow
The char * string representation.
Definition: String.h:253
~String()
Destructor.
Definition: String.h:130
inline::std::ostream & operator<<(::std::ostream &os, const String &str)
Output String to output stream.
Definition: String.h:266
Xerces-C modern C++ wrapper.
Definition: Base.h:53