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