src/Format.h

changeset 88
5def6ff8b466
child 110
7a7a53f1d51b
equal deleted inserted replaced
87:8f65914e7046 88:5def6ff8b466
1 /*
2 Copyright 2012-2014 Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef BOTC_FORMAT_H
30 #define BOTC_FORMAT_H
31
32 #include "String.h"
33 #include "Containers.h"
34
35 class FormatArgument
36 {
37 public:
38 FormatArgument (const String& a) :
39 mText (a) {}
40
41 FormatArgument (char a) :
42 mText (a) {}
43
44 FormatArgument (int a) :
45 mText (String::FromNumber (a)) {}
46
47 FormatArgument (long a) :
48 mText (String::FromNumber (a)) {}
49
50 FormatArgument (const char* a) :
51 mText (a) {}
52
53 FormatArgument (void* a)
54 {
55 mText.SPrintf ("%p", a);
56 }
57
58 FormatArgument (const void* a)
59 {
60 mText.SPrintf ("%p", a);
61 }
62
63 template<class T> FormatArgument (const List<T>& list)
64 {
65 if (list.IsEmpty())
66 {
67 mText = "{}";
68 return;
69 }
70
71 mText = "{ ";
72
73 for (const T & a : list)
74 {
75 if (&a != &list[0])
76 mText += ", ";
77
78 mText += FormatArgument (a).AsString();
79 }
80
81 mText += " }";
82 }
83
84 inline const String& AsString() const
85 {
86 return mText;
87 }
88
89 private:
90 String mText;
91 };
92
93 template<class T> String custom_format (T a, const char* fmtstr)
94 {
95 String out;
96 out.SPrintf (fmtstr, a);
97 return out;
98 }
99
100 String FormatArgs (const List<FormatArgument>& args);
101 void PrintArgs (FILE* fp, const List<FormatArgument>& args);
102 void DoError (String msg);
103
104 #ifndef IN_IDE_PARSER
105 # define Format(...) FormatArgs ({__VA_ARGS__})
106 # define PrintTo(A, ...) PrintArgs (A, {__VA_ARGS__})
107 # define Print(...) PrintArgs (stdout, {__VA_ARGS__})
108 # define Error(...) DoError (Format (__VA_ARGS__))
109 #else
110 String Format (void, ...);
111 void PrintTo (FILE* fp, ...);
112 void Print (void, ...);
113 void Error (void, ...);
114 #endif
115
116 #ifndef IN_IDE_PARSER
117 # ifdef DEBUG
118 # define devf(...) fprint (stderr, __VA_ARGS__)
119 # define dvalof( A ) fprint (stderr, "value of '%1' = %2\n", #A, A)
120 # else
121 # define devf(...)
122 # define dvalof( A )
123 # endif // DEBUG
124 #else
125 // print something in debug builds
126 void devf (void, ...);
127
128 // print the value of @a
129 void dvalof (void a);
130 #endif // IN_IDE_PARSER
131
132 #endif // BOTC_FORMAT_H

mercurial