]> git.cworth.org Git - apitrace/blob - retrace/json.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / retrace / json.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * JSON writing functions.
28  */
29
30 #ifndef _JSON_HPP_
31 #define _JSON_HPP_
32
33 #include <stddef.h>
34 #include <wchar.h>
35
36 #ifdef _MSC_VER
37 #  include <float.h>
38 #  define isfinite _finite
39 #  define isnan _isnan
40 #else
41 #  include <math.h> // isfinite, isnan
42 #endif
43
44 #include <iomanip>
45 #include <limits>
46 #include <ostream>
47 #include <string>
48
49
50 namespace image {
51     class Image;
52 }
53
54
55 class JSONWriter
56 {
57 private:
58     std::ostream &os;
59
60     int level;
61     bool value;
62     char space;
63
64     void
65     newline(void);
66
67     void
68     separator(void);
69
70 public:
71     JSONWriter(std::ostream &_os);
72
73     ~JSONWriter();
74
75     void
76     beginObject();
77
78     void
79     endObject();
80
81     void
82     beginMember(const char * name);
83
84     inline void
85     beginMember(const std::string &name) {
86         beginMember(name.c_str());
87     }
88
89     void
90     endMember(void);
91
92     void
93     beginArray();
94
95     void
96     endArray(void);
97
98     void
99     writeString(const char *s);
100
101     inline void
102     writeString(const std::string &s) {
103         writeString(s.c_str());
104     }
105
106     void
107     writeBase64(const void *bytes, size_t size);
108
109     void
110     writeNull(void);
111
112     void
113     writeBool(bool b);
114
115     /**
116      * Special case for char to prevent it to be written as a literal
117      * character.
118      */
119     inline void
120     writeInt(signed char n) {
121         separator();
122         os << std::dec << static_cast<int>(n);
123         value = true;
124         space = ' ';
125     }
126
127     inline void
128     writeInt(unsigned char n) {
129         separator();
130         os << std::dec << static_cast<unsigned>(n);
131         value = true;
132         space = ' ';
133     }
134
135     template<class T>
136     inline void
137     writeInt(T n) {
138         separator();
139         os << std::dec << n;
140         value = true;
141         space = ' ';
142     }
143
144     template<class T>
145     void
146     writeFloat(T n) {
147         separator();
148         if (isnan(n)) {
149             // NaN is non-standard but widely supported
150             os << "NaN";
151         } else if (!isfinite(n)) {
152             // Infinite is non-standard but widely supported
153             if (n < 0) {
154                 os << '-';
155             }
156             os << "Infinity";
157         } else {
158             os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
159         }
160         value = true;
161         space = ' ';
162     }
163     
164     inline void
165     writeStringMember(const char *name, const char *s) {
166         beginMember(name);
167         writeString(s);
168         endMember();
169     }
170
171     inline void
172     writeBoolMember(const char *name, bool b) {
173         beginMember(name);
174         writeBool(b);
175         endMember();
176     }
177
178     template<class T>
179     inline void
180     writeIntMember(const char *name, T n) {
181         beginMember(name);
182         writeInt(n);
183         endMember();
184     }
185
186     void
187     writeImage(image::Image *image, const char *format, unsigned depth = 1);
188 };
189
190 #endif /* _JSON_HPP_ */