add basic locale interface to ResPool, remove deprecated _gxx hashes
[platform/upstream/libzypp.git] / zypp / IdString.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/IdString.h
10  *
11 */
12 #ifndef ZYPP_SAT_IDSTR_H
13 #define ZYPP_SAT_IDSTR_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/SafeBool.h"
19
20 #include "zypp/sat/detail/PoolMember.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   ///////////////////////////////////////////////////////////////////
27   //
28   //    CLASS NAME : IdString
29   //
30   /** Access to the sat-pools string space.
31    *
32    * Construction from string will place a copy of the string in the
33    * string space, if it is not already present.
34    *
35    * While comparison differs between \ref IdString::Null and \ref IdString::Empty
36    * ( \c NULL and \c "" ), both are represented by an empty string \c "".
37    */
38   class IdString : protected sat::detail::PoolMember,
39                    private base::SafeBool<IdString>
40   {
41     public:
42       /** Default ctor, empty string. */
43       IdString() : _id( sat::detail::emptyId ) {}
44
45       /** Ctor from id. */
46       explicit IdString( sat::detail::IdType id_r ) : _id( id_r ) {}
47
48       /** Ctor from string. */
49       explicit IdString( const char * str_r );
50
51       /** Ctor from string. */
52       explicit IdString( const std::string & str_r );
53
54     public:
55       /** No or Null string ( Id \c 0 ). */
56       static const IdString Null;
57
58       /** Empty string. */
59       static const IdString Empty;
60
61     public:
62       /** Evaluate in a boolean context <tt>( != \c Null )</tt>. */
63       using base::SafeBool<IdString>::operator bool_type;
64
65       /** Whether the string is empty.
66        * This is true for \ref Null and \ref Empty.
67        */
68       bool empty() const
69       { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
70
71       /** The strings size. */
72       unsigned size() const;
73
74     public:
75       /** Conversion to <tt>const char *</tt> */
76       const char * c_str() const;
77
78       /** Conversion to <tt>std::string</tt> */
79       std::string asString() const
80       { return c_str(); }
81
82     public:
83       /** Fast compare equal. */
84       bool compareEQ( const IdString & rhs ) const
85       { return( _id == rhs.id() ); }
86
87       /** Compare IdString returning <tt>-1,0,1</tt>. */
88       int compare( const IdString & rhs ) const;
89
90       /** \overload */
91       int compare( const char * rhs ) const;
92
93       /** \overload */
94       int compare( const std::string & rhs ) const
95       { return compare( rhs.c_str() ); }
96
97     public:
98       /** Expert backdoor. */
99       sat::detail::IdType id() const
100       { return _id; }
101     private:
102       friend base::SafeBool<IdString>::operator bool_type() const;
103       bool boolTest() const { return _id; }
104     private:
105       sat::detail::IdType _id;
106   };
107   ///////////////////////////////////////////////////////////////////
108
109   /** \relates IdString Stream output */
110   std::ostream & operator<<( std::ostream & str, const IdString & obj );
111
112   /** \relates IdString Equal */
113   inline bool operator==( const IdString & lhs, const IdString & rhs )
114   { return lhs.compareEQ( rhs ); }
115   /** \overload */
116   inline bool operator==( const IdString & lhs, const char * rhs )
117   { return lhs.compare( rhs ) == 0; }
118   /** \overload */
119   inline bool operator==( const IdString & lhs, const std::string & rhs )
120   { return lhs.compare( rhs ) == 0; }
121   /** \overload */
122   inline bool operator==( const char * lhs, const IdString & rhs )
123   { return rhs.compare( lhs ) == 0; }
124   /** \overload */
125   inline bool operator==( const std::string & lhs, const IdString & rhs )
126   { return rhs.compare( lhs ) == 0; }
127
128   /** \relates IdString NotEqual */
129   inline bool operator!=( const IdString & lhs, const IdString & rhs )
130   { return ! lhs.compareEQ( rhs ); }
131   /** \overload */
132   inline bool operator!=( const IdString & lhs, const char * rhs )
133   { return lhs.compare( rhs ) != 0; }
134   /** \overload */
135   inline bool operator!=( const IdString & lhs, const std::string & rhs )
136   { return lhs.compare( rhs ) != 0; }
137   /** \overload */
138   inline bool operator!=( const char * lhs, const IdString & rhs )
139   { return rhs.compare( lhs ) != 0; }
140   /** \overload */
141   inline bool operator!=( const std::string & lhs, const IdString & rhs )
142   { return rhs.compare( lhs ) != 0; }
143
144   /** \relates IdString Less */
145   inline bool operator<( const IdString & lhs, const IdString & rhs )
146   { return lhs.compare( rhs ) < 0; }
147   /** \overload */
148   inline bool operator<( const IdString & lhs, const char * rhs )
149   { return lhs.compare( rhs ) < 0; }
150   /** \overload */
151   inline bool operator<( const IdString & lhs, const std::string & rhs )
152   { return lhs.compare( rhs ) < 0; }
153   /** \overload */
154   inline bool operator<( const char * lhs, const IdString & rhs )
155   { return rhs.compare( lhs ) >= 0; }
156   /** \overload */
157   inline bool operator<( const std::string & lhs, const IdString & rhs )
158   { return rhs.compare( lhs ) >= 0; }
159
160   /** \relates IdString LessEqual*/
161   inline bool operator<=( const IdString & lhs, const IdString & rhs )
162   { return lhs.compare( rhs ) <= 0; }
163   /** \overload */
164   inline bool operator<=( const IdString & lhs, const char * rhs )
165   { return lhs.compare( rhs ) <= 0; }
166   /** \overload */
167   inline bool operator<=( const IdString & lhs, const std::string & rhs )
168   { return lhs.compare( rhs ) <= 0; }
169   /** \overload */
170   inline bool operator<=( const char * lhs, const IdString & rhs )
171   { return rhs.compare( lhs ) > 0; }
172   /** \overload */
173   inline bool operator<=( const std::string & lhs, const IdString & rhs )
174   { return rhs.compare( lhs ) > 0; }
175
176   /** \relates IdString Greater */
177   inline bool operator>( const IdString & lhs, const IdString & rhs )
178   { return lhs.compare( rhs ) > 0; }
179   /** \overload */
180   inline bool operator>( const IdString & lhs, const char * rhs )
181   { return lhs.compare( rhs ) > 0; }
182   /** \overload */
183   inline bool operator>( const IdString & lhs, const std::string & rhs )
184   { return lhs.compare( rhs ) > 0; }
185   /** \overload */
186   inline bool operator>( const char * lhs, const IdString & rhs )
187   { return rhs.compare( lhs ) <= 0; }
188   /** \overload */
189   inline bool operator>( const std::string & lhs, const IdString & rhs )
190   { return rhs.compare( lhs ) <= 0; }
191
192   /** \relates IdString GreaterEqual */
193   inline bool operator>=( const IdString & lhs, const IdString & rhs )
194   { return lhs.compare( rhs ) >= 0; }
195   /** \overload */
196   inline bool operator>=( const IdString & lhs, const char * rhs )
197   { return lhs.compare( rhs ) >= 0; }
198   /** \overload */
199   inline bool operator>=( const IdString & lhs, const std::string & rhs )
200   { return lhs.compare( rhs ) >= 0; }
201   /** \overload */
202   inline bool operator>=( const char * lhs, const IdString & rhs )
203   { return rhs.compare( lhs ) < 0; }
204   /** \overload */
205   inline bool operator>=( const std::string & lhs, const IdString & rhs )
206   { return rhs.compare( lhs ) < 0; }
207
208   /////////////////////////////////////////////////////////////////
209 } // namespace zypp
210 ///////////////////////////////////////////////////////////////////
211
212 ZYPP_DEFINE_ID_HASHABLE( ::zypp::IdString )
213
214 #endif // ZYPP_SAT_IDSTR_H