]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Refer args by index.
[apitrace] / trace_model.cpp
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
27 #include "formatter.hpp"
28 #include "trace_model.hpp"
29
30
31 namespace Trace {
32
33
34 void Null::visit(Visitor &visitor) {
35    visitor.visit(this);
36 }
37
38 void Bool::visit(Visitor &visitor) {
39    visitor.visit(this);
40 }
41
42 void SInt::visit(Visitor &visitor) {
43    visitor.visit(this);
44 }
45
46 void UInt::visit(Visitor &visitor) {
47    visitor.visit(this);
48 }
49
50 void Float::visit(Visitor &visitor) {
51    visitor.visit(this);
52 }
53
54 void String::visit(Visitor &visitor) {
55    visitor.visit(this);
56 }
57
58 void Const::visit(Visitor &visitor) {
59    visitor.visit(this);
60 }
61
62 void Array::visit(Visitor &visitor) {
63    visitor.visit(this);
64 }
65
66 void Blob::visit(Visitor &visitor) {
67    visitor.visit(this);
68 }
69
70
71 class Dumper : public Visitor
72 {
73 protected:
74    std::ostream &os;
75    Formatter::Formatter *formatter;
76    Formatter::Attribute *normal;
77    Formatter::Attribute *bold;
78    Formatter::Attribute *italic;
79    Formatter::Attribute *red;
80    Formatter::Attribute *pointer;
81    Formatter::Attribute *literal;
82
83 public:
84    Dumper(std::ostream &_os) : os(_os) {
85       formatter = Formatter::defaultFormatter();
86       normal = formatter->normal();
87       bold = formatter->bold();
88       italic = formatter->italic();
89       red = formatter->color(Formatter::RED);
90       pointer = formatter->color(Formatter::GREEN);
91       literal = formatter->color(Formatter::BLUE);
92    }
93
94    ~Dumper() {
95       delete normal;
96       delete bold;
97       delete italic;
98       delete red;
99       delete pointer;
100       delete literal;
101       delete formatter;
102    }
103
104    void visit(Null *node) {
105       os << "NULL";
106    }
107
108    void visit(Bool *node) {
109       os << literal << (node->value ? "true" : "false") << normal;
110    }
111
112    void visit(SInt *node) {
113       os << literal << node->value << normal;
114    }
115
116    void visit(UInt *node) {
117       os << literal << node->value << normal;
118    }
119
120    void visit(Float *node) {
121       os << literal << node->value << normal;
122    }
123
124    void visit(String *node) {
125       os << literal << '"' << node->value << '"' << normal;
126    }
127
128    void visit(Const *node) {
129       os << literal << node->name << normal;
130    }
131
132    void visit(Array *array) {
133       if (array->values.size() == 1) {
134          os << "&";
135          _visit(array->values[0]);
136       }
137       else {
138          const char *sep = "";
139          os << "{";
140          for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
141             os << sep;
142             _visit(*it);
143             sep = ", ";
144          }
145          os << "}";
146       }
147    }
148    
149    void visit(Blob *blob) {
150       os << pointer << "blob(" << blob->size << ")" << normal;
151    }
152
153    void visit(Call *call) {
154       const char *sep = "";
155       os << bold << call->name << normal << "(";
156       for (std::vector<Arg>::iterator it = call->args.begin(); it != call->args.end(); ++it) {
157          os << sep << italic << it->first << normal << " = ";
158          _visit(it->second);
159          sep = ", ";
160       }
161       os << ")";
162       if (call->ret) {
163          os << " = ";
164          _visit(call->ret);
165       }
166       os << "\n";
167    }
168 };
169
170
171 std::ostream & operator <<(std::ostream &os, Value *value) {
172    Dumper d(os);
173    if (value) {
174       value->visit(d);
175    }
176    return os;
177 }
178
179
180 static inline const Value *unwrap(const Value *node) {
181    const Const *c = dynamic_cast<const Const *>(node);
182    if (c)
183       return c->value;
184    return node;
185 }
186
187
188 Value::operator signed long long(void) const {
189    const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
190    if (sint)
191       return sint->value;
192    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
193    if (uint)
194       return uint->value;
195    assert(0);
196    return 0;
197 }
198
199 Value::operator unsigned long long(void) const {
200    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
201    if (uint)
202       return uint->value;
203    assert(0);
204    return 0;
205 }
206
207
208 Value::operator double(void) const {
209    const Float *fl = dynamic_cast<const Float *>(unwrap(this));
210    assert(fl);
211    return fl->value;
212 }
213
214 static Null null;
215
216 const Value & Value::operator[](size_t index) const {
217     const Array *array = dynamic_cast<const Array *>(unwrap(this));
218     if (array) {
219         if (index < array->values.size()) {
220             return *array->values[index];
221         }
222     }
223     return null;
224 }
225
226 void * Value::blob(void) const {
227    const Blob *blob = dynamic_cast<const Blob *>(unwrap(this));
228    if (blob)
229        return blob->buf;
230    const Null *null = dynamic_cast<const Null *>(unwrap(this));
231    if (null);
232        return NULL;
233    assert(0);
234    return NULL;
235 }
236
237 const char * Value::string(void) const {
238    const String *string = dynamic_cast<const String *>(unwrap(this));
239    if (string)
240        return string->value.c_str();
241    const Null *null = dynamic_cast<const Null *>(unwrap(this));
242    if (null);
243        return NULL;
244    assert(0);
245    return NULL;
246 }
247
248 std::ostream & operator <<(std::ostream &os, Call &call) {
249    Dumper d(os);
250    d.visit(&call);
251    return os;
252 }
253
254
255 } /* namespace Trace */