src/types.h

changeset 14
825d9ed1ee01
parent 13
9bdddd2ccde6
child 20
a5457405cc9b
equal deleted inserted replaced
13:9bdddd2ccde6 14:825d9ed1ee01
55 // Converts a given value into a string that can be retrieved with ::value (). 55 // Converts a given value into a string that can be retrieved with ::value ().
56 // Used as the argument type to the formatting functions, hence its name. 56 // Used as the argument type to the formatting functions, hence its name.
57 // ============================================================================= 57 // =============================================================================
58 class StringFormatArg { 58 class StringFormatArg {
59 public: 59 public:
60 StringFormatArg( const str& v ); 60 StringFormatArg (const str& v);
61 StringFormatArg( const char& v ); 61 StringFormatArg (const char& v);
62 StringFormatArg( const uchar& v ); 62 StringFormatArg (const uchar& v);
63 StringFormatArg( const qchar& v ); 63 StringFormatArg (const qchar& v);
64 64
65 #define NUMERIC_FORMAT_ARG(T,C) \ 65 #define NUMERIC_FORMAT_ARG(T,C) \
66 StringFormatArg (const T& v) { \ 66 StringFormatArg (const T& v) { \
67 char valstr[32]; \ 67 char valstr[32]; \
68 sprintf (valstr, "%" #C, v); \ 68 sprintf (valstr, "%" #C, v); \
69 m_val = valstr; \ 69 m_val = valstr; \
70 } 70 }
71 71
72 NUMERIC_FORMAT_ARG( int, d ) 72 NUMERIC_FORMAT_ARG (int, d)
73 NUMERIC_FORMAT_ARG( short, d ) 73 NUMERIC_FORMAT_ARG (short, d)
74 NUMERIC_FORMAT_ARG( long, ld ) 74 NUMERIC_FORMAT_ARG (long, ld)
75 NUMERIC_FORMAT_ARG( uint, u ) 75 NUMERIC_FORMAT_ARG (uint, u)
76 NUMERIC_FORMAT_ARG( ushort, u ) 76 NUMERIC_FORMAT_ARG (ushort, u)
77 NUMERIC_FORMAT_ARG( ulong, lu ) 77 NUMERIC_FORMAT_ARG (ulong, lu)
78 78
79 StringFormatArg( const float& v ); 79 StringFormatArg (const float& v);
80 StringFormatArg( const double& v ); 80 StringFormatArg (const double& v);
81 StringFormatArg( const char* v ); 81 StringFormatArg (const char* v);
82 StringFormatArg( const void* v ); 82 StringFormatArg (const void* v);
83 83
84 template<class T> StringFormatArg( const list<T>& v ) { 84 template<class T> StringFormatArg (const list<T>& v) {
85 m_val = "{ "; 85 m_val = "{ ";
86 uint i = 0; 86 uint i = 0;
87 const bool isString = typeid( T ) == typeid( str ); 87 const bool isString = typeid (T) == typeid (str);
88 88
89 for( const T& it : v ) { 89 for (const T& it : v) {
90 if( i++ ) 90 if (i++)
91 m_val += ", "; 91 m_val += ", ";
92 92
93 StringFormatArg arg( it ); 93 StringFormatArg arg (it);
94 94
95 if( isString ) 95 if (isString)
96 m_val += "\"" + arg.value() + "\""; 96 m_val += "\"" + arg.value() + "\"";
97 else 97 else
98 m_val += arg.value(); 98 m_val += arg.value();
99 } 99 }
100 100
101 if( i ) 101 if (i)
102 m_val += " "; 102 m_val += " ";
103 103
104 m_val += "}"; 104 m_val += "}";
105 } 105 }
106 106
110 private: 110 private:
111 str m_val; 111 str m_val;
112 }; 112 };
113 113
114 // Formatter function 114 // Formatter function
115 str doFormat( initlist<StringFormatArg> args ); 115 str doFormat (initlist<StringFormatArg> args);
116 116
117 // printf replacement 117 // printf replacement
118 void doPrint( FILE* fp, initlist<StringFormatArg> args ); 118 void doPrint (FILE* fp, initlist<StringFormatArg> args);
119 119
120 // Macros to access these functions 120 // Macros to access these functions
121 #ifndef IN_IDE_PARSER 121 #ifndef IN_IDE_PARSER
122 # define fmt(...) doFormat({ __VA_ARGS__ }) 122 # define fmt(...) doFormat({ __VA_ARGS__ })
123 # define print(...) doPrint( stdout, { __VA_ARGS__ }) 123 # define print(...) doPrint (stdout, { __VA_ARGS__ })
124 # define fprint(FP, ...) doPrint( FP, { __VA_ARGS__ }) 124 # define fprint(FP, ...) doPrint (FP, { __VA_ARGS__ })
125 #else 125 #else
126 str fmt( const char* fmtstr, ... ); 126 str fmt (const char* fmtstr, ...);
127 void print( const char* fmtstr, ... ); 127 void print (const char* fmtstr, ...);
128 void fprint( FILE* fp, const char* fmtstr, ... ); 128 void fprint (FILE* fp, const char* fmtstr, ...);
129 #endif 129 #endif
130 130
131 #endif // TYPES_H 131 #endif // TYPES_H

mercurial