CMS 3D CMS Logo

XercesStrUtils.h
Go to the documentation of this file.
1 #ifndef UTILITIES_XERCES_STRING_UTILS_H
2 #define UTILITIES_XERCES_STRING_UTILS_H
3 
4 #include <xercesc/util/XercesDefs.hpp>
5 #include <xercesc/util/XMLString.hpp>
6 #include <memory>
7 #include <sstream>
8 
9 namespace cms {
10  namespace xerces {
11 
12 #ifdef XERCES_CPP_NAMESPACE_USE
14 #endif
15 
16  inline void dispose(XMLCh* ptr) { XMLString::release(&ptr); }
17  inline void dispose(char* ptr) { XMLString::release(&ptr); }
18 
19  template <class CharType>
20  class ZStr // Zero-terminated string.
21  {
22  public:
23  ZStr(CharType const* str) : m_array(const_cast<CharType*>(str), &dispose) {}
24 
25  CharType const* ptr() const { return m_array.get(); }
26 
27  private:
28  std::unique_ptr<CharType, void (*)(CharType*)> m_array;
29  };
30 
31  inline ZStr<XMLCh> uStr(char const* str) { return ZStr<XMLCh>(XMLString::transcode(str)); }
32 
33  inline ZStr<char> cStr(XMLCh const* str) { return ZStr<char>(XMLString::transcode(str)); }
34 
35  inline std::string toString(XMLCh const* toTranscode) { return std::string(cStr(toTranscode).ptr()); }
36 
37  inline unsigned int toUInt(XMLCh const* toTranscode) {
38  std::istringstream iss(toString(toTranscode));
39  unsigned int returnValue;
40  iss >> returnValue;
41  return returnValue;
42  }
43 
44  inline bool toBool(XMLCh const* toTranscode) {
45  std::string value = toString(toTranscode);
46  if ((value == "true") || (value == "1"))
47  return true;
48  return false;
49  }
50 
51  inline double toDouble(XMLCh const* toTranscode) {
52  std::istringstream iss(toString(toTranscode));
53  double returnValue;
54  iss >> returnValue;
55  return returnValue;
56  }
57  } // namespace xerces
58 } // namespace cms
59 
60 #endif
double toDouble(XMLCh const *toTranscode)
void dispose(XMLCh *ptr)
std::string toString(XMLCh const *toTranscode)
ZStr(CharType const *str)
ZStr< char > cStr(XMLCh const *str)
unsigned int toUInt(XMLCh const *toTranscode)
ZStr< XMLCh > uStr(char const *str)
Definition: value.py:1
Namespace of DDCMS conversion namespace.
std::unique_ptr< CharType, void(*)(CharType *)> m_array
CharType const * ptr() const
#define str(s)
bool toBool(XMLCh const *toTranscode)