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