libzypp
10.5.0
|
00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #ifndef ZYPP_SAT_ATTRMATCHER_H 00013 #define ZYPP_SAT_ATTRMATCHER_H 00014 00015 extern "C" 00016 { 00017 struct _Datamatcher; 00018 } 00019 00020 #include <iosfwd> 00021 #include <string> 00022 00023 #include "zypp/base/PtrTypes.h" 00024 #include "zypp/base/SafeBool.h" 00025 #include "zypp/base/Exception.h" 00026 00028 namespace zypp 00029 { 00030 00032 // 00033 // CLASS NAME : Match 00034 // 00041 class Match : private base::SafeBool<Match> 00042 { 00043 private: 00044 static const int _modemask; 00045 static const int _flagmask; 00046 00047 public: 00049 enum Mode 00050 { 00051 NOTHING, 00052 STRING, 00053 STRINGSTART, 00054 STRINGEND, 00055 SUBSTRING, 00056 GLOB, 00057 REGEX, 00058 OTHER 00059 }; 00060 00069 static const Match NOCASE; 00071 static const Match NO_STORAGE_SOLVABLE; 00073 static const Match SUB; 00075 static const Match ARRAYSENTINEL; 00077 static const Match SKIP_KIND; 00079 static const Match FILES; 00081 00082 public: 00084 Match() 00085 : _val( 0 ) 00086 {} 00087 00089 Match( Mode val_r ) 00090 : _val( modeval( val_r ) ) 00091 {} 00092 00094 explicit Match( int val_r ) 00095 : _val( val_r ) 00096 {} 00097 00098 #ifndef SWIG // Swig treats it as syntax error 00099 00100 using base::SafeBool<Match>::operator bool_type; 00101 #endif 00102 00103 public: 00105 bool test( const Match & rhs ) const 00106 { return ( ( flagval() & rhs.flagval() ) == rhs.flagval() ) 00107 && ( !rhs.modeval() || rhs.modeval() == modeval() ); } 00108 00110 bool testAnyOf( const Match & rhs ) const 00111 { return ( flagval() & rhs.flagval() ) 00112 || ( rhs.modeval() && rhs.modeval() == modeval() ); } 00113 00115 void set( const Match & rhs ) 00116 { 00117 if ( rhs.modeval() ) 00118 _val = rhs._val | flagval(); // also set the rhs mode 00119 else 00120 _val |= rhs._val; // just set the flags 00121 } 00122 00124 void unset( const Match & rhs ) 00125 { 00126 if ( modeval() == rhs.modeval() ) 00127 _val = flagval() & ~rhs.flagval(); // also unset mode 00128 else 00129 _val &= ~rhs.flagval(); // just unset falgs 00130 } 00131 00133 void turn( const Match & rhs, bool onoff ) 00134 { onoff ? set( rhs ) : unset( rhs ); } 00135 00137 Match & operator|=( const Match & rhs ) 00138 { set( rhs ); return *this; } 00139 00141 Match & operator-=( const Match & rhs ) 00142 { unset( rhs ); return *this; } 00143 00144 public: 00146 Mode mode() const; 00147 00149 Match flags() const 00150 { return Match( flagval() ); } 00151 00152 public: 00156 int get() const { return _val; } 00158 int modeval() const { return _val & _modemask; } 00160 int flagval() const { return _val & _flagmask; } 00162 00163 public: 00167 bool isMode( Mode rhs ) const 00168 { return modeval() == modeval( rhs ); } 00170 bool isModeString() const 00171 { return isMode( STRING ); } 00173 bool isModeStringstart() const 00174 { return isMode( STRINGSTART ); } 00176 bool isModeStringend() const 00177 { return isMode( STRINGEND ); } 00179 bool isModeSubstring() const 00180 { return isMode( SUBSTRING ); } 00182 bool isModeGlob() const 00183 { return isMode( GLOB ); } 00185 bool isModeRegex() const 00186 { return isMode( REGEX ); } 00187 00189 void setMode( Mode rhs ) 00190 { _val = modeval( rhs ) | flagval(); } 00192 void setModeString() 00193 { setMode( STRING ); } 00195 void setModeStringstart() 00196 { setMode( STRINGSTART ); } 00198 void setModeStringend() 00199 { setMode( STRINGEND ); } 00201 void setModeSubstring() 00202 { setMode( SUBSTRING ); } 00204 void setModeGlob() 00205 { setMode( GLOB ); } 00207 void setModeRegex() 00208 { setMode( REGEX ); } 00210 00212 std::string asString() const; 00213 00214 private: 00215 friend base::SafeBool<Match>::operator bool_type() const; 00216 bool boolTest() const { return _val; } 00217 00219 static int modeval( Mode mode_r ); 00220 00221 private: 00222 int _val; 00223 }; 00224 00226 inline bool operator==( const Match & lhs, const Match & rhs ) 00227 { return lhs.get() == rhs.get(); } 00229 inline bool operator!=( const Match & lhs, const Match & rhs ) 00230 { return lhs.get() != rhs.get(); } 00231 00233 inline Match operator|( const Match & lhs, const Match & rhs ) 00234 { return Match(lhs) |= rhs; } 00236 inline Match operator|( Match::Mode lhs, Match::Mode rhs ) 00237 { return Match(lhs) |= rhs; } 00238 00240 inline Match operator-( const Match & lhs, const Match & rhs ) 00241 { return Match(lhs) -= rhs; } 00243 inline Match operator-( Match::Mode lhs, Match::Mode rhs ) 00244 { return Match(lhs) -= rhs; } 00245 00247 std::ostream & operator<<( std::ostream & str, Match::Mode obj ); 00248 00250 std::ostream & operator<<( std::ostream & str, const Match & obj ); 00251 00252 00254 00256 // 00257 // CLASS NAME : MatchException 00258 // 00260 struct MatchException : public Exception 00261 { 00263 explicit MatchException( const std::string & msg_r ) : Exception( msg_r ) {} 00264 }; 00265 00267 struct MatchUnknownModeException : public MatchException 00268 { 00270 explicit MatchUnknownModeException( const std::string & msg_r ) : MatchException( msg_r ) {} 00271 00273 MatchUnknownModeException( const Match & mode_r, const std::string & msg_r = std::string() ); 00274 }; 00275 00277 struct MatchInvalidRegexException : public MatchException 00278 { 00280 explicit MatchInvalidRegexException( const std::string & msg_r ) : MatchException( msg_r ) {} 00281 00283 MatchInvalidRegexException( const std::string & regex_r, int regcomp_r ); 00284 }; 00285 00287 00289 namespace sat 00290 { 00291 00293 // 00294 // CLASS NAME : AttrMatcher 00295 // 00312 class AttrMatcher : private base::SafeBool<AttrMatcher> 00313 { 00314 friend std::ostream & operator<<( std::ostream & str, const AttrMatcher & obj ); 00315 00316 public: 00317 typedef MatchException Exception; 00318 00319 public: 00321 class Impl; 00322 00323 public: 00325 AttrMatcher(); 00326 00328 AttrMatcher( const std::string & search_r ); 00329 00331 AttrMatcher( const std::string & search_r, const Match & flags_r ); 00332 00337 AttrMatcher( const std::string & search_r, const Match::Mode & flags_r ); 00338 00340 AttrMatcher( const std::string & search_r, int flags_r ); 00341 00342 #ifndef SWIG // Swig treats it as syntax error 00343 00344 using base::SafeBool<AttrMatcher>::operator bool_type; 00345 #endif 00346 00347 public: 00353 template<class _Tp> 00354 bool operator()( const _Tp & string_r ) const 00355 { return doMatch( string_r.c_str() ); } 00357 bool operator()( const char * string_r ) const 00358 { return doMatch( string_r ); } 00359 00360 public: 00362 const std::string & searchstring() const; 00363 00365 void setSearchstring( const std::string & string_r ); 00366 00368 void setSearchstring( const std::string & string_r, const Match & flags_r ); 00369 00371 const Match & flags() const; 00372 00374 void setFlags( const Match & flags_r ); 00375 00376 public: 00383 void compile() const; 00384 00386 bool isCompiled() const; 00387 00392 bool doMatch( const char * string_r ) const; 00393 00394 private: 00395 friend base::SafeBool<AttrMatcher>::operator bool_type() const; 00396 bool boolTest() const 00397 { return !searchstring().empty(); } 00398 00399 private: 00401 RWCOW_pointer<Impl> _pimpl; 00402 }; 00404 00406 std::ostream & operator<<( std::ostream & str, const AttrMatcher & obj ); 00407 00409 bool operator==( const AttrMatcher & lhs, const AttrMatcher & rhs ); 00410 00412 inline bool operator!=( const AttrMatcher & lhs, const AttrMatcher & rhs ) 00413 { return !( lhs == rhs ); } 00414 00416 bool operator<( const AttrMatcher & lhs, const AttrMatcher & rhs ); 00417 00419 } // namespace sat 00422 } // namespace zypp 00424 #endif // ZYPP_SAT_ATTRMATCHER_H