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