]> git.cworth.org Git - apitrace/blob - trace_model.hpp
Cleanup.
[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 Const : public Value
163 {
164 public:
165    Const(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 Array : public Value
175 {
176 public:
177    Array(size_t len) : values(len) {}
178
179    void visit(Visitor &visitor);
180
181    std::vector<Value *> values;
182 };
183
184
185 class Blob : public Value
186 {
187 public:
188    Blob(size_t _size) {
189        size = _size;
190        buf = new char[_size];
191    }
192
193    ~Blob() {
194        delete [] buf;
195    }
196
197    void visit(Visitor &visitor);
198
199    size_t size;
200    char *buf;
201 };
202
203
204 class Visitor
205 {
206 public:
207    virtual void visit(Null *) {assert(0);}
208    virtual void visit(Bool *) {assert(0);}
209    virtual void visit(SInt *) {assert(0);}
210    virtual void visit(UInt *) {assert(0);}
211    virtual void visit(Float *) {assert(0);}
212    virtual void visit(String *) {assert(0);}
213    virtual void visit(Const *) {assert(0);}
214    virtual void visit(Array *) {assert(0);}
215    virtual void visit(Blob *) {assert(0);}
216
217 protected:
218    inline void _visit(Value *value) {
219       if (value) { 
220          value->visit(*this); 
221       }
222    }
223 };
224
225
226 std::ostream & operator <<(std::ostream &os, Value *value);
227
228
229 signed long long asSInt(const Value &node);
230 unsigned long long asUInt(const Value &node);
231 double asFloat(const Value &node);
232
233
234 typedef std::pair<std::string, Value *> Arg;
235
236 class Call
237 {
238 public:
239    std::string name;
240    std::vector<Arg> args;
241    Value *ret;
242
243    Call() : ret(0) { }
244
245    inline Value & arg(unsigned index) {
246        return *(args[index].second);
247    }
248 };
249
250
251
252 std::ostream & operator <<(std::ostream &os, Call &call);
253
254
255 } /* namespace Trace */
256
257 #endif /* _TRACE_MODEL_HPP_ */