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