src/types.h

changeset 0
bb264814dd81
child 1
c9b22c06307c
equal deleted inserted replaced
-1:000000000000 0:bb264814dd81
1 #ifndef TYPES_H
2 #define TYPES_H
3
4 #include "main.h"
5 #include <QString>
6 #include <QList>
7
8 typedef QString str;
9 typedef QChar qchar;
10 template<class T> using list = QList<T>;
11 typedef unsigned int uint;
12 typedef unsigned short ushort;
13 typedef unsigned long ulong;
14 template<class T> using initlist = std::initializer_list<T>;
15 using std::size_t;
16
17 // =============================================================================
18 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
19 // =============================================================================
20 // StringFormatArg
21 //
22 // Converts a given value into a string that can be retrieved with ::value ().
23 // Used as the argument type to the formatting functions, hence its name.
24 // =============================================================================
25 class StringFormatArg {
26 public:
27 StringFormatArg( const str& v );
28 StringFormatArg( const char& v );
29 StringFormatArg( const uchar& v );
30 StringFormatArg( const qchar& v );
31
32 #define NUMERIC_FORMAT_ARG(T,C) \
33 StringFormatArg (const T& v) { \
34 char valstr[32]; \
35 sprintf (valstr, "%" #C, v); \
36 m_val = valstr; \
37 }
38
39 NUMERIC_FORMAT_ARG( int, d )
40 NUMERIC_FORMAT_ARG( short, d )
41 NUMERIC_FORMAT_ARG( long, ld )
42 NUMERIC_FORMAT_ARG( uint, u )
43 NUMERIC_FORMAT_ARG( ushort, u )
44 NUMERIC_FORMAT_ARG( ulong, lu )
45
46 StringFormatArg( const float& v );
47 StringFormatArg( const double& v );
48 StringFormatArg( const char* v );
49 StringFormatArg( const void* v );
50
51 template<class T> StringFormatArg( const list<T>& v ) {
52 m_val = "{ ";
53 uint i = 0;
54
55 for( const T& it : v ) {
56 if( i++ )
57 m_val += ", ";
58
59 StringFormatArg arg( it );
60 m_val += arg.value();
61 }
62
63 if( i )
64 m_val += " ";
65
66 m_val += "}";
67 }
68
69 str value() const {
70 return m_val;
71 }
72 private:
73 str m_val;
74 };
75
76 // Formatter function
77 str doFormat( std::vector< StringFormatArg > args );
78
79 // printf replacement
80 void doPrint( initlist<StringFormatArg> args ); // heh
81
82 // Macros to access these functions
83 #ifndef IN_IDE_PARSER
84 # define fmt(...) doFormat({ __VA_ARGS__ })
85 # define print(...) doPrint({ __VA_ARGS__ })
86 #else
87 str fmt( const char* fmtstr, ... );
88 void print( const char* fmtstr, ... );
89 #endif
90
91 #endif // TYPES_H

mercurial