src/types.h

changeset 287
3fcccd8c3357
parent 286
7a562bf3d829
child 288
2980d7fd948e
equal deleted inserted replaced
286:7a562bf3d829 287:3fcccd8c3357
21 21
22 #include <QString> 22 #include <QString>
23 #include <vector> 23 #include <vector>
24 #include "common.h" 24 #include "common.h"
25 25
26 // Null pointer
27 static const std::nullptr_t null = nullptr;
28
26 typedef QChar qchar; 29 typedef QChar qchar;
27 typedef QString str; 30 typedef QString str;
28 template<class T> class ConstVectorReverser; 31 template<class T> class ConstVectorReverser;
29 template<class T> using c_rev = ConstVectorReverser<T>; 32 template<class T> using c_rev = ConstVectorReverser<T>;
30 class strconfig; 33 class strconfig;
31 class intconfig; 34 class intconfig;
32 class floatconfig; 35 class floatconfig;
36 class QFile;
37 class QTextStream;
33 38
34 typedef unsigned int uint; 39 typedef unsigned int uint;
35 typedef unsigned short ushort; 40 typedef unsigned short ushort;
36 typedef unsigned long ulong; 41 typedef unsigned long ulong;
37 42
123 128
124 private: 129 private:
125 double m_coords[3]; 130 double m_coords[3];
126 }; 131 };
127 132
128
129 // ============================================================================= 133 // =============================================================================
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 134 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
131 // ============================================================================= 135 // =============================================================================
132 // vector 136 // vector
133 // 137 //
134 // Array class that wraps around vector 138 // Array class that wraps around std::vector
135 // ============================================================================= 139 // =============================================================================
136 template<class T> class vector { 140 template<class T> class vector {
137 public: 141 public:
138 typedef typename std::vector<T>::iterator it; 142 typedef typename std::vector<T>::iterator it;
139 typedef typename std::vector<T>::const_iterator c_it; 143 typedef typename std::vector<T>::const_iterator c_it;
275 279
276 private: 280 private:
277 std::vector<T> m_vect; 281 std::vector<T> m_vect;
278 }; 282 };
279 283
284 // =============================================================================
285 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
286 // =============================================================================
287 // VectorReverser (aka rev)
288 //
289 // Helper class used to reverse-iterate vectors in range-for-loops.
290 // =============================================================================
280 template<class T> class VectorReverser { 291 template<class T> class VectorReverser {
281 public: 292 public:
282 typedef typename vector<T>::r_it it; 293 typedef typename vector<T>::r_it it;
283 294
284 VectorReverser (vector<T>& vect) { 295 VectorReverser (vector<T>& vect) {
295 306
296 private: 307 private:
297 vector<T>* m_vect; 308 vector<T>* m_vect;
298 }; 309 };
299 310
311 // =============================================================================
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
313 // =============================================================================
314 // ConstVectorReverser (aka c_rev)
315 //
316 // Like VectorReverser, except works on const vectors.
317 // =============================================================================
300 template<class T> class ConstVectorReverser { 318 template<class T> class ConstVectorReverser {
301 public: 319 public:
302 typedef typename vector<T>::cr_it it; 320 typedef typename vector<T>::cr_it it;
303 321
304 ConstVectorReverser (const vector<T>& vect) { 322 ConstVectorReverser (const vector<T>& vect) {
318 }; 336 };
319 337
320 template<class T> using rev = VectorReverser<T>; 338 template<class T> using rev = VectorReverser<T>;
321 template<class T> using c_rev = ConstVectorReverser<T>; 339 template<class T> using c_rev = ConstVectorReverser<T>;
322 340
341 // =============================================================================
342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
343 // =============================================================================
344 // StringFormatArg
345 //
346 // Converts a given value into a string that can be retrieved with ::value ().
347 // Used as the argument type to the formatting functions, hence its name.
323 // ============================================================================= 348 // =============================================================================
324 class StringFormatArg { 349 class StringFormatArg {
325 public: 350 public:
326 StringFormatArg (const str& v); 351 StringFormatArg (const str& v);
327 StringFormatArg (const char& v); 352 StringFormatArg (const char& v);
354 str value () const { return m_val; } 379 str value () const { return m_val; }
355 private: 380 private:
356 str m_val; 381 str m_val;
357 }; 382 };
358 383
359 str DoFormat (vector< StringFormatArg > args); 384 // Formatter function
385 str DoFormat (vector<StringFormatArg> args);
360 #ifndef IN_IDE_PARSER 386 #ifndef IN_IDE_PARSER
361 #define fmt(...) DoFormat ({__VA_ARGS__}) 387 #define fmt(...) DoFormat ({__VA_ARGS__})
362 #else 388 #else
363 str fmt (const char* fmtstr, ...); 389 str fmt (const char* fmtstr, ...);
364 #endif 390 #endif
365 391
392 // =============================================================================
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
394 // =============================================================================
395 // File
396 //
397 // A file interface with simple interface and support for range-for-loops.
398 // =============================================================================
399 class File {
400 public:
401 // Iterator class to enable range-for-loop support. Rough hack.. don't use directly!
402 class iterator {
403 public:
404 iterator () : m_file (null) {} // end iterator has m_file == null
405 iterator (File* f) : m_file (f), m_text (m_file->readLine ()) {}
406 void operator++ ();
407 str operator* ();
408 bool operator== (iterator& other);
409 bool operator!= (iterator& other);
410
411 private:
412 File* m_file;
413 str m_text;
414 };
415
416 enum OpenType {
417 Read,
418 Write,
419 Append
420 };
421
422 File (const std::nullptr_t&);
423 File (str path, File::OpenType rtype);
424 File (FILE* fp, File::OpenType rtype);
425 ~File ();
426
427 bool atEnd () const;
428 iterator begin ();
429 void close ();
430 iterator& end ();
431 bool flush ();
432 bool isNull () const;
433 str readLine ();
434 bool open (FILE* fp, OpenType rtype);
435 bool open (str path, OpenType rtype, FILE* fp = null);
436 bool operator! () const;
437 void write (str msg);
438
439 private:
440 QFile* m_file;
441 QTextStream* m_textstream;
442 iterator m_endIterator;
443 };
444
445 // Null-file, equivalent to a null FILE*
446 extern const File nullfile;
447
366 #endif // TYPES_H 448 #endif // TYPES_H

mercurial