1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/IdString.h
12 #ifndef ZYPP_SAT_IDSTR_H
13 #define ZYPP_SAT_IDSTR_H
18 #include "zypp/base/SafeBool.h"
20 #include "zypp/sat/detail/PoolMember.h"
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 // CLASS NAME : IdString
30 /** Access to the sat-pools string space.
32 * Construction from string will place a copy of the string in the
33 * string space, if it is not already present.
35 * While comparison differs between \ref IdString::Null and \ref IdString::Empty
36 * ( \c NULL and \c "" ), both are represented by an empty string \c "".
38 class IdString : protected sat::detail::PoolMember,
39 private base::SafeBool<IdString>
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 using base::SafeBool<IdString>::operator bool_type;
68 /** Whether the string is empty.
69 * This is true for \ref Null and \ref Empty.
72 { return( _id == sat::detail::emptyId || _id == sat::detail::noId ); }
74 /** The strings size. */
75 unsigned size() const;
78 /** Conversion to <tt>const char *</tt> */
79 const char * c_str() const;
81 /** Conversion to <tt>std::string</tt> */
82 std::string asString() const
86 /** Fast compare equal. */
87 bool compareEQ( const IdString & rhs ) const
88 { return( _id == rhs.id() ); }
90 /** Compare IdString returning <tt>-1,0,1</tt>. */
91 int compare( const IdString & rhs ) const;
94 int compare( const char * rhs ) const;
97 int compare( const std::string & rhs ) const
98 { return compare( rhs.c_str() ); }
101 /** Expert backdoor. */
105 friend base::SafeBool<IdString>::operator bool_type() const;
106 bool boolTest() const { return _id; }
110 ///////////////////////////////////////////////////////////////////
112 /** \relates IdString Stream output */
113 std::ostream & operator<<( std::ostream & str, const IdString & obj );
115 /** \relates IdString Stream output */
116 std::ostream & dumpOn( std::ostream & str, const IdString & obj );
118 /** \relates IdString Equal */
119 inline bool operator==( const IdString & lhs, const IdString & rhs )
120 { return lhs.compareEQ( rhs ); }
122 inline bool operator==( const IdString & lhs, const char * rhs )
123 { return lhs.compare( rhs ) == 0; }
125 inline bool operator==( const IdString & lhs, const std::string & rhs )
126 { return lhs.compare( rhs ) == 0; }
128 inline bool operator==( const char * lhs, const IdString & rhs )
129 { return rhs.compare( lhs ) == 0; }
131 inline bool operator==( const std::string & lhs, const IdString & rhs )
132 { return rhs.compare( lhs ) == 0; }
134 /** \relates IdString NotEqual */
135 inline bool operator!=( const IdString & lhs, const IdString & rhs )
136 { return ! lhs.compareEQ( rhs ); }
138 inline bool operator!=( const IdString & lhs, const char * rhs )
139 { return lhs.compare( rhs ) != 0; }
141 inline bool operator!=( const IdString & lhs, const std::string & rhs )
142 { return lhs.compare( rhs ) != 0; }
144 inline bool operator!=( const char * lhs, const IdString & rhs )
145 { return rhs.compare( lhs ) != 0; }
147 inline bool operator!=( const std::string & lhs, const IdString & rhs )
148 { return rhs.compare( lhs ) != 0; }
150 /** \relates IdString Less */
151 inline bool operator<( const IdString & lhs, const IdString & rhs )
152 { return lhs.compare( rhs ) < 0; }
154 inline bool operator<( const IdString & lhs, const char * rhs )
155 { return lhs.compare( rhs ) < 0; }
157 inline bool operator<( const IdString & lhs, const std::string & rhs )
158 { return lhs.compare( rhs ) < 0; }
160 inline bool operator<( const char * lhs, const IdString & rhs )
161 { return rhs.compare( lhs ) >= 0; }
163 inline bool operator<( const std::string & lhs, const IdString & rhs )
164 { return rhs.compare( lhs ) >= 0; }
166 /** \relates IdString LessEqual*/
167 inline bool operator<=( const IdString & lhs, const IdString & rhs )
168 { return lhs.compare( rhs ) <= 0; }
170 inline bool operator<=( const IdString & lhs, const char * rhs )
171 { return lhs.compare( rhs ) <= 0; }
173 inline bool operator<=( const IdString & lhs, const std::string & rhs )
174 { return lhs.compare( rhs ) <= 0; }
176 inline bool operator<=( const char * lhs, const IdString & rhs )
177 { return rhs.compare( lhs ) > 0; }
179 inline bool operator<=( const std::string & lhs, const IdString & rhs )
180 { return rhs.compare( lhs ) > 0; }
182 /** \relates IdString Greater */
183 inline bool operator>( const IdString & lhs, const IdString & rhs )
184 { return lhs.compare( rhs ) > 0; }
186 inline bool operator>( const IdString & lhs, const char * rhs )
187 { return lhs.compare( rhs ) > 0; }
189 inline bool operator>( const IdString & lhs, const std::string & rhs )
190 { return lhs.compare( rhs ) > 0; }
192 inline bool operator>( const char * lhs, const IdString & rhs )
193 { return rhs.compare( lhs ) <= 0; }
195 inline bool operator>( const std::string & lhs, const IdString & rhs )
196 { return rhs.compare( lhs ) <= 0; }
198 /** \relates IdString GreaterEqual */
199 inline bool operator>=( const IdString & lhs, const IdString & rhs )
200 { return lhs.compare( rhs ) >= 0; }
202 inline bool operator>=( const IdString & lhs, const char * rhs )
203 { return lhs.compare( rhs ) >= 0; }
205 inline bool operator>=( const IdString & lhs, const std::string & rhs )
206 { return lhs.compare( rhs ) >= 0; }
208 inline bool operator>=( const char * lhs, const IdString & rhs )
209 { return rhs.compare( lhs ) < 0; }
211 inline bool operator>=( const std::string & lhs, const IdString & rhs )
212 { return rhs.compare( lhs ) < 0; }
214 /////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////
218 ZYPP_DEFINE_ID_HASHABLE( ::zypp::IdString );
220 #endif // ZYPP_SAT_IDSTR_H