]> git.cworth.org Git - apitrace/blob - trace_model.hpp
Merge remote-tracking branch 'origin/drawarrays'
[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     operator bool (void) const;
58     operator signed long long (void) const;
59     operator unsigned long long (void) const;
60     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     void *blob(void) const;
109     void visit(Visitor &visitor);
110 };
111
112
113 #undef Bool
114
115 class Bool : public Value
116 {
117 public:
118     Bool(bool _value) : value(_value) {}
119
120     void visit(Visitor &visitor);
121
122     bool value;
123 };
124
125
126 class SInt : public Value
127 {
128 public:
129     SInt(signed long long _value) : value(_value) {}
130
131     void visit(Visitor &visitor);
132
133     signed long long value;
134 };
135
136
137 class UInt : public Value
138 {
139 public:
140     UInt(unsigned long long _value) : value(_value) {}
141
142     void visit(Visitor &visitor);
143
144     unsigned long long value;
145 };
146
147
148 class Float : public Value
149 {
150 public:
151     Float(double _value) : value(_value) {}
152
153     void visit(Visitor &visitor);
154
155     double value;
156 };
157
158
159 class String : public Value
160 {
161 public:
162     String(std::string _value) : value(_value) {}
163
164     void visit(Visitor &visitor);
165
166     std::string value;
167 };
168
169
170 class Enum : public Value
171 {
172 public:
173     typedef std::pair<std::string, Value *> Signature;
174
175     Enum(const Signature *_sig) : sig(_sig) {}
176
177     void visit(Visitor &visitor);
178
179     const Signature *sig;
180 };
181
182
183 class Bitmask : public UInt
184 {
185 public:
186     typedef std::pair<std::string, unsigned long long> Pair;
187     typedef std::vector<Pair> Signature;
188
189     Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
190
191     void visit(Visitor &visitor);
192
193     const Signature *sig;
194 };
195
196
197 class Struct : public Value
198 {
199 public:
200     struct Signature {
201         std::string name;
202         std::vector<std::string> member_names;
203     };
204
205     Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { }
206     ~Struct();
207
208     void visit(Visitor &visitor);
209
210     const Signature *sig;
211     std::vector<Value *> members;
212 };
213
214
215 class Array : public Value
216 {
217 public:
218     Array(size_t len) : values(len) {}
219     ~Array();
220
221     void visit(Visitor &visitor);
222
223     std::vector<Value *> values;
224 };
225
226
227 class Blob : public Value
228 {
229 public:
230     Blob(size_t _size) {
231         size = _size;
232         buf = new char[_size];
233     }
234
235     ~Blob();
236
237     void *blob(void) const;
238     void visit(Visitor &visitor);
239
240     size_t size;
241     char *buf;
242 };
243
244
245 class Pointer : public UInt
246 {
247 public:
248     Pointer(unsigned long long value) : UInt(value) {}
249
250     void *blob(void) const;
251     void visit(Visitor &visitor);
252 };
253
254
255 class Visitor
256 {
257 public:
258     virtual void visit(Null *);
259     virtual void visit(Bool *);
260     virtual void visit(SInt *);
261     virtual void visit(UInt *);
262     virtual void visit(Float *);
263     virtual void visit(String *);
264     virtual void visit(Enum *);
265     virtual void visit(Bitmask *);
266     virtual void visit(Struct *);
267     virtual void visit(Array *);
268     virtual void visit(Blob *);
269     virtual void visit(Pointer *);
270
271 protected:
272     inline void _visit(Value *value) {
273         if (value) { 
274             value->visit(*this); 
275         }
276     }
277 };
278
279
280 std::ostream & operator <<(std::ostream &os, Value *value);
281
282
283 signed long long asSInt(const Value &node);
284 unsigned long long asUInt(const Value &node);
285 double asFloat(const Value &node);
286
287
288 class Call
289 {
290 public:
291     struct Signature {
292         std::string name;
293         std::vector<std::string> arg_names;
294     };
295
296     unsigned no;
297     const Signature *sig;
298     std::vector<Value *> args;
299     Value *ret;
300
301     Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { }
302     ~Call();
303
304     inline const std::string name(void) const {
305         return sig->name;
306     }
307
308     inline Value & arg(unsigned index) {
309         return *(args[index]);
310     }
311 };
312
313
314 std::ostream & operator <<(std::ostream &os, Call &call);
315
316
317 } /* namespace Trace */
318
319 #endif /* _TRACE_MODEL_HPP_ */