]> git.cworth.org Git - apitrace/blob - trace_model.hpp
preallocate array values
[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 unsigned long long toUIntPtr(void) const;
100     virtual const char *toString(void) const;
101
102     const Value & operator[](size_t index) const;
103
104     void dump(std::ostream &os, bool color=true);
105 };
106
107
108 class Null : public Value
109 {
110 public:
111     bool toBool(void) const;
112     signed long long toSInt(void) const;
113     unsigned long long toUInt(void) const;
114     virtual float toFloat(void) const;
115     virtual double toDouble(void) const;
116     void *toPointer(void) const;
117     unsigned long long toUIntPtr(void) const;
118     const char *toString(void) const;
119     void visit(Visitor &visitor);
120 };
121
122
123 class Bool : public Value
124 {
125 public:
126     Bool(bool _value) : value(_value) {}
127
128     bool toBool(void) const;
129     signed long long toSInt(void) const;
130     unsigned long long toUInt(void) const;
131     virtual float toFloat(void) const;
132     virtual double toDouble(void) const;
133     void visit(Visitor &visitor);
134
135     bool value;
136 };
137
138
139 class SInt : public Value
140 {
141 public:
142     SInt(signed long long _value) : value(_value) {}
143
144     bool toBool(void) const;
145     signed long long toSInt(void) const;
146     unsigned long long toUInt(void) const;
147     virtual float toFloat(void) const;
148     virtual double toDouble(void) const;
149     void visit(Visitor &visitor);
150
151     signed long long value;
152 };
153
154
155 class UInt : public Value
156 {
157 public:
158     UInt(unsigned long long _value) : value(_value) {}
159
160     bool toBool(void) const;
161     signed long long toSInt(void) const;
162     unsigned long long toUInt(void) const;
163     virtual float toFloat(void) const;
164     virtual double toDouble(void) const;
165     void visit(Visitor &visitor);
166
167     unsigned long long value;
168 };
169
170
171 class Float : public Value
172 {
173 public:
174     Float(double _value) : value(_value) {}
175
176     bool toBool(void) const;
177     signed long long toSInt(void) const;
178     unsigned long long toUInt(void) const;
179     virtual float toFloat(void) const;
180     virtual double toDouble(void) const;
181     void visit(Visitor &visitor);
182
183     double value;
184 };
185
186
187 class String : public Value
188 {
189 public:
190     String(const char * _value) : value(_value) {}
191
192     bool toBool(void) const;
193     const char *toString(void) const;
194     void visit(Visitor &visitor);
195
196     const char * value;
197 };
198
199
200 class Enum : public Value
201 {
202 public:
203     Enum(const EnumSig *_sig) : sig(_sig) {}
204
205     bool toBool(void) const;
206     signed long long toSInt(void) const;
207     unsigned long long toUInt(void) const;
208     virtual float toFloat(void) const;
209     virtual double toDouble(void) const;
210     void visit(Visitor &visitor);
211
212     const EnumSig *sig;
213 };
214
215
216 class Bitmask : public UInt
217 {
218 public:
219     Bitmask(const BitmaskSig *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
220
221     void visit(Visitor &visitor);
222
223     const BitmaskSig *sig;
224 };
225
226
227 class Struct : public Value
228 {
229 public:
230     Struct(StructSig *_sig) : sig(_sig), members(_sig->num_members) { }
231     ~Struct();
232
233     bool toBool(void) const;
234     void visit(Visitor &visitor);
235
236     const StructSig *sig;
237     std::vector<Value *> members;
238 };
239
240
241 class Array : public Value
242 {
243 public:
244     Array(size_t len) : values(len) {}
245     ~Array();
246
247     bool toBool(void) const;
248     void visit(Visitor &visitor);
249
250     std::vector<Value *> values;
251 };
252
253
254 class Blob : public Value
255 {
256 public:
257     Blob(size_t _size) {
258         size = _size;
259         buf = new char[_size];
260     }
261
262     ~Blob();
263
264     bool toBool(void) const;
265     void *toPointer(void) const;
266     void visit(Visitor &visitor);
267
268     size_t size;
269     char *buf;
270 };
271
272
273 class Pointer : public UInt
274 {
275 public:
276     Pointer(unsigned long long value) : UInt(value) {}
277
278     bool toBool(void) const;
279     void *toPointer(void) const;
280     unsigned long long toUIntPtr(void) const;
281     void visit(Visitor &visitor);
282 };
283
284
285 class Visitor
286 {
287 public:
288     virtual void visit(Null *);
289     virtual void visit(Bool *);
290     virtual void visit(SInt *);
291     virtual void visit(UInt *);
292     virtual void visit(Float *);
293     virtual void visit(String *);
294     virtual void visit(Enum *);
295     virtual void visit(Bitmask *);
296     virtual void visit(Struct *);
297     virtual void visit(Array *);
298     virtual void visit(Blob *);
299     virtual void visit(Pointer *);
300
301 protected:
302     inline void _visit(Value *value) {
303         if (value) { 
304             value->visit(*this); 
305         }
306     }
307 };
308
309
310 inline std::ostream & operator <<(std::ostream &os, Value *value) {
311     if (value) {
312         value->dump(os);
313     }
314     return os;
315 }
316
317
318 class Call
319 {
320 public:
321     unsigned no;
322     const FunctionSig *sig;
323     std::vector<Value *> args;
324     Value *ret;
325
326     Call(FunctionSig *_sig) : sig(_sig), args(_sig->num_args), ret(0) { }
327     ~Call();
328
329     inline const char * name(void) const {
330         return sig->name;
331     }
332
333     inline Value & arg(unsigned index) {
334         assert(index < args.size());
335         return *(args[index]);
336     }
337
338     void dump(std::ostream &os, bool color=true);
339 };
340
341
342 inline std::ostream & operator <<(std::ostream &os, Call &call) {
343     call.dump(os);
344     return os;
345 }
346
347
348 } /* namespace Trace */
349
350 #endif /* _TRACE_MODEL_HPP_ */