1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/IdString.h
12 #ifndef ZYPP_SAT_IDSTR_H
13 #define ZYPP_SAT_IDSTR_H
18 #include "zypp/sat/detail/PoolMember.h"
20 ///////////////////////////////////////////////////////////////////
22 { /////////////////////////////////////////////////////////////////
25 typedef std::tr1::unordered_set<IdString> IdStringSet;
27 ///////////////////////////////////////////////////////////////////
29 // CLASS NAME : IdString
31 /** Access to the sat-pools string space.
33 * Construction from string will place a copy of the string in the
34 * string space, if it is not already present.
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 "".
39 class IdString : protected sat::detail::PoolMember
42 typedef sat::detail::IdType IdType;
45 /** Default ctor, empty string. */
46 IdString() : _id( sat::detail::emptyId ) {}
49 explicit IdString( IdType id_r ) : _id( id_r ) {}
51 /** Ctor from string. */
52 explicit IdString( const char * str_r );
54 /** Ctor from string. */
55 explicit IdString( const std::string & str_r );
58 /** No or Null string ( Id \c 0 ). */
59 static const IdString Null;
62 static const IdString Empty;
65 /** Evaluate in a boolean context <tt>( != \c Null )</tt>. */
66 explicit operator bool() const
69 /** Whether the string is empty.
70 * This is true for \ref Null and \ref Empty.
73 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
75 /** The strings size. */
76 unsigned size() const;
79 /** Conversion to <tt>const char *</tt> */
80 const char * c_str() const;
82 /** Conversion to <tt>std::string</tt> */
83 std::string asString() const
87 /** Fast compare equal. */
88 bool compareEQ( const IdString & rhs ) const
89 { return( _id == rhs.id() ); }
91 /** Compare IdString returning <tt>-1,0,1</tt>. */
92 int compare( const IdString & rhs ) const;
95 int compare( const char * rhs ) const;
98 int compare( const std::string & rhs ) const
99 { return compare( rhs.c_str() ); }
102 /** Expert backdoor. */
109 ///////////////////////////////////////////////////////////////////
111 /** \relates IdString Stream output */
112 std::ostream & operator<<( std::ostream & str, const IdString & obj );
114 /** \relates IdString Stream output */
115 std::ostream & dumpOn( std::ostream & str, const IdString & obj );
117 /** \relates IdString Equal */
118 inline bool operator==( const IdString & lhs, const IdString & rhs )
119 { return lhs.compareEQ( rhs ); }
121 inline bool operator==( const IdString & lhs, const char * rhs )
122 { return lhs.compare( rhs ) == 0; }
124 inline bool operator==( const IdString & lhs, const std::string & rhs )
125 { return lhs.compare( rhs ) == 0; }
127 inline bool operator==( const char * lhs, const IdString & rhs )
128 { return rhs.compare( lhs ) == 0; }
130 inline bool operator==( const std::string & lhs, const IdString & rhs )
131 { return rhs.compare( lhs ) == 0; }
133 /** \relates IdString NotEqual */
134 inline bool operator!=( const IdString & lhs, const IdString & rhs )
135 { return ! lhs.compareEQ( rhs ); }
137 inline bool operator!=( const IdString & lhs, const char * rhs )
138 { return lhs.compare( rhs ) != 0; }
140 inline bool operator!=( const IdString & lhs, const std::string & rhs )
141 { return lhs.compare( rhs ) != 0; }
143 inline bool operator!=( const char * lhs, const IdString & rhs )
144 { return rhs.compare( lhs ) != 0; }
146 inline bool operator!=( const std::string & lhs, const IdString & rhs )
147 { return rhs.compare( lhs ) != 0; }
149 /** \relates IdString Less */
150 inline bool operator<( const IdString & lhs, const IdString & rhs )
151 { return lhs.compare( rhs ) < 0; }
153 inline bool operator<( const IdString & lhs, const char * rhs )
154 { return lhs.compare( rhs ) < 0; }
156 inline bool operator<( const IdString & lhs, const std::string & rhs )
157 { return lhs.compare( rhs ) < 0; }
159 inline bool operator<( const char * lhs, const IdString & rhs )
160 { return rhs.compare( lhs ) >= 0; }
162 inline bool operator<( const std::string & lhs, const IdString & rhs )
163 { return rhs.compare( lhs ) >= 0; }
165 /** \relates IdString LessEqual*/
166 inline bool operator<=( const IdString & lhs, const IdString & rhs )
167 { return lhs.compare( rhs ) <= 0; }
169 inline bool operator<=( const IdString & lhs, const char * rhs )
170 { return lhs.compare( rhs ) <= 0; }
172 inline bool operator<=( const IdString & lhs, const std::string & rhs )
173 { return lhs.compare( rhs ) <= 0; }
175 inline bool operator<=( const char * lhs, const IdString & rhs )
176 { return rhs.compare( lhs ) > 0; }
178 inline bool operator<=( const std::string & lhs, const IdString & rhs )
179 { return rhs.compare( lhs ) > 0; }
181 /** \relates IdString Greater */
182 inline bool operator>( const IdString & lhs, const IdString & rhs )
183 { return lhs.compare( rhs ) > 0; }
185 inline bool operator>( const IdString & lhs, const char * rhs )
186 { return lhs.compare( rhs ) > 0; }
188 inline bool operator>( const IdString & lhs, const std::string & rhs )
189 { return lhs.compare( rhs ) > 0; }
191 inline bool operator>( const char * lhs, const IdString & rhs )
192 { return rhs.compare( lhs ) <= 0; }
194 inline bool operator>( const std::string & lhs, const IdString & rhs )
195 { return rhs.compare( lhs ) <= 0; }
197 /** \relates IdString GreaterEqual */
198 inline bool operator>=( const IdString & lhs, const IdString & rhs )
199 { return lhs.compare( rhs ) >= 0; }
201 inline bool operator>=( const IdString & lhs, const char * rhs )
202 { return lhs.compare( rhs ) >= 0; }
204 inline bool operator>=( const IdString & lhs, const std::string & rhs )
205 { return lhs.compare( rhs ) >= 0; }
207 inline bool operator>=( const char * lhs, const IdString & rhs )
208 { return rhs.compare( lhs ) < 0; }
210 inline bool operator>=( const std::string & lhs, const IdString & rhs )
211 { return rhs.compare( lhs ) < 0; }
213 /////////////////////////////////////////////////////////////////
215 ///////////////////////////////////////////////////////////////////
217 ZYPP_DEFINE_ID_HASHABLE( ::zypp::IdString );
219 #endif // ZYPP_SAT_IDSTR_H