src/types.h

Wed, 17 Jul 2013 19:45:19 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Wed, 17 Jul 2013 19:45:19 +0300
changeset 9
f9893eea978b
parent 6
67b6ef6917ba
child 13
9bdddd2ccde6
permissions
-rw-r--r--

shrink a little

0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #ifndef TYPES_H
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 #define TYPES_H
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 #include "main.h"
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 #include <QString>
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 #include <QList>
3
d0b278fd29d5 Implemented wad path list
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
7 #include <QVariant>
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 typedef QString str;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 typedef QChar qchar;
3
d0b278fd29d5 Implemented wad path list
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
11 typedef QVariant var;
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 template<class T> using list = QList<T>;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 typedef unsigned int uint;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 typedef unsigned short ushort;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 typedef unsigned long ulong;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 template<class T> using initlist = std::initializer_list<T>;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 using std::size_t;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
6
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
19 typedef qint8 int8;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
20 typedef qint16 int16;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
21 typedef qint32 int32;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
22 typedef qint64 int64;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
23 typedef quint8 uint8;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
24 typedef quint16 uint16;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
25 typedef quint32 uint32;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
26 typedef quint64 uint64;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
27
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
28 #ifdef IN_IDE_PARSER // :|
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
29 typedef void FILE;
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
30 #endif
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
31
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 // =============================================================================
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 // =============================================================================
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 // StringFormatArg
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 //
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 // Converts a given value into a string that can be retrieved with ::value ().
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 // Used as the argument type to the formatting functions, hence its name.
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 // =============================================================================
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 class StringFormatArg {
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 public:
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 StringFormatArg( const str& v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 StringFormatArg( const char& v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 StringFormatArg( const uchar& v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 StringFormatArg( const qchar& v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 #define NUMERIC_FORMAT_ARG(T,C) \
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 StringFormatArg (const T& v) { \
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 char valstr[32]; \
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 sprintf (valstr, "%" #C, v); \
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 m_val = valstr; \
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 }
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 NUMERIC_FORMAT_ARG( int, d )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 NUMERIC_FORMAT_ARG( short, d )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 NUMERIC_FORMAT_ARG( long, ld )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 NUMERIC_FORMAT_ARG( uint, u )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 NUMERIC_FORMAT_ARG( ushort, u )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 NUMERIC_FORMAT_ARG( ulong, lu )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 StringFormatArg( const float& v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 StringFormatArg( const double& v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 StringFormatArg( const char* v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 StringFormatArg( const void* v );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 template<class T> StringFormatArg( const list<T>& v ) {
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 m_val = "{ ";
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 uint i = 0;
6
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
69 const bool isString = typeid( T ) == typeid( str );
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 for( const T& it : v ) {
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 if( i++ )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 m_val += ", ";
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 StringFormatArg arg( it );
6
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
76
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
77 if( isString )
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
78 m_val += "\"" + arg.value() + "\"";
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
79 else
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
80 m_val += arg.value();
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 }
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 if( i )
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 m_val += " ";
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 m_val += "}";
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 }
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 str value() const {
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 return m_val;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 }
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 private:
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 str m_val;
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 };
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 // Formatter function
1
c9b22c06307c Done config box base
Teemu Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
97 str doFormat( initlist<StringFormatArg> args );
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 // printf replacement
6
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
100 void doPrint( FILE* fp, initlist<StringFormatArg> args );
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 // Macros to access these functions
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 #ifndef IN_IDE_PARSER
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 # define fmt(...) doFormat({ __VA_ARGS__ })
6
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
105 # define print(...) doPrint( stdout, { __VA_ARGS__ })
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
106 # define fprint(FP, ...) doPrint( FP, { __VA_ARGS__ })
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 #else
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 str fmt( const char* fmtstr, ... );
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 void print( const char* fmtstr, ... );
6
67b6ef6917ba Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents: 3
diff changeset
110 void fprint( FILE* fp, const char* fmtstr, ... );
0
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 #endif
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
bb264814dd81 Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 #endif // TYPES_H

mercurial