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