]> git.cworth.org Git - apitrace/blob - trace_model.hpp
Make more value methods virtual.
[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 #undef Bool
118
119 class Bool : public Value
120 {
121 public:
122     Bool(bool _value) : value(_value) {}
123
124     operator bool (void) const;
125     operator signed long long (void) const;
126     operator unsigned long long (void) const;
127     operator double (void) const;
128     void visit(Visitor &visitor);
129
130     bool value;
131 };
132
133
134 class SInt : public Value
135 {
136 public:
137     SInt(signed long long _value) : value(_value) {}
138
139     operator bool (void) const;
140     operator signed long long (void) const;
141     operator unsigned long long (void) const;
142     operator double (void) const;
143     void visit(Visitor &visitor);
144
145     signed long long value;
146 };
147
148
149 class UInt : public Value
150 {
151 public:
152     UInt(unsigned long long _value) : value(_value) {}
153
154     operator bool (void) const;
155     operator signed long long (void) const;
156     operator unsigned long long (void) const;
157     operator double (void) const;
158     void visit(Visitor &visitor);
159
160     unsigned long long value;
161 };
162
163
164 class Float : public Value
165 {
166 public:
167     Float(double _value) : value(_value) {}
168
169     operator bool (void) const;
170     operator signed long long (void) const;
171     operator unsigned long long (void) const;
172     operator double (void) const;
173     void visit(Visitor &visitor);
174
175     double value;
176 };
177
178
179 class String : public Value
180 {
181 public:
182     String(std::string _value) : value(_value) {}
183
184     operator bool (void) const;
185     void visit(Visitor &visitor);
186
187     std::string value;
188 };
189
190
191 class Enum : public Value
192 {
193 public:
194     typedef std::pair<std::string, Value *> Signature;
195
196     Enum(const Signature *_sig) : sig(_sig) {}
197
198     operator bool (void) const;
199     operator signed long long (void) const;
200     operator unsigned long long (void) const;
201     operator double (void) const;
202     void visit(Visitor &visitor);
203
204     const Signature *sig;
205 };
206
207
208 class Bitmask : public UInt
209 {
210 public:
211     typedef std::pair<std::string, unsigned long long> Pair;
212     typedef std::vector<Pair> Signature;
213
214     Bitmask(const Signature *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
215
216     void visit(Visitor &visitor);
217
218     const Signature *sig;
219 };
220
221
222 class Struct : public Value
223 {
224 public:
225     struct Signature {
226         std::string name;
227         std::vector<std::string> member_names;
228     };
229
230     Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { }
231     ~Struct();
232
233     operator bool (void) const;
234     void visit(Visitor &visitor);
235
236     const Signature *sig;
237     std::vector<Value *> members;
238 };
239
240
241 class Array : public Value
242 {
243 public:
244     Array(size_t len) : values(len) {}
245     ~Array();
246
247     operator bool (void) const;
248     void visit(Visitor &visitor);
249
250     std::vector<Value *> values;
251 };
252
253
254 class Blob : public Value
255 {
256 public:
257     Blob(size_t _size) {
258         size = _size;
259         buf = new char[_size];
260     }
261
262     ~Blob();
263
264     operator bool (void) const;
265     void *blob(void) const;
266     void visit(Visitor &visitor);
267
268     size_t size;
269     char *buf;
270 };
271
272
273 class Pointer : public UInt
274 {
275 public:
276     Pointer(unsigned long long value) : UInt(value) {}
277
278     operator bool (void) const;
279     void *blob(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 signed long long asSInt(const Value &node);
313 unsigned long long asUInt(const Value &node);
314 double asFloat(const Value &node);
315
316
317 class Call
318 {
319 public:
320     struct Signature {
321         std::string name;
322         std::vector<std::string> arg_names;
323     };
324
325     unsigned no;
326     const Signature *sig;
327     std::vector<Value *> args;
328     Value *ret;
329
330     Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { }
331     ~Call();
332
333     inline const std::string name(void) const {
334         return sig->name;
335     }
336
337     inline Value & arg(unsigned index) {
338         return *(args[index]);
339     }
340 };
341
342
343 std::ostream & operator <<(std::ostream &os, Call &call);
344
345
346 } /* namespace Trace */
347
348 #endif /* _TRACE_MODEL_HPP_ */