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