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