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