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