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