libzypp  17.38.7
IdStringType.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_IDSTRINGTYPE_H
13 #define ZYPP_IDSTRINGTYPE_H
14 
15 #include <zypp/IdString.h>
16 #include <string.h> // for strcmp
17 
18 #if ( LEGACY(1735) ) && !ZYPPNG
20 #endif
21 
23 namespace zypp
24 {
25 
27  //
28  // CLASS NAME : IdStringType<Derived>
29  //
90  template <class Derived>
92  #if ( LEGACY(1735) ) && !ZYPPNG
93  : protected sat::detail::PoolMember
94  #endif
95  {
96  public:
98 
99  protected:
100  IdStringType() = default;
101  IdStringType(const IdStringType &) = default;
102  IdStringType &operator=(const IdStringType &) = default;
103  IdStringType(IdStringType &&) noexcept = default;
104  IdStringType &operator=(IdStringType &&) noexcept = default;
105  ~IdStringType() = default;
106 
107  private:
108  const Derived & self() const { return *static_cast<const Derived*>( this ); }
109 
110  public:
111  IdString idStr() const { return self()._str; }
112 
113  bool empty() const { return idStr().empty(); }
114  unsigned size() const { return idStr().size(); }
115  const char * c_str() const { return idStr().c_str(); }
116  std::string asString() const { return idStr().asString(); }
117 
118 #ifdef __cpp_lib_string_view
119  std::string_view asStringView() const { return idStr().asStringView(); }
120  explicit operator std::string_view() const { return asStringView(); }
121 #endif
122 
123  IdType id() const { return idStr().id(); }
124 
125  public:
127  explicit operator bool() const
128  { return ! empty(); }
129 
131  explicit operator IdString() const
132  { return idStr(); }
133 
135  explicit operator std::string() const
136  { return asString(); }
137 
138  public:
139  // - break it down to idString/const char* <=> idString/cont char*
140  // - handle idString(0)/NULL being the least value
141  // - everything else goes to _doCompare (no NULL)
142  static int compare( const Derived & lhs, const Derived & rhs ) { return compare( lhs.idStr(), rhs.idStr() ); }
143  static int compare( const Derived & lhs, const IdString & rhs ) { return compare( lhs.idStr(), rhs ); }
144  static int compare( const Derived & lhs, const std::string & rhs ) { return compare( lhs.idStr(), rhs.c_str() ); }
145  static int compare( const Derived & lhs, const char * rhs ) { return compare( lhs.idStr(), rhs );}
146 
147  static int compare( const IdString & lhs, const Derived & rhs ) { return compare( lhs, rhs.idStr() ); }
148  static int compare( const IdString & lhs, const IdString & rhs ) { return lhs == rhs ? 0 : Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ),
149  (rhs ? rhs.c_str() : (const char *)0 ) ); }
150  static int compare( const IdString & lhs, const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
151  static int compare( const IdString & lhs, const char * rhs ) { return Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ), rhs ); }
152 
153  static int compare( const std::string & lhs, const Derived & rhs ) { return compare( lhs.c_str(), rhs.idStr() ); }
154  static int compare( const std::string & lhs, const IdString & rhs ) { return compare( lhs.c_str(), rhs ); }
155  static int compare( const std::string & lhs, const std::string & rhs ) { return compare( lhs.c_str(), rhs.c_str() ); }
156  static int compare( const std::string & lhs, const char * rhs ) { return compare( lhs.c_str(), rhs ); }
157 
158  static int compare( const char * lhs, const Derived & rhs ) { return compare( lhs, rhs.idStr() ); }
159  static int compare( const char * lhs, const IdString & rhs ) { return Derived::_doCompare( lhs, (rhs ? rhs.c_str() : (const char *)0 ) ); }
160  static int compare( const char * lhs, const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
161  static int compare( const char * lhs, const char * rhs ) { return Derived::_doCompare( lhs, rhs ); }
162 
163  public:
164  int compare( const Derived & rhs ) const { return compare( idStr(), rhs.idStr() ); }
165  int compare( const IdStringType & rhs ) const { return compare( idStr(), rhs.idStr() ); }
166  int compare( const IdString & rhs ) const { return compare( idStr(), rhs ); }
167  int compare( const std::string & rhs ) const { return compare( idStr(), rhs.c_str() ); }
168  int compare( const char * rhs ) const { return compare( idStr(), rhs ); }
169 
170  private:
171  static inline int _doCompare( const char * lhs, const char * rhs ) ZYPP_API
172  {
173  if ( ! lhs ) return rhs ? -1 : 0;
174  return rhs ? ::strcmp( lhs, rhs ) : 1;
175  }
176  };
178 
180  template <class Derived>
181  inline std::ostream & operator<<( std::ostream & str, const IdStringType<Derived> & obj )
182  { return str << obj.c_str(); }
183 
185  template <class Derived>
186  inline bool operator==( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
187  { return lhs.compare( rhs ) == 0; }
189  template <class Derived>
190  inline bool operator==( const IdStringType<Derived> & lhs, const IdString & rhs )
191  { return lhs.compare( rhs ) == 0; }
193  template <class Derived>
194  inline bool operator==( const IdStringType<Derived> & lhs, const char * rhs )
195  { return lhs.compare( rhs ) == 0; }
197  template <class Derived>
198  inline bool operator==( const IdStringType<Derived> & lhs, const std::string & rhs )
199  { return lhs.compare( rhs ) == 0; }
201  template <class Derived>
202  inline bool operator==( const IdString & lhs, const IdStringType<Derived> & rhs )
203  { return rhs.compare( lhs ) == 0; }
205  template <class Derived>
206  inline bool operator==( const char * lhs, const IdStringType<Derived> & rhs )
207  { return rhs.compare( lhs ) == 0; }
209  template <class Derived>
210  inline bool operator==( const std::string & lhs, const IdStringType<Derived> & rhs )
211  { return rhs.compare( lhs ) == 0; }
212 
214  template <class Derived>
215  inline bool operator!=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
216  { return lhs.compare( rhs ) != 0; }
218  template <class Derived>
219  inline bool operator!=( const IdStringType<Derived> & lhs, const IdString & rhs )
220  { return lhs.compare( rhs ) != 0; }
222  template <class Derived>
223  inline bool operator!=( const IdStringType<Derived> & lhs, const char * rhs )
224  { return lhs.compare( rhs ) != 0; }
226  template <class Derived>
227  inline bool operator!=( const IdStringType<Derived> & lhs, const std::string & rhs )
228  { return lhs.compare( rhs ) != 0; }
230  template <class Derived>
231  inline bool operator!=( const IdString & lhs, const IdStringType<Derived> & rhs )
232  { return rhs.compare( lhs ) != 0; }
234  template <class Derived>
235  inline bool operator!=( const char * lhs, const IdStringType<Derived> & rhs )
236  { return rhs.compare( lhs ) != 0; }
238  template <class Derived>
239  inline bool operator!=( const std::string & lhs, const IdStringType<Derived> & rhs )
240  { return rhs.compare( lhs ) != 0; }
241 
243  template <class Derived>
244  inline bool operator<( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
245  { return lhs.compare( rhs ) < 0; }
247  template <class Derived>
248  inline bool operator<( const IdStringType<Derived> & lhs, const IdString & rhs )
249  { return lhs.compare( rhs ) < 0; }
251  template <class Derived>
252  inline bool operator<( const IdStringType<Derived> & lhs, const char * rhs )
253  { return lhs.compare( rhs ) < 0; }
255  template <class Derived>
256  inline bool operator<( const IdStringType<Derived> & lhs, const std::string & rhs )
257  { return lhs.compare( rhs ) < 0; }
259  template <class Derived>
260  inline bool operator<( const IdString & lhs, const IdStringType<Derived> & rhs )
261  { return rhs.compare( lhs ) >= 0; }
263  template <class Derived>
264  inline bool operator<( const char * lhs, const IdStringType<Derived> & rhs )
265  { return rhs.compare( lhs ) >= 0; }
267  template <class Derived>
268  inline bool operator<( const std::string & lhs, const IdStringType<Derived> & rhs )
269  { return rhs.compare( lhs ) >= 0; }
270 
272  template <class Derived>
273  inline bool operator<=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
274  { return lhs.compare( rhs ) <= 0; }
276  template <class Derived>
277  inline bool operator<=( const IdStringType<Derived> & lhs, const IdString & rhs )
278  { return lhs.compare( rhs ) <= 0; }
280  template <class Derived>
281  inline bool operator<=( const IdStringType<Derived> & lhs, const char * rhs )
282  { return lhs.compare( rhs ) <= 0; }
284  template <class Derived>
285  inline bool operator<=( const IdStringType<Derived> & lhs, const std::string & rhs )
286  { return lhs.compare( rhs ) <= 0; }
288  template <class Derived>
289  inline bool operator<=( const IdString & lhs, const IdStringType<Derived> & rhs )
290  { return rhs.compare( lhs ) > 0; }
292  template <class Derived>
293  inline bool operator<=( const char * lhs, const IdStringType<Derived> & rhs )
294  { return rhs.compare( lhs ) > 0; }
296  template <class Derived>
297  inline bool operator<=( const std::string & lhs, const IdStringType<Derived> & rhs )
298  { return rhs.compare( lhs ) > 0; }
299 
301  template <class Derived>
302  inline bool operator>( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
303  { return lhs.compare( rhs ) > 0; }
305  template <class Derived>
306  inline bool operator>( const IdStringType<Derived> & lhs, const IdString & rhs )
307  { return lhs.compare( rhs ) > 0; }
309  template <class Derived>
310  inline bool operator>( const IdStringType<Derived> & lhs, const char * rhs )
311  { return lhs.compare( rhs ) > 0; }
313  template <class Derived>
314  inline bool operator>( const IdStringType<Derived> & lhs, const std::string & rhs )
315  { return lhs.compare( rhs ) > 0; }
317  template <class Derived>
318  inline bool operator>( const IdString & lhs, const IdStringType<Derived> & rhs )
319  { return rhs.compare( lhs ) <= 0; }
321  template <class Derived>
322  inline bool operator>( const char * lhs, const IdStringType<Derived> & rhs )
323  { return rhs.compare( lhs ) <= 0; }
325  template <class Derived>
326  inline bool operator>( const std::string & lhs, const IdStringType<Derived> & rhs )
327  { return rhs.compare( lhs ) <= 0; }
328 
330  template <class Derived>
331  inline bool operator>=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
332  { return lhs.compare( rhs ) >= 0; }
334  template <class Derived>
335  inline bool operator>=( const IdStringType<Derived> & lhs, const IdString & rhs )
336  { return lhs.compare( rhs ) >= 0; }
338  template <class Derived>
339  inline bool operator>=( const IdStringType<Derived> & lhs, const char * rhs )
340  { return lhs.compare( rhs ) >= 0; }
342  template <class Derived>
343  inline bool operator>=( const IdStringType<Derived> & lhs, const std::string & rhs )
344  { return lhs.compare( rhs ) >= 0; }
346  template <class Derived>
347  inline bool operator>=( const IdString & lhs, const IdStringType<Derived> & rhs )
348  { return rhs.compare( lhs ) < 0; }
350  template <class Derived>
351  inline bool operator>=( const char * lhs, const IdStringType<Derived> & rhs )
352  { return rhs.compare( lhs ) < 0; }
354  template <class Derived>
355  inline bool operator>=( const std::string & lhs, const IdStringType<Derived> & rhs )
356  { return rhs.compare( lhs ) < 0; }
357 
359 } // namespace zypp
361 #endif // ZYPP_IDSTRINGTYPE_H
static int compare(const std::string &lhs, const char *rhs)
Definition: IdStringType.h:156
#define LEGACY(CL)
Legacy code we still support.
Definition: Globals.h:36
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition: Capability.h:309
IdString idStr() const
Definition: IdStringType.h:111
int compare(const IdString &rhs) const
Compare IdString returning -1,0,1.
Definition: IdString.cc:54
IdType id() const
Definition: IdStringType.h:123
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition: Patch.cc:122
bool operator>(const IdString &lhs, const IdString &rhs)
relates: IdString Greater
Definition: IdString.h:223
static int compare(const char *lhs, const char *rhs)
Definition: IdStringType.h:161
static int compare(const IdString &lhs, const std::string &rhs)
Definition: IdStringType.h:150
int compare(const IdStringType &rhs) const
Definition: IdStringType.h:165
String related utilities and Regular expression matching.
static int compare(const char *lhs, const std::string &rhs)
Definition: IdStringType.h:160
Access to the sat-pools string space.
Definition: IdString.h:51
sat::detail::IdType IdType
Definition: IdString.h:57
Base class for creating IdString based types.
Definition: IdStringType.h:91
static int _doCompare(const char *lhs, const char *rhs) ZYPP_API
Definition: IdStringType.h:171
static int compare(const std::string &lhs, const IdString &rhs)
Definition: IdStringType.h:154
static int compare(const IdString &lhs, const char *rhs)
Definition: IdStringType.h:151
static int compare(const Derived &lhs, const Derived &rhs)
Definition: IdStringType.h:142
static int compare(const Derived &lhs, const IdString &rhs)
Definition: IdStringType.h:143
static int compare(const Derived &lhs, const char *rhs)
Definition: IdStringType.h:145
Backlink to the associated PoolImpl.
Definition: PoolMember.h:52
std::string asString() const
Definition: IdStringType.h:116
static int compare(const std::string &lhs, const std::string &rhs)
Definition: IdStringType.h:155
bool empty() const
Definition: IdStringType.h:113
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition: Arch.h:173
bool operator!=(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition: Capability.h:313
bool operator>=(const IdString &lhs, const IdString &rhs)
relates: IdString GreaterEqual
Definition: IdString.h:239
static int compare(const char *lhs, const IdString &rhs)
Definition: IdStringType.h:159
static int compare(const Derived &lhs, const std::string &rhs)
Definition: IdStringType.h:144
const char * c_str() const
Conversion to const char *
Definition: IdString.cc:51
static int compare(const std::string &lhs, const Derived &rhs)
Definition: IdStringType.h:153
int compare(const Derived &rhs) const
Definition: IdStringType.h:164
int compare(const IdString &rhs) const
Definition: IdStringType.h:166
static int compare(const IdString &lhs, const IdString &rhs)
Definition: IdStringType.h:148
static int compare(const char *lhs, const Derived &rhs)
Definition: IdStringType.h:158
static int compare(const IdString &lhs, const Derived &rhs)
Definition: IdStringType.h:147
int compare(const char *rhs) const
Definition: IdStringType.h:168
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
unsigned size() const
Definition: IdStringType.h:114
zypp::IdString IdString
Definition: idstring.h:16
const char * c_str() const
Definition: IdStringType.h:115
int compare(const std::string &rhs) const
Definition: IdStringType.h:167