/
IdStringType.h
346 lines (323 loc) · 14.6 KB
/
IdStringType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*---------------------------------------------------------------------\
| ____ _ __ __ ___ |
| |__ / \ / / . \ . \ |
| / / \ V /| _/ _/ |
| / /__ | | | | | | |
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
/** \file zypp/IdStringType.h
*
*/
#ifndef ZYPP_IDSTRINGTYPE_H
#define ZYPP_IDSTRINGTYPE_H
#include "zypp/IdString.h"
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : IdStringType<Derived>
//
/** Base class for creating \ref IdString based types.
*
* Just by deriving from \ref IdStringType a class provides all
* the operations an \ref IdString does. (incl. conversion to string types,
* comparison of string types and stream output).
*
* To disable any comparison, declare (but do not define) \ref _doCompare
* in your class.
* \code
* class NoCompare : public IdStringType<NoCompare>
* {
* private:
* static int _doCompare( const char * lhs, const char * rhs );
*
* };
* \endcode
*
* If you need a different than the default lexicographical
* order, write your own \ref _doCompare.
*
* \code
* // uses default lexicographical order
* class CaseCmp : public IdStringType<CaseCmp>
* {
* public:
* CaseCmp() {}
* explicit CaseCmp( const char * cstr_r ) : _str( cstr_r ) {}
* private:
* friend class IdStringType<CaseCmp>;
* IdString _str;
* };
*
* // uses case insensitive comparison order
* class NoCaseCmp : public IdStringType<NoCaseCmp>
* {
* public:
* NoCaseCmp() {}
* explicit NoCaseCmp( const char * cstr_r ) : _str( cstr_r ) {}
* private:
* static int _doCompare( const char * lhs, const char * rhs )
* {
* if ( lhs == rhs ) return 0;
* if ( lhs && rhs ) return ::strcasecmp( lhs, rhs );
* return( lhs ? 1 : -1 );
* }
* private:
* friend class IdStringType<NoCaseCmp>;
* IdString _str;
* };
*
* CaseCmp ca( "a" );
* NoCaseCmp na( "a" );
* DBG << "ca == a ? " << (ca == "a") << endl; // ca == a ? 1
* DBG << "ca == A ? " << (ca == "A") << endl; // ca == A ? 0
* DBG << "na == a ? " << (na == "a") << endl; // na == a ? 1
* DBG << "na == A ? " << (na == "A") << endl; // na == A ? 1
* \endcode
* \todo allow redefinition of order vis _doCompare not only for char* but on any level
* \ingroup g_CRTP
*/
template <class Derived>
class IdStringType : protected sat::detail::PoolMember,
private base::SafeBool<Derived>
{
typedef typename base::SafeBool<Derived>::bool_type bool_type;
public:
typedef IdString::IdType IdType;
protected:
IdStringType() {}
IdStringType(const IdStringType &) {}
void operator=(const IdStringType &) {}
~IdStringType() {}
private:
const Derived & self() const { return *static_cast<const Derived*>( this ); }
public:
const IdString & idStr() const { return self()._str; }
bool empty() const { return idStr().empty(); }
unsigned size() const { return idStr().size(); }
const char * c_str() const { return idStr().c_str(); }
std::string asString() const { return idStr().asString(); }
IdType id() const { return idStr().id(); }
public:
#ifndef SWIG // Swig treats it as syntax error
/** Evaluate in a boolean context <tt>( ! empty() )</tt>. */
using base::SafeBool<Derived>::operator bool_type;
#endif
public:
// - break it down to idString/const char* <=> idString/cont char*
// - handle idString(0)/NULL being the least value
// - everything else goes to _doCompare (no NULL)
static int compare( const Derived & lhs, const Derived & rhs ) { return compare( lhs.idStr(), rhs.idStr() ); }
static int compare( const Derived & lhs, const IdString & rhs ) { return compare( lhs.idStr(), rhs ); }
static int compare( const Derived & lhs, const std::string & rhs ) { return compare( lhs.idStr(), rhs.c_str() ); }
static int compare( const Derived & lhs, const char * rhs ) { return compare( lhs.idStr(), rhs );}
static int compare( const IdString & lhs, const Derived & rhs ) { return compare( lhs, rhs.idStr() ); }
static int compare( const IdString & lhs, const IdString & rhs ) { return lhs == rhs ? 0 : Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ),
(rhs ? rhs.c_str() : (const char *)0 ) ); }
static int compare( const IdString & lhs, const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
static int compare( const IdString & lhs, const char * rhs ) { return Derived::_doCompare( (lhs ? lhs.c_str() : (const char *)0 ), rhs ); }
static int compare( const std::string & lhs, const Derived & rhs ) { return compare( lhs.c_str(), rhs.idStr() ); }
static int compare( const std::string & lhs, const IdString & rhs ) { return compare( lhs.c_str(), rhs ); }
static int compare( const std::string & lhs, const std::string & rhs ) { return compare( lhs.c_str(), rhs.c_str() ); }
static int compare( const std::string & lhs, const char * rhs ) { return compare( lhs.c_str(), rhs ); }
static int compare( const char * lhs, const Derived & rhs ) { return compare( lhs, rhs.idStr() ); }
static int compare( const char * lhs, const IdString & rhs ) { return Derived::_doCompare( lhs, (rhs ? rhs.c_str() : (const char *)0 ) ); }
static int compare( const char * lhs, const std::string & rhs ) { return compare( lhs, rhs.c_str() ); }
static int compare( const char * lhs, const char * rhs ) { return Derived::_doCompare( lhs, rhs ); }
public:
int compare( const Derived & rhs ) const { return compare( idStr(), rhs.idStr() ); }
int compare( const IdStringType & rhs ) const { return compare( idStr(), rhs.idStr() ); }
int compare( const IdString & rhs ) const { return compare( idStr(), rhs ); }
int compare( const std::string & rhs ) const { return compare( idStr(), rhs.c_str() ); }
int compare( const char * rhs ) const { return compare( idStr(), rhs ); }
private:
static int _doCompare( const char * lhs, const char * rhs )
{
if ( ! lhs ) return rhs ? -1 : 0;
return rhs ? ::strcmp( lhs, rhs ) : 1;
}
private:
#ifndef SWIG // Swig treats it as syntax error
friend base::SafeBool<Derived>::operator bool_type() const;
#endif
bool boolTest() const { return ! empty(); }
};
///////////////////////////////////////////////////////////////////
/** \relates IdStringType Stream output */
template <class Derived>
inline std::ostream & operator<<( std::ostream & str, const IdStringType<Derived> & obj )
{ return str << obj.c_str(); }
/** \relates IdStringType Equal */
template <class Derived>
inline bool operator==( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
{ return lhs.compare( rhs ) == 0; }
/** \overload */
template <class Derived>
inline bool operator==( const IdStringType<Derived> & lhs, const IdString & rhs )
{ return lhs.compare( rhs ) == 0; }
/** \overload */
template <class Derived>
inline bool operator==( const IdStringType<Derived> & lhs, const char * rhs )
{ return lhs.compare( rhs ) == 0; }
/** \overload */
template <class Derived>
inline bool operator==( const IdStringType<Derived> & lhs, const std::string & rhs )
{ return lhs.compare( rhs ) == 0; }
/** \overload */
template <class Derived>
inline bool operator==( const IdString & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) == 0; }
/** \overload */
template <class Derived>
inline bool operator==( const char * lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) == 0; }
/** \overload */
template <class Derived>
inline bool operator==( const std::string & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) == 0; }
/** \relates IdStringType NotEqual */
template <class Derived>
inline bool operator!=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
{ return lhs.compare( rhs ) != 0; }
/** \overload */
template <class Derived>
inline bool operator!=( const IdStringType<Derived> & lhs, const IdString & rhs )
{ return lhs.compare( rhs ) != 0; }
/** \overload */
template <class Derived>
inline bool operator!=( const IdStringType<Derived> & lhs, const char * rhs )
{ return lhs.compare( rhs ) != 0; }
/** \overload */
template <class Derived>
inline bool operator!=( const IdStringType<Derived> & lhs, const std::string & rhs )
{ return lhs.compare( rhs ) != 0; }
/** \overload */
template <class Derived>
inline bool operator!=( const IdString & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) != 0; }
/** \overload */
template <class Derived>
inline bool operator!=( const char * lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) != 0; }
/** \overload */
template <class Derived>
inline bool operator!=( const std::string & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) != 0; }
/** \relates IdStringType Less */
template <class Derived>
inline bool operator<( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
{ return lhs.compare( rhs ) < 0; }
/** \overload */
template <class Derived>
inline bool operator<( const IdStringType<Derived> & lhs, const IdString & rhs )
{ return lhs.compare( rhs ) < 0; }
/** \overload */
template <class Derived>
inline bool operator<( const IdStringType<Derived> & lhs, const char * rhs )
{ return lhs.compare( rhs ) < 0; }
/** \overload */
template <class Derived>
inline bool operator<( const IdStringType<Derived> & lhs, const std::string & rhs )
{ return lhs.compare( rhs ) < 0; }
/** \overload */
template <class Derived>
inline bool operator<( const IdString & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) >= 0; }
/** \overload */
template <class Derived>
inline bool operator<( const char * lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) >= 0; }
/** \overload */
template <class Derived>
inline bool operator<( const std::string & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) >= 0; }
/** \relates IdStringType LessEqual */
template <class Derived>
inline bool operator<=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
{ return lhs.compare( rhs ) <= 0; }
/** \overload */
template <class Derived>
inline bool operator<=( const IdStringType<Derived> & lhs, const IdString & rhs )
{ return lhs.compare( rhs ) <= 0; }
/** \overload */
template <class Derived>
inline bool operator<=( const IdStringType<Derived> & lhs, const char * rhs )
{ return lhs.compare( rhs ) <= 0; }
/** \overload */
template <class Derived>
inline bool operator<=( const IdStringType<Derived> & lhs, const std::string & rhs )
{ return lhs.compare( rhs ) <= 0; }
/** \overload */
template <class Derived>
inline bool operator<=( const IdString & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) > 0; }
/** \overload */
template <class Derived>
inline bool operator<=( const char * lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) > 0; }
/** \overload */
template <class Derived>
inline bool operator<=( const std::string & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) > 0; }
/** \relates IdStringType Greater */
template <class Derived>
inline bool operator>( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
{ return lhs.compare( rhs ) > 0; }
/** \overload */
template <class Derived>
inline bool operator>( const IdStringType<Derived> & lhs, const IdString & rhs )
{ return lhs.compare( rhs ) > 0; }
/** \overload */
template <class Derived>
inline bool operator>( const IdStringType<Derived> & lhs, const char * rhs )
{ return lhs.compare( rhs ) > 0; }
/** \overload */
template <class Derived>
inline bool operator>( const IdStringType<Derived> & lhs, const std::string & rhs )
{ return lhs.compare( rhs ) > 0; }
/** \overload */
template <class Derived>
inline bool operator>( const IdString & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) <= 0; }
/** \overload */
template <class Derived>
inline bool operator>( const char * lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) <= 0; }
/** \overload */
template <class Derived>
inline bool operator>( const std::string & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) <= 0; }
/** \relates IdStringType GreaterEqual */
template <class Derived>
inline bool operator>=( const IdStringType<Derived> & lhs, const IdStringType<Derived> & rhs )
{ return lhs.compare( rhs ) >= 0; }
/** \overload */
template <class Derived>
inline bool operator>=( const IdStringType<Derived> & lhs, const IdString & rhs )
{ return lhs.compare( rhs ) >= 0; }
/** \overload */
template <class Derived>
inline bool operator>=( const IdStringType<Derived> & lhs, const char * rhs )
{ return lhs.compare( rhs ) >= 0; }
/** \overload */
template <class Derived>
inline bool operator>=( const IdStringType<Derived> & lhs, const std::string & rhs )
{ return lhs.compare( rhs ) >= 0; }
/** \overload */
template <class Derived>
inline bool operator>=( const IdString & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) < 0; }
/** \overload */
template <class Derived>
inline bool operator>=( const char * lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) < 0; }
/** \overload */
template <class Derived>
inline bool operator>=( const std::string & lhs, const IdStringType<Derived> & rhs )
{ return rhs.compare( lhs ) < 0; }
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////
#endif // ZYPP_IDSTRINGTYPE_H