1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/IdString.h
12 #ifndef ZYPP_SAT_IDSTR_H
13 #define ZYPP_SAT_IDSTR_H
18 #include <boost/utility/string_ref_fwd.hpp>
20 #include "zypp/sat/detail/PoolMember.h"
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
27 typedef std::unordered_set<IdString> IdStringSet;
29 ///////////////////////////////////////////////////////////////////
31 // CLASS NAME : IdString
33 /** Access to the sat-pools string space.
35 * Construction from string will place a copy of the string in the
36 * string space, if it is not already present.
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 "".
41 class IdString : protected sat::detail::PoolMember
44 typedef sat::detail::IdType IdType;
47 /** Default ctor, empty string. */
48 constexpr IdString() : _id( sat::detail::emptyId ) {}
51 constexpr explicit IdString( IdType id_r ) : _id( id_r ) {}
53 /** Ctor from string. */
54 explicit IdString( const char * str_r );
56 /** Ctor from string (pointer,length). */
57 IdString( const char * str_r, unsigned len_r );
59 /** Ctor from string. */
60 explicit IdString( const std::string & str_r );
62 /** Ctor from boost::string_ref. */
63 explicit IdString( boost::string_ref str_r );
66 /** No or Null string ( Id \c 0 ). */
67 static const IdString Null;
70 static const IdString Empty;
73 /** Evaluate in a boolean context <tt>( != \c Null )</tt>. */
74 constexpr explicit operator bool() const
77 /** Whether the string is empty.
78 * This is true for \ref Null and \ref Empty.
80 constexpr bool empty() const
81 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
83 /** The strings size. */
84 unsigned size() const;
87 /** Conversion to <tt>const char *</tt> */
88 const char * c_str() const;
90 /** Conversion to <tt>std::string</tt> */
91 std::string asString() const
94 /** Explicit conversion to std::string */
95 explicit operator std::string() const
96 { return asString(); }
99 /** Fast compare equal. */
100 bool compareEQ( const IdString & rhs ) const
101 { return( _id == rhs.id() ); }
103 /** Compare IdString returning <tt>-1,0,1</tt>. */
104 int compare( const IdString & rhs ) const;
107 int compare( const char * rhs ) const;
110 int compare( const std::string & rhs ) const
111 { return compare( rhs.c_str() ); }
114 /** Expert backdoor. */
121 ///////////////////////////////////////////////////////////////////
123 /** \relates IdString Stream output */
124 std::ostream & operator<<( std::ostream & str, const IdString & obj );
126 /** \relates IdString Stream output */
127 std::ostream & dumpOn( std::ostream & str, const IdString & obj );
129 /** \relates IdString Equal */
130 inline bool operator==( const IdString & lhs, const IdString & rhs )
131 { return lhs.compareEQ( rhs ); }
133 inline bool operator==( const IdString & lhs, const char * rhs )
134 { return lhs.compare( rhs ) == 0; }
136 inline bool operator==( const IdString & lhs, const std::string & rhs )
137 { return lhs.compare( rhs ) == 0; }
139 inline bool operator==( const char * lhs, const IdString & rhs )
140 { return rhs.compare( lhs ) == 0; }
142 inline bool operator==( const std::string & lhs, const IdString & rhs )
143 { return rhs.compare( lhs ) == 0; }
145 /** \relates IdString NotEqual */
146 inline bool operator!=( const IdString & lhs, const IdString & rhs )
147 { return ! lhs.compareEQ( rhs ); }
149 inline bool operator!=( const IdString & lhs, const char * rhs )
150 { return lhs.compare( rhs ) != 0; }
152 inline bool operator!=( const IdString & lhs, const std::string & rhs )
153 { return lhs.compare( rhs ) != 0; }
155 inline bool operator!=( const char * lhs, const IdString & rhs )
156 { return rhs.compare( lhs ) != 0; }
158 inline bool operator!=( const std::string & lhs, const IdString & rhs )
159 { return rhs.compare( lhs ) != 0; }
161 /** \relates IdString Less */
162 inline bool operator<( const IdString & lhs, const IdString & rhs )
163 { return lhs.compare( rhs ) < 0; }
165 inline bool operator<( const IdString & lhs, const char * rhs )
166 { return lhs.compare( rhs ) < 0; }
168 inline bool operator<( const IdString & lhs, const std::string & rhs )
169 { return lhs.compare( rhs ) < 0; }
171 inline bool operator<( const char * lhs, const IdString & rhs )
172 { return rhs.compare( lhs ) >= 0; }
174 inline bool operator<( const std::string & lhs, const IdString & rhs )
175 { return rhs.compare( lhs ) >= 0; }
177 /** \relates IdString LessEqual*/
178 inline bool operator<=( const IdString & lhs, const IdString & rhs )
179 { return lhs.compare( rhs ) <= 0; }
181 inline bool operator<=( const IdString & lhs, const char * rhs )
182 { return lhs.compare( rhs ) <= 0; }
184 inline bool operator<=( const IdString & lhs, const std::string & rhs )
185 { return lhs.compare( rhs ) <= 0; }
187 inline bool operator<=( const char * lhs, const IdString & rhs )
188 { return rhs.compare( lhs ) > 0; }
190 inline bool operator<=( const std::string & lhs, const IdString & rhs )
191 { return rhs.compare( lhs ) > 0; }
193 /** \relates IdString Greater */
194 inline bool operator>( const IdString & lhs, const IdString & rhs )
195 { return lhs.compare( rhs ) > 0; }
197 inline bool operator>( const IdString & lhs, const char * rhs )
198 { return lhs.compare( rhs ) > 0; }
200 inline bool operator>( const IdString & lhs, const std::string & rhs )
201 { return lhs.compare( rhs ) > 0; }
203 inline bool operator>( const char * lhs, const IdString & rhs )
204 { return rhs.compare( lhs ) <= 0; }
206 inline bool operator>( const std::string & lhs, const IdString & rhs )
207 { return rhs.compare( lhs ) <= 0; }
209 /** \relates IdString GreaterEqual */
210 inline bool operator>=( const IdString & lhs, const IdString & rhs )
211 { return lhs.compare( rhs ) >= 0; }
213 inline bool operator>=( const IdString & lhs, const char * rhs )
214 { return lhs.compare( rhs ) >= 0; }
216 inline bool operator>=( const IdString & lhs, const std::string & rhs )
217 { return lhs.compare( rhs ) >= 0; }
219 inline bool operator>=( const char * lhs, const IdString & rhs )
220 { return rhs.compare( lhs ) < 0; }
222 inline bool operator>=( const std::string & lhs, const IdString & rhs )
223 { return rhs.compare( lhs ) < 0; }
225 /////////////////////////////////////////////////////////////////
227 ///////////////////////////////////////////////////////////////////
229 ZYPP_DEFINE_ID_HASHABLE( ::zypp::IdString );
231 #endif // ZYPP_SAT_IDSTR_H