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