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