]> git.cworth.org Git - apitrace/blob - trace_model.hpp
Synchronize X and GL on windows resizes.
[apitrace] / trace_model.hpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
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  * Object hierarchy for describing the traces in memory.
28  */
29
30 #ifndef _TRACE_MODEL_HPP_
31 #define _TRACE_MODEL_HPP_
32
33
34 #include <cassert>
35
36 #include <string>
37 #include <map>
38 #include <list>
39 #include <vector>
40 #include <iostream>
41
42
43 namespace Trace {
44
45
46 class Visitor;
47 class Dumper;
48 class UInt;
49
50
51 class Value
52 {
53 public:
54     virtual ~Value() {}
55     virtual void visit(Visitor &visitor) = 0;
56
57     virtual operator bool (void) const = 0;
58     virtual operator signed long long (void) const;
59     virtual operator unsigned long long (void) const;
60     virtual operator double (void) const;
61
62     virtual void *blob(void) const;
63     const char *string(void) const;
64
65     inline operator signed char (void) const { 
66         return static_cast<signed long long>(*this);
67     }
68
69     inline operator unsigned char (void) const { 
70         return static_cast<signed long long>(*this);
71     }
72
73     inline operator signed short (void) const { 
74         return static_cast<signed long long>(*this);
75     }
76
77     inline operator unsigned short (void) const { 
78         return static_cast<unsigned long long>(*this);
79     }
80
81     inline operator signed (void) const { 
82         return static_cast<signed long long>(*this);
83     }
84
85     inline operator unsigned (void) const { 
86         return static_cast<unsigned long long>(*this);
87     }
88
89     inline operator signed long (void) const { 
90         return static_cast<signed long long>(*this);
91     }
92
93     inline operator unsigned long (void) const { 
94         return static_cast<unsigned long long>(*this);
95     }
96
97     inline operator float (void) const { 
98         return static_cast<double>(*this);
99     }
100
101     const Value & operator[](size_t index) const;
102 };
103
104
105 class Null : public Value
106 {
107 public:
108     operator bool (void) const;
109     operator signed long long (void) const;
110     operator unsigned long long (void) const;
111     operator double (void) const;
112     void *blob(void) const;
113     void visit(Visitor &visitor);
114 };
115
116
117 class Bool : public Value
118 {
119 public:
120     Bool(bool _value) : value(_value) {}
121
122     operator bool (void) const;
123     operator signed long long (void) const;
124     operator unsigned long long (void) const;
125     operator double (void) const;
126     void visit(Visitor &visitor);
127
128     bool value;
129 };
130
131
132 class SInt : public Value
133 {
134 public:
135     SInt(signed long long _value) : value(_value) {}
136
137     operator bool (void) const;
138     operator signed long long (void) const;
139     operator unsigned long long (void) const;
140     operator double (void) const;
141     void visit(Visitor &visitor);
142
143     signed long long value;
144 };
145
146
147 class UInt : public Value
148 {
149 public:
150     UInt(unsigned long long _value) : value(_value) {}
151
152     operator bool (void) const;
153     operator signed long long (void) const;
154     operator unsigned long long (void) const;
155     operator double (void) const;
156     void visit(Visitor &visitor);
157
158     unsigned long long value;
159 };
160
161
162 class Float : public Value
163 {
164 public:
165     Float(double _value) : value(_value) {}
166
167     operator bool (void) const;
168     operator signed long long (void) const;
169     operator unsigned long long (void) const;
170     operator double (void) const;
171     void visit(Visitor &visitor);
172
173     double value;
174 };
175
176
177 class String : public Value
178 {
179 public:
180     String(std::string _value) : value(_value) {}
181
182     operator bool (void) const;
183     void visit(Visitor &visitor);
184
185     std::string value;
186 };
187
188
189 class Enum : public Value
190 {
191 public:
192     typedef std::pair<std::string, Value *> Signature;
193
194     Enum(const Signature *_sig) : sig(_sig) {}
195
196     operator bool (void) const;
197     operator signed long long (void) const;
198     operator unsigned long long (void) const;
199     operator double (void) const;
200     void visit(Visitor &visitor);
201
202     const Signature *sig;
203 };
204
205
206 class Bitmask : public UInt
207 {
208 public:
209     typedef std::pair<std::string, unsigned long long> Pair;
210     typedef std::vector<Pair> Signature;
211
212     Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
213
214     void visit(Visitor &visitor);
215
216     const Signature *sig;
217 };
218
219
220 class Struct : public Value
221 {
222 public:
223     struct Signature {
224         std::string name;
225         std::vector<std::string> member_names;
226     };
227
228     Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { }
229     ~Struct();
230
231     operator bool (void) const;
232     void visit(Visitor &visitor);
233
234     const Signature *sig;
235     std::vector<Value *> members;
236 };
237
238
239 class Array : public Value
240 {
241 public:
242     Array(size_t len) : values(len) {}
243     ~Array();
244
245     operator bool (void) const;
246     void visit(Visitor &visitor);
247
248     std::vector<Value *> values;
249 };
250
251
252 class Blob : public Value
253 {
254 public:
255     Blob(size_t _size) {
256         size = _size;
257         buf = new char[_size];
258     }
259
260     ~Blob();
261
262     operator bool (void) const;
263     void *blob(void) const;
264     void visit(Visitor &visitor);
265
266     size_t size;
267     char *buf;
268 };
269
270
271 class Pointer : public UInt
272 {
273 public:
274     Pointer(unsigned long long value) : UInt(value) {}
275
276     operator bool (void) const;
277     void *blob(void) const;
278     void visit(Visitor &visitor);
279 };
280
281
282 class Visitor
283 {
284 public:
285     virtual void visit(Null *);
286     virtual void visit(Bool *);
287     virtual void visit(SInt *);
288     virtual void visit(UInt *);
289     virtual void visit(Float *);
290     virtual void visit(String *);
291     virtual void visit(Enum *);
292     virtual void visit(Bitmask *);
293     virtual void visit(Struct *);
294     virtual void visit(Array *);
295     virtual void visit(Blob *);
296     virtual void visit(Pointer *);
297
298 protected:
299     inline void _visit(Value *value) {
300         if (value) { 
301             value->visit(*this); 
302         }
303     }
304 };
305
306
307 std::ostream & operator <<(std::ostream &os, Value *value);
308
309
310 signed long long asSInt(const Value &node);
311 unsigned long long asUInt(const Value &node);
312 double asFloat(const Value &node);
313
314
315 class Call
316 {
317 public:
318     struct Signature {
319         std::string name;
320         std::vector<std::string> arg_names;
321     };
322
323     unsigned no;
324     const Signature *sig;
325     std::vector<Value *> args;
326     Value *ret;
327
328     Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { }
329     ~Call();
330
331     inline const std::string name(void) const {
332         return sig->name;
333     }
334
335     inline Value & arg(unsigned index) {
336         return *(args[index]);
337     }
338 };
339
340
341 std::ostream & operator <<(std::ostream &os, Call &call);
342
343
344 } /* namespace Trace */
345
346 #endif /* _TRACE_MODEL_HPP_ */