Imported Upstream version 17.23.5
[platform/upstream/libzypp.git] / zypp / parser / xml / XmlString.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/xml/XmlString.h
10  *
11 */
12 #ifndef ZYPP_PARSER_XML_XMLSTRING_H
13 #define ZYPP_PARSER_XML_XMLSTRING_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include <zypp/base/PtrTypes.h>
19
20 #include <zypp/parser/xml/libxmlfwd.h>
21 #include <zypp/parser/xml/XmlEscape.h>
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26   ///////////////////////////////////////////////////////////////////
27   namespace xml
28   { /////////////////////////////////////////////////////////////////
29
30     ///////////////////////////////////////////////////////////////////
31     //
32     //  CLASS NAME : XmlString
33     //
34     /** <tt>xmlChar *</tt> wrapper.
35      *
36      * Common handling of <tt>xmlChar *</tt> that do or do not need to
37      * be freed. If the wraped <tt>xmlChar *</tt> needs to be freed by
38      * calling \c xmlFree, pass \c FREE as 2nd argument to the ctor.
39      **/
40     class XmlString
41     {
42       /** shared_ptr custom deleter calling \c xmlFree. */
43       struct Deleter
44       {
45         void operator()( const xmlChar * xmlstr_r ) const
46         { xmlFree( (void*)(xmlstr_r) ); }
47       };
48
49     public:
50
51       /** Dtor policy. */
52       enum OnDelete { NOFREE, FREE };
53
54       /** Ctor from xmlChar.
55        * Pass \c FREE as 2nd arg if \c xmlFree needs to be called on destruction.
56       */
57       XmlString( const xmlChar *const xmlstr_r = NULL,
58                  OnDelete ondelete_r           = NOFREE );
59
60       /** Access the <tt>xmlChar *</tt>. */
61       const xmlChar * get() const
62       {
63         if ( ! _xmlstr )
64           return NULL;
65         return &(*_xmlstr);
66       }
67
68       /** Implicit conversion to <tt>xmlChar *</tt>. */
69       operator const xmlChar * () const
70       { return get(); }
71
72       /** Explicit conversion to <tt>const char *</tt>. */
73       const char * c_str() const
74       { return reinterpret_cast<const char *const>(get()); }
75
76       /** Explicit conversion to <tt>std::string</tt>. */
77       std::string asString() const
78       {
79         if ( ! _xmlstr )
80           return std::string();
81         return c_str();
82       }
83
84       bool operator==( const std::string & rhs ) const
85       { return( rhs == c_str() ); }
86
87       bool operator!=( const std::string & rhs ) const
88       { return( rhs != c_str() ); }
89
90       bool operator==( const char *const rhs ) const
91       { return( asString() == rhs ); }
92
93       bool operator!=( const char *const rhs ) const
94       { return( asString() != rhs ); }
95
96       bool operator==( const XmlString & rhs ) const
97       { return( asString() == rhs.c_str() ); }
98
99       bool operator!=( const XmlString & rhs ) const
100       { return( asString() != rhs.c_str() ); }
101
102     private:
103       /** Wraps the <tt>xmlChar *</tt>.
104        * The appropriate custom deleter is set by the ctor. */
105       shared_ptr<const xmlChar> _xmlstr;
106     };
107     ///////////////////////////////////////////////////////////////////
108
109     /** \relates XmlString Stream output. */
110     std::ostream & operator<<( std::ostream & str, const XmlString & obj );
111
112     /////////////////////////////////////////////////////////////////
113   } // namespace xml
114   ///////////////////////////////////////////////////////////////////
115   /////////////////////////////////////////////////////////////////
116 } // namespace zypp
117 ///////////////////////////////////////////////////////////////////
118 #endif // ZYPP_PARSER_XML_XMLSTRING_H