]> git.cworth.org Git - apitrace/blob - trace_model.hpp
Unify Bitmask::Signature with BitmaskSig.
[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 <assert.h>
35
36 #include <map>
37 #include <list>
38 #include <vector>
39 #include <iostream>
40
41 #include "trace_writer.hpp"
42
43
44 namespace Trace {
45
46
47 class Visitor;
48
49
50 class Value
51 {
52 public:
53     virtual ~Value() {}
54     virtual void visit(Visitor &visitor) = 0;
55
56     virtual bool toBool(void) const = 0;
57     virtual signed long long toSInt(void) const;
58     virtual unsigned long long toUInt(void) const;
59     virtual float toFloat(void) const;
60     virtual double toDouble(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     const Value & operator[](size_t index) const;
67 };
68
69
70 class Null : public Value
71 {
72 public:
73     bool toBool(void) const;
74     signed long long toSInt(void) const;
75     unsigned long long toUInt(void) const;
76     virtual float toFloat(void) const;
77     virtual double toDouble(void) const;
78     void *toPointer(void) const;
79     unsigned long long toUIntPtr(void) const;
80     const char *toString(void) const;
81     void visit(Visitor &visitor);
82 };
83
84
85 class Bool : public Value
86 {
87 public:
88     Bool(bool _value) : value(_value) {}
89
90     bool toBool(void) const;
91     signed long long toSInt(void) const;
92     unsigned long long toUInt(void) const;
93     virtual float toFloat(void) const;
94     virtual double toDouble(void) const;
95     void visit(Visitor &visitor);
96
97     bool value;
98 };
99
100
101 class SInt : public Value
102 {
103 public:
104     SInt(signed long long _value) : value(_value) {}
105
106     bool toBool(void) const;
107     signed long long toSInt(void) const;
108     unsigned long long toUInt(void) const;
109     virtual float toFloat(void) const;
110     virtual double toDouble(void) const;
111     void visit(Visitor &visitor);
112
113     signed long long value;
114 };
115
116
117 class UInt : public Value
118 {
119 public:
120     UInt(unsigned long long _value) : value(_value) {}
121
122     bool toBool(void) const;
123     signed long long toSInt(void) const;
124     unsigned long long toUInt(void) const;
125     virtual float toFloat(void) const;
126     virtual double toDouble(void) const;
127     void visit(Visitor &visitor);
128
129     unsigned long long value;
130 };
131
132
133 class Float : public Value
134 {
135 public:
136     Float(double _value) : value(_value) {}
137
138     bool toBool(void) const;
139     signed long long toSInt(void) const;
140     unsigned long long toUInt(void) const;
141     virtual float toFloat(void) const;
142     virtual double toDouble(void) const;
143     void visit(Visitor &visitor);
144
145     double value;
146 };
147
148
149 class String : public Value
150 {
151 public:
152     String(const char * _value) : value(_value) {}
153
154     bool toBool(void) const;
155     const char *toString(void) const;
156     void visit(Visitor &visitor);
157
158     const char * value;
159 };
160
161
162 class Enum : public Value
163 {
164 public:
165     struct Signature : public std::pair<const char *, Value *>
166     {
167         Signature()
168             : std::pair<const char *, Value *>()
169         {}
170         Signature(const char *n, Trace::Value *val)
171             : std::pair<const char *, Value *>(n, val)
172         {}
173         ~Signature()
174         {
175             delete second;
176         }
177     };
178
179     Enum(const Signature *_sig) : sig(_sig) {}
180
181     bool toBool(void) const;
182     signed long long toSInt(void) const;
183     unsigned long long toUInt(void) const;
184     virtual float toFloat(void) const;
185     virtual double toDouble(void) const;
186     void visit(Visitor &visitor);
187
188     const Signature *sig;
189 };
190
191
192 class Bitmask : public UInt
193 {
194 public:
195     Bitmask(const BitmaskSig *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {}
196
197     void visit(Visitor &visitor);
198
199     const BitmaskSig *sig;
200 };
201
202
203 class Struct : public Value
204 {
205 public:
206     struct Signature {
207         const char *name;
208         std::vector<const char *> member_names;
209     };
210
211     Struct(Signature *_sig) : sig(_sig), members(_sig->member_names.size()) { }
212     ~Struct();
213
214     bool toBool(void) const;
215     void visit(Visitor &visitor);
216
217     const Signature *sig;
218     std::vector<Value *> members;
219 };
220
221
222 class Array : public Value
223 {
224 public:
225     Array(size_t len) : values(len) {}
226     ~Array();
227
228     bool toBool(void) const;
229     void visit(Visitor &visitor);
230
231     std::vector<Value *> values;
232 };
233
234
235 class Blob : public Value
236 {
237 public:
238     Blob(size_t _size) {
239         size = _size;
240         buf = new char[_size];
241     }
242
243     ~Blob();
244
245     bool toBool(void) const;
246     void *toPointer(void) const;
247     void visit(Visitor &visitor);
248
249     size_t size;
250     char *buf;
251 };
252
253
254 class Pointer : public UInt
255 {
256 public:
257     Pointer(unsigned long long value) : UInt(value) {}
258
259     bool toBool(void) const;
260     void *toPointer(void) const;
261     unsigned long long toUIntPtr(void) const;
262     void visit(Visitor &visitor);
263 };
264
265
266 class Visitor
267 {
268 public:
269     virtual void visit(Null *);
270     virtual void visit(Bool *);
271     virtual void visit(SInt *);
272     virtual void visit(UInt *);
273     virtual void visit(Float *);
274     virtual void visit(String *);
275     virtual void visit(Enum *);
276     virtual void visit(Bitmask *);
277     virtual void visit(Struct *);
278     virtual void visit(Array *);
279     virtual void visit(Blob *);
280     virtual void visit(Pointer *);
281
282 protected:
283     inline void _visit(Value *value) {
284         if (value) { 
285             value->visit(*this); 
286         }
287     }
288 };
289
290
291 std::ostream & operator <<(std::ostream &os, Value *value);
292
293
294 class Call
295 {
296 public:
297     struct Signature {
298         const char * name;
299         std::vector<const char *> arg_names;
300     };
301
302     unsigned no;
303     const Signature *sig;
304     std::vector<Value *> args;
305     Value *ret;
306
307     Call(Signature *_sig) : sig(_sig), args(_sig->arg_names.size()), ret(0) { }
308     ~Call();
309
310     inline const char * name(void) const {
311         return sig->name;
312     }
313
314     inline Value & arg(unsigned index) {
315         assert(index < args.size());
316         return *(args[index]);
317     }
318 };
319
320
321 std::ostream & operator <<(std::ostream &os, Call &call);
322
323
324 } /* namespace Trace */
325
326 #endif /* _TRACE_MODEL_HPP_ */