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 Const::visit(Visitor &visitor) {
62 void Array::visit(Visitor &visitor) {
66 void Blob::visit(Visitor &visitor) {
71 class Dumper : public Visitor
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;
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);
104 void visit(Null *node) {
108 void visit(Bool *node) {
109 os << literal << (node->value ? "true" : "false") << normal;
112 void visit(SInt *node) {
113 os << literal << node->value << normal;
116 void visit(UInt *node) {
117 os << literal << node->value << normal;
120 void visit(Float *node) {
121 os << literal << node->value << normal;
124 void visit(String *node) {
125 os << literal << '"' << node->value << '"' << normal;
128 void visit(Const *node) {
129 os << literal << node->name << normal;
132 void visit(Array *array) {
133 if (array->values.size() == 1) {
135 _visit(array->values[0]);
138 const char *sep = "";
140 for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
149 void visit(Blob *blob) {
150 os << pointer << "blob(" << blob->size << ")" << normal;
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 << " = ";
171 std::ostream & operator <<(std::ostream &os, Value *value) {
180 static inline const Value *unwrap(const Value *node) {
181 const Const *c = dynamic_cast<const Const *>(node);
188 Value::operator bool(void) const {
189 const Bool *b = dynamic_cast<const Bool *>(unwrap(this));
196 Value::operator signed long long(void) const {
197 const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
200 const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
207 Value::operator unsigned long long(void) const {
208 const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
216 Value::operator double(void) const {
217 const Float *fl = dynamic_cast<const Float *>(unwrap(this));
224 const Value & Value::operator[](size_t index) const {
225 const Array *array = dynamic_cast<const Array *>(unwrap(this));
227 if (index < array->values.size()) {
228 return *array->values[index];
234 void * Value::blob(void) const {
235 const Blob *blob = dynamic_cast<const Blob *>(unwrap(this));
238 const Null *null = dynamic_cast<const Null *>(unwrap(this));
245 const char * Value::string(void) const {
246 const String *string = dynamic_cast<const String *>(unwrap(this));
248 return string->value.c_str();
249 const Null *null = dynamic_cast<const Null *>(unwrap(this));
256 std::ostream & operator <<(std::ostream &os, Call &call) {
263 } /* namespace Trace */