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