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 "str.h" |
|
33 #include "containers.h" |
|
34 |
|
35 class format_arg |
|
36 { |
|
37 public: |
|
38 format_arg (const string& a) : |
|
39 m_string (a) {} |
|
40 |
|
41 format_arg (char a) : |
|
42 m_string (a) {} |
|
43 |
|
44 format_arg (int a) : |
|
45 m_string (string::from_number (a)) {} |
|
46 |
|
47 format_arg (long a) : |
|
48 m_string (string::from_number (a)) {} |
|
49 |
|
50 format_arg (const char* a) : |
|
51 m_string (a) {} |
|
52 |
|
53 format_arg (void* a) |
|
54 { |
|
55 m_string.sprintf ("%p", a); |
|
56 } |
|
57 |
|
58 format_arg (const void* a) |
|
59 { |
|
60 m_string.sprintf ("%p", a); |
|
61 } |
|
62 |
|
63 template<class T> format_arg (const list<T>& list) |
|
64 { |
|
65 if (list.is_empty()) |
|
66 { |
|
67 m_string = "{}"; |
|
68 return; |
|
69 } |
|
70 |
|
71 m_string = "{ "; |
|
72 |
|
73 for (const T & a : list) |
|
74 { |
|
75 if (&a != &list[0]) |
|
76 m_string += ", "; |
|
77 |
|
78 m_string += format_arg (a).as_string(); |
|
79 } |
|
80 |
|
81 m_string += " }"; |
|
82 } |
|
83 |
|
84 inline const string& as_string() const |
|
85 { |
|
86 return m_string; |
|
87 } |
|
88 |
|
89 private: |
|
90 string m_string; |
|
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 format_args (const list<format_arg>& args); |
|
101 void print_args (FILE* fp, const list<format_arg>& args); |
|
102 void do_fatal (const list<format_arg>& args); |
|
103 void do_error (string msg); |
|
104 |
|
105 #ifndef IN_IDE_PARSER |
|
106 # define format(...) format_args({ __VA_ARGS__ }) |
|
107 # define fprint(A, ...) print_args( A, { __VA_ARGS__ }) |
|
108 # define print(...) print_args( stdout, { __VA_ARGS__ }) |
|
109 # define error(...) do_error (format (__VA_ARGS__)) |
|
110 #else |
|
111 string format (void, ...); |
|
112 void fprint (FILE* fp, ...); |
|
113 void print (void, ...); |
|
114 void error (void, ...); |
|
115 #endif |
|
116 |
|
117 #ifndef IN_IDE_PARSER |
|
118 # ifdef DEBUG |
|
119 # define devf(...) fprint (stderr, __VA_ARGS__) |
|
120 # define dvalof( A ) fprint (stderr, "value of '%1' = %2\n", #A, A) |
|
121 # else |
|
122 # define devf(...) |
|
123 # define dvalof( A ) |
|
124 # endif // DEBUG |
|
125 #else |
|
126 // print something in debug builds |
|
127 void devf (void, ...); |
|
128 |
|
129 // print the value of @a |
|
130 void dvalof (void a); |
|
131 #endif // IN_IDE_PARSER |
|
132 |
|
133 #endif // BOTC_FORMAT_H |
|