1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/IdString.h
12 #ifndef ZYPP_SAT_IDSTR_H
13 #define ZYPP_SAT_IDSTR_H
17 #include <string_view>
19 #include <boost/utility/string_ref_fwd.hpp>
21 #include <zypp/sat/detail/PoolMember.h>
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
28 typedef std::unordered_set<IdString> IdStringSet;
30 ///////////////////////////////////////////////////////////////////
32 // CLASS NAME : IdString
34 /** Access to the sat-pools string space.
36 * Construction from string will place a copy of the string in the
37 * string space, if it is not already present.
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 "".
42 class IdString : protected sat::detail::PoolMember
45 typedef sat::detail::IdType IdType;
48 /** Default ctor, empty string. */
49 constexpr IdString() : _id( sat::detail::emptyId ) {}
52 constexpr explicit IdString( IdType id_r ) : _id( id_r ) {}
54 /** Ctor from string. */
55 explicit IdString( const char * str_r );
57 /** Ctor from string (pointer,length). */
58 IdString( const char * str_r, unsigned len_r );
60 /** Ctor from string. */
61 explicit IdString( const std::string & str_r );
63 /** Ctor from boost::string_ref. */
64 explicit IdString( boost::string_ref str_r );
66 #ifdef __cpp_lib_string_view
67 explicit IdString( std::string_view str_r )
68 : IdString( str_r.data(), str_r.size() )
73 /** No or Null string ( Id \c 0 ). */
74 static const IdString Null;
77 static const IdString Empty;
80 /** Evaluate in a boolean context <tt>( != \c Null )</tt>. */
81 constexpr explicit operator bool() const
84 /** Whether the string is empty.
85 * This is true for \ref Null and \ref Empty.
87 constexpr bool empty() const
88 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
90 /** The strings size. */
91 unsigned size() const;
94 /** Conversion to <tt>const char *</tt> */
95 const char * c_str() const;
97 /** Conversion to <tt>std::string</tt> */
98 std::string asString() const
101 /** Explicit conversion to std::string */
102 explicit operator std::string() const
103 { return asString(); }
106 /** Fast compare equal. */
107 bool compareEQ( const IdString & rhs ) const
108 { return( _id == rhs.id() ); }
110 /** Compare IdString returning <tt>-1,0,1</tt>. */
111 int compare( const IdString & rhs ) const;
114 int compare( const char * rhs ) const;
117 int compare( const std::string & rhs ) const
118 { return compare( rhs.c_str() ); }
121 /** Expert backdoor. */
128 ///////////////////////////////////////////////////////////////////
130 /** \relates IdString Stream output */
131 std::ostream & operator<<( std::ostream & str, const IdString & obj );
133 /** \relates IdString Stream output */
134 std::ostream & dumpOn( std::ostream & str, const IdString & obj );
136 /** \relates IdString Equal */
137 inline bool operator==( const IdString & lhs, const IdString & rhs )
138 { return lhs.compareEQ( rhs ); }
140 inline bool operator==( const IdString & lhs, const char * rhs )
141 { return lhs.compare( rhs ) == 0; }
143 inline bool operator==( const IdString & lhs, const std::string & rhs )
144 { return lhs.compare( rhs ) == 0; }
146 inline bool operator==( const char * lhs, const IdString & rhs )
147 { return rhs.compare( lhs ) == 0; }
149 inline bool operator==( const std::string & lhs, const IdString & rhs )
150 { return rhs.compare( lhs ) == 0; }
152 /** \relates IdString NotEqual */
153 inline bool operator!=( const IdString & lhs, const IdString & rhs )
154 { return ! lhs.compareEQ( rhs ); }
156 inline bool operator!=( const IdString & lhs, const char * rhs )
157 { return lhs.compare( rhs ) != 0; }
159 inline bool operator!=( const IdString & lhs, const std::string & rhs )
160 { return lhs.compare( rhs ) != 0; }
162 inline bool operator!=( const char * lhs, const IdString & rhs )
163 { return rhs.compare( lhs ) != 0; }
165 inline bool operator!=( const std::string & lhs, const IdString & rhs )
166 { return rhs.compare( lhs ) != 0; }
168 /** \relates IdString Less */
169 inline bool operator<( const IdString & lhs, const IdString & rhs )
170 { return lhs.compare( rhs ) < 0; }
172 inline bool operator<( const IdString & lhs, const char * rhs )
173 { return lhs.compare( rhs ) < 0; }
175 inline bool operator<( const IdString & lhs, const std::string & rhs )
176 { return lhs.compare( rhs ) < 0; }
178 inline bool operator<( const char * lhs, const IdString & rhs )
179 { return rhs.compare( lhs ) >= 0; }
181 inline bool operator<( const std::string & lhs, const IdString & rhs )
182 { return rhs.compare( lhs ) >= 0; }
184 /** \relates IdString LessEqual*/
185 inline bool operator<=( const IdString & lhs, const IdString & rhs )
186 { return lhs.compare( rhs ) <= 0; }
188 inline bool operator<=( const IdString & lhs, const char * rhs )
189 { return lhs.compare( rhs ) <= 0; }
191 inline bool operator<=( const IdString & lhs, const std::string & rhs )
192 { return lhs.compare( rhs ) <= 0; }
194 inline bool operator<=( const char * lhs, const IdString & rhs )
195 { return rhs.compare( lhs ) > 0; }
197 inline bool operator<=( const std::string & lhs, const IdString & rhs )
198 { return rhs.compare( lhs ) > 0; }
200 /** \relates IdString Greater */
201 inline bool operator>( const IdString & lhs, const IdString & rhs )
202 { return lhs.compare( rhs ) > 0; }
204 inline bool operator>( const IdString & lhs, const char * rhs )
205 { return lhs.compare( rhs ) > 0; }
207 inline bool operator>( const IdString & lhs, const std::string & rhs )
208 { return lhs.compare( rhs ) > 0; }
210 inline bool operator>( const char * lhs, const IdString & rhs )
211 { return rhs.compare( lhs ) <= 0; }
213 inline bool operator>( const std::string & lhs, const IdString & rhs )
214 { return rhs.compare( lhs ) <= 0; }
216 /** \relates IdString GreaterEqual */
217 inline bool operator>=( const IdString & lhs, const IdString & rhs )
218 { return lhs.compare( rhs ) >= 0; }
220 inline bool operator>=( const IdString & lhs, const char * rhs )
221 { return lhs.compare( rhs ) >= 0; }
223 inline bool operator>=( const IdString & lhs, const std::string & rhs )
224 { return lhs.compare( rhs ) >= 0; }
226 inline bool operator>=( const char * lhs, const IdString & rhs )
227 { return rhs.compare( lhs ) < 0; }
229 inline bool operator>=( const std::string & lhs, const IdString & rhs )
230 { return rhs.compare( lhs ) < 0; }
232 /////////////////////////////////////////////////////////////////
234 ///////////////////////////////////////////////////////////////////
236 ZYPP_DEFINE_ID_HASHABLE( ::zypp::IdString );
238 #endif // ZYPP_SAT_IDSTR_H