1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
27 #include "formatter.hpp"
28 #include "trace_model.hpp"
34 void Null::visit(Visitor &visitor) {
38 void Bool::visit(Visitor &visitor) {
42 void SInt::visit(Visitor &visitor) {
46 void UInt::visit(Visitor &visitor) {
50 void Float::visit(Visitor &visitor) {
54 void String::visit(Visitor &visitor) {
58 void Enum::visit(Visitor &visitor) {
62 void Bitmask::visit(Visitor &visitor) {
66 void Array::visit(Visitor &visitor) {
70 void Blob::visit(Visitor &visitor) {
75 class Dumper : public Visitor
79 Formatter::Formatter *formatter;
80 Formatter::Attribute *normal;
81 Formatter::Attribute *bold;
82 Formatter::Attribute *italic;
83 Formatter::Attribute *red;
84 Formatter::Attribute *pointer;
85 Formatter::Attribute *literal;
88 Dumper(std::ostream &_os) : os(_os) {
89 formatter = Formatter::defaultFormatter();
90 normal = formatter->normal();
91 bold = formatter->bold();
92 italic = formatter->italic();
93 red = formatter->color(Formatter::RED);
94 pointer = formatter->color(Formatter::GREEN);
95 literal = formatter->color(Formatter::BLUE);
112 void visit(Bool *node) {
113 os << literal << (node->value ? "true" : "false") << normal;
116 void visit(SInt *node) {
117 os << literal << node->value << normal;
120 void visit(UInt *node) {
121 os << literal << node->value << normal;
124 void visit(Float *node) {
125 os << literal << node->value << normal;
128 void visit(String *node) {
129 os << literal << '"' << node->value << '"' << normal;
132 void visit(Enum *node) {
133 os << literal << node->name << normal;
136 void visit(Bitmask *bitmask) {
137 unsigned long long value = bitmask->value;
138 const Bitmask::Signature *sig = bitmask->sig;
140 for (Bitmask::Signature::const_iterator it = sig->begin(); value != 0 && it != sig->end(); ++it) {
142 if ((value & it->second) == it->second) {
146 os << literal << it->first << normal;
147 value &= ~it->second;
151 if (value || first) {
155 os << literal << std::hex << value << std::dec << normal;
159 void visit(Array *array) {
160 if (array->values.size() == 1) {
162 _visit(array->values[0]);
165 const char *sep = "";
167 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
176 void visit(Blob *blob) {
177 os << pointer << "blob(" << blob->size << ")" << normal;
180 void visit(Call *call) {
181 const char *sep = "";
182 os << bold << call->sig->name << normal << "(";
183 for (unsigned i = 0; i < call->args.size(); ++i) {
184 os << sep << italic << call->sig->arg_names[i] << normal << " = ";
185 _visit(call->args[i]);
198 std::ostream & operator <<(std::ostream &os, Value *value) {
207 static inline const Value *unwrap(const Value *node) {
208 const Enum *c = dynamic_cast<const Enum *>(node);
215 Value::operator bool(void) const {
216 const Bool *b = dynamic_cast<const Bool *>(unwrap(this));
223 Value::operator signed long long(void) const {
224 const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
227 const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
234 Value::operator unsigned long long(void) const {
235 const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
243 Value::operator double(void) const {
244 const Float *fl = dynamic_cast<const Float *>(unwrap(this));
251 const Value & Value::operator[](size_t index) const {
252 const Array *array = dynamic_cast<const Array *>(unwrap(this));
254 if (index < array->values.size()) {
255 return *array->values[index];
261 void * Value::blob(void) const {
262 const Blob *blob = dynamic_cast<const Blob *>(unwrap(this));
265 const Null *null = dynamic_cast<const Null *>(unwrap(this));
272 const char * Value::string(void) const {
273 const String *string = dynamic_cast<const String *>(unwrap(this));
275 return string->value.c_str();
276 const Null *null = dynamic_cast<const Null *>(unwrap(this));
283 std::ostream & operator <<(std::ostream &os, Call &call) {
290 } /* namespace Trace */