]> git.cworth.org Git - apitrace/blob - trace_model.hpp
8436fc1afac77bb7dd6ab8a08b5351a71d458376
[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     struct Signature : public std::pair<std::string, Value *>
193     {
194         Signature()
195             : std::pair<std::string, Value *>()
196         {}
197         Signature(const std::string &n, Trace::Value *val)
198             : std::pair<std::string, Value *>(n, val)
199         {}
200         ~Signature()
201         {
202             delete second;
203         }
204     };
205
206     Enum(const Signature *_sig) : sig(_sig) {}
207
208     operator bool (void) const;
209     operator signed long long (void) const;
210     operator unsigned long long (void) const;
211     operator double (void) const;
212     void visit(Visitor &visitor);
213
214     const Signature *sig;
215 };
216
217
218 class Bitmask : public UInt
219 {
220 public:
221     typedef std::pair<std::string, unsigned long long> Pair;
222     typedef std::vector<Pair> Signature;
223
224     Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
225
226     void visit(Visitor &visitor);
227
228     const Signature *sig;
229 };
230
231
232 class Struct : public Value
233 {
234 public:
235     struct Signature {
236         std::string name;
237         std::vector<std::string> member_names;
238     };
239
240     Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { }
241     ~Struct();
242
243     operator bool (void) const;
244     void visit(Visitor &visitor);
245
246     const Signature *sig;
247     std::vector<Value *> members;
248 };
249
250
251 class Array : public Value
252 {
253 public:
254     Array(size_t len) : values(len) {}
255     ~Array();
256
257     operator bool (void) const;
258     void visit(Visitor &visitor);
259
260     std::vector<Value *> values;
261 };
262
263
264 class Blob : public Value
265 {
266 public:
267     Blob(size_t _size) {
268         size = _size;
269         buf = new char[_size];
270     }
271
272     ~Blob();
273
274     operator bool (void) const;
275     void *blob(void) const;
276     void visit(Visitor &visitor);
277
278     size_t size;
279     char *buf;
280 };
281
282
283 class Pointer : public UInt
284 {
285 public:
286     Pointer(unsigned long long value) : UInt(value) {}
287
288     operator bool (void) const;
289     void *blob(void) const;
290     void visit(Visitor &visitor);
291 };
292
293
294 class Visitor
295 {
296 public:
297     virtual void visit(Null *);
298     virtual void visit(Bool *);
299     virtual void visit(SInt *);
300     virtual void visit(UInt *);
301     virtual void visit(Float *);
302     virtual void visit(String *);
303     virtual void visit(Enum *);
304     virtual void visit(Bitmask *);
305     virtual void visit(Struct *);
306     virtual void visit(Array *);
307     virtual void visit(Blob *);
308     virtual void visit(Pointer *);
309
310 protected:
311     inline void _visit(Value *value) {
312         if (value) { 
313             value->visit(*this); 
314         }
315     }
316 };
317
318
319 std::ostream & operator <<(std::ostream &os, Value *value);
320
321
322 signed long long asSInt(const Value &node);
323 unsigned long long asUInt(const Value &node);
324 double asFloat(const Value &node);
325
326
327 class Call
328 {
329 public:
330     struct Signature {
331         std::string name;
332         std::vector<std::string> arg_names;
333     };
334
335     unsigned no;
336     const Signature *sig;
337     std::vector<Value *> args;
338     Value *ret;
339
340     Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { }
341     ~Call();
342
343     inline const std::string name(void) const {
344         return sig->name;
345     }
346
347     inline Value & arg(unsigned index) {
348         return *(args[index]);
349     }
350 };
351
352
353 std::ostream & operator <<(std::ostream &os, Call &call);
354
355
356 } /* namespace Trace */
357
358 #endif /* _TRACE_MODEL_HPP_ */