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