]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Make better use of C++ implicit casts. Support arrays.
[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 "trace_model.hpp"
28
29
30 namespace Trace {
31
32
33 void Void::visit(Visitor &visitor) {
34    visitor.visit(this);
35 }
36
37 void Bool::visit(Visitor &visitor) {
38    visitor.visit(this);
39 }
40
41 void SInt::visit(Visitor &visitor) {
42    visitor.visit(this);
43 }
44
45 void UInt::visit(Visitor &visitor) {
46    visitor.visit(this);
47 }
48
49 void Float::visit(Visitor &visitor) {
50    visitor.visit(this);
51 }
52
53 void String::visit(Visitor &visitor) {
54    visitor.visit(this);
55 }
56
57 void Const::visit(Visitor &visitor) {
58    visitor.visit(this);
59 }
60
61 void Array::visit(Visitor &visitor) {
62    visitor.visit(this);
63 }
64
65
66 class Dumper : public Visitor
67 {
68 public:
69    std::ostream &os;
70
71    Dumper() : os(std::cout) {}
72
73    Dumper(std::ostream &_os) : os(_os) {}
74
75    void visit(Void *node) {
76    }
77
78    void visit(Bool *node) {
79       os << (node->value ? "true" : "false");
80    }
81
82    void visit(SInt *node) {
83       os << node->value;
84    }
85
86    void visit(UInt *node) {
87       os << node->value;
88    }
89
90    void visit(Float *node) {
91       os << node->value;
92    }
93
94    void visit(String *node) {
95       os << '"' << node->value << '"';
96    }
97
98    void visit(Const *node) {
99       os << node->name;
100    }
101
102    void visit(Array *node) {
103       const char *sep = "";
104       os << "{";
105       for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
106          os << sep;
107          (*it)->visit(*this);
108          sep = ", ";
109       }
110       os << "}";
111    }
112 };
113
114
115 std::ostream & operator <<(std::ostream &os, Value *value) {
116    Dumper d(os);
117    if (value) {
118       value->visit(d);
119    }
120    return os;
121 }
122
123
124 static inline const Value *unwrap(const Value *node) {
125    const Const *c = dynamic_cast<const Const *>(node);
126    if (c)
127       return c->value;
128    return node;
129 }
130
131
132 Value::operator signed long long(void) const {
133    const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
134    if (sint)
135       return sint->value;
136    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
137    if (uint)
138       return uint->value;
139    assert(0);
140    return 0;
141 }
142
143 Value::operator unsigned long long(void) const {
144    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
145    if (uint)
146       return uint->value;
147    assert(0);
148    return 0;
149 }
150
151
152 Value::operator double(void) const {
153    const Float *fl = dynamic_cast<const Float *>(unwrap(this));
154    assert(fl);
155    return fl->value;
156 }
157
158 static Void void_;
159
160 const Value & Value::operator[](size_t index) const {
161     const Array *array = dynamic_cast<const Array *>(unwrap(this));
162     if (array) {
163         if (index < array->values.size()) {
164             return *array->values[index];
165         }
166     }
167     return void_;
168 }
169
170 Value & Call::arg(const char *name) {
171    for (std::list<Arg>::iterator it = args.begin(); it != args.end(); ++it) {
172       if (it->first == name) {
173          return *it->second;
174       }
175    }
176    return void_;
177 }
178
179 std::ostream & operator <<(std::ostream &os, Call &call) {
180    const char *sep = "";
181    os << call.name << "(";
182    for (std::list<Arg>::iterator it = call.args.begin(); it != call.args.end(); ++it) {
183       os << sep << it->first << " = " << it->second;
184       sep = ", ";
185    }
186    os << ")";
187    if (call.ret) {
188       os << " = " << call.ret;
189    }
190    os << "\n";
191    return os;
192 }
193
194
195 } /* namespace Trace */