args-parser 6.3.3
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1
31#ifndef ARGS__TYPES_HPP__INCLUDED
32#define ARGS__TYPES_HPP__INCLUDED
33
34#ifdef ARGS_WSTRING_BUILD
35 // C++ include.
36 #include <string>
37 #include <iostream>
38
39 #ifdef ARGS_LIST
40 #include <list>
41 #elif defined( ARGS_DEQUE )
42 #include <deque>
43 #else
44 #include <vector>
45 #endif
46
47#elif defined( ARGS_QSTRING_BUILD )
48 // Qt include.
49 #include <QString>
50 #include <QTextStream>
51
52 #ifdef ARGS_LIST
53 #include <QLinkedList>
54 #elif defined( ARGS_DEQUE )
55 #include <QList>
56 #else
57 #include <QVector>
58 #endif
59
60#else
61 // C++ include.
62 #include <string>
63 #include <iostream>
64
65 #ifdef ARGS_LIST
66 #include <list>
67 #elif defined( ARGS_DEQUE )
68 #include <deque>
69 #else
70 #include <vector>
71 #endif
72
73#endif
74
75
76namespace Args {
77
78#ifdef ARGS_WSTRING_BUILD
79
81using String = std::wstring;
82
84using Char = String::value_type;
85
87using OutStreamType = std::wostream;
88
90static OutStreamType & outStream()
91{
92 return std::wcout;
93}
94
95#define SL(str) L##str
96
98#ifdef ARGS_LIST
99 using StringList = std::list< String >;
100#elif defined( ARGS_DEQUE )
101 using StringList = std::deque< String >;
102#else
103 using StringList = std::vector< String >;
104#endif
105
106#elif defined( ARGS_QSTRING_BUILD )
107
109using Char = QChar;
110
112using OutStreamType = QTextStream;
113
114class String {
115public:
116 using size_type = int;
117
118 String()
119 {
120 }
121
122 String( size_type size, Char ch )
123 : m_str( size, ch )
124 {
125 }
126
127 String( const char * str )
128 : m_str( str )
129 {
130 }
131
132 String( const QString & other )
133 : m_str( other )
134 {
135 }
136
137
138 String( const Char * unicode, size_type size = -1 )
139 : m_str( unicode, size )
140 {
141 }
142
143 String( Char ch )
144 : m_str( ch )
145 {
146 }
147
148 String( QLatin1String str )
149 : m_str( str )
150 {
151 }
152
153 String( const QByteArray & ba )
154 : m_str( ba )
155 {
156 }
157
158 operator QString ()
159 {
160 return m_str;
161 }
162
163 operator QString () const
164 {
165 return m_str;
166 }
167
168 inline bool empty() const
169 {
170 return m_str.isEmpty();
171 }
172
173 static const int npos = -1;
174
175 inline int find( Char ch ) const
176 {
177 return m_str.indexOf( ch );
178 }
179
180 inline int find( const String & str ) const
181 {
182 return m_str.indexOf( str.m_str );
183 }
184
185 QString::iterator begin()
186 {
187 return m_str.begin();
188 }
189
190 QString::iterator end()
191 {
192 return m_str.end();
193 }
194
195 QString::const_iterator begin() const
196 {
197 return m_str.begin();
198 }
199
200 QString::const_iterator end() const
201 {
202 return m_str.end();
203 }
204
205 QString::const_iterator cbegin() const
206 {
207 return m_str.begin();
208 }
209
210 QString::const_iterator cend() const
211 {
212 return m_str.end();
213 }
214
215 size_type length() const
216 {
217 return m_str.length();
218 }
219
220 String substr( size_type pos, size_type count = npos ) const
221 {
222 return m_str.mid( pos, count );
223 }
224
225 friend bool operator == ( const String & s1, const String & s2 )
226 {
227 return ( s1.m_str == s2.m_str );
228 }
229
230 friend bool operator != ( const String & s1, const String & s2 )
231 {
232 return ( s1.m_str != s2.m_str );
233 }
234
235 friend String operator + ( const String & s1, const String & s2 )
236 {
237 return String( s1.m_str + s2.m_str );
238 }
239
240 friend String operator + ( const String & s1, const char * s2 )
241 {
242 return String( s1.m_str + s2 );
243 }
244
245 friend String operator + ( const char * s1, const String & s2 )
246 {
247 return String( s1 + s2.m_str );
248 }
249
250 friend String operator + ( const String & s1, const char ch )
251 {
252 return String( s1.m_str + ch );
253 }
254
255 friend String operator + ( const char ch, const String & s2 )
256 {
257 return String( ch + s2.m_str );
258 }
259
260 friend bool operator < ( const String & s1, const String & s2 )
261 {
262 return s1.m_str < s2.m_str;
263 }
264
265 friend OutStreamType & operator << ( OutStreamType & to,
266 const String & what )
267 {
268 to << what.m_str;
269
270 return to;
271 }
272
273 const Char operator [] ( size_type pos ) const
274 {
275 return m_str[ pos ];
276 }
277
278 String & append( const String & other )
279 {
280 m_str.append( other.m_str );
281
282 return *this;
283 }
284
285 String & append( size_type count, Char ch )
286 {
287 m_str.append( QString( count, ch ) );
288
289 return *this;
290 }
291
292 void clear()
293 {
294 m_str.clear();
295 }
296
297private:
299 QString m_str;
300}; // class String
301
303static OutStreamType & outStream()
304{
305 static QTextStream stream( stdout );
306
307 return stream;
308}
309
310#define SL(str) str
311
313#ifdef ARGS_LIST
314 using StringList = QLinkedList< String >;
315#elif defined( ARGS_DEQUE )
316 using StringList = QList< String >;
317#else
318 using StringList = QVector< String >;
319#endif
320
321#else
322
324using String = std::string;
325
327using Char = String::value_type;
328
330using OutStreamType = std::ostream;
331
333static OutStreamType & outStream()
334{
335 return std::cout;
336}
337
338#define SL(str) str
339
341#ifdef ARGS_LIST
342 using StringList = std::list< String >;
343#elif defined( ARGS_DEQUE )
344 using StringList = std::deque< String >;
345#else
346 using StringList = std::vector< String >;
347#endif
348
349#endif
350
351
352namespace details {
353
354//
355// Deleter
356//
357
359template< typename T >
360class Deleter {
361public:
362 explicit Deleter( bool shouldIDelete )
363 : m_delete( shouldIDelete )
364 {
365 }
366
367 void operator () ( T * obj ) noexcept
368 {
369 if( m_delete )
370 delete obj;
371 }
372
373private:
375 bool m_delete;
376}; // class Deleter
377
378} /* namespace details */
379
380} /* namespace Args */
381
382#endif // ARGS__TYPES_HPP__INCLUDED
Deleter(bool shouldIDelete)
Definition types.hpp:362
void operator()(T *obj) noexcept
Definition types.hpp:367
Definition api.hpp:42
std::string String
String type.
Definition types.hpp:324
std::vector< String > StringList
List of strings.
Definition types.hpp:346
std::ostream OutStreamType
Out stream type.
Definition types.hpp:330
String::value_type Char
Char type.
Definition types.hpp:327