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