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