]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Start migrating to the visitor pattern.
[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 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 signed long long asSInt(const Value &node) {
132    const SInt *sint = dynamic_cast<const SInt *>(unwrap(node));
133    if (sint)
134       return sint->value;
135    const UInt *uint = dynamic_cast<const UInt *>(unwrap(node));
136    if (uint)
137       return uint->value;
138    assert(0);
139    return 0;
140 }
141
142 unsigned long long asUInt(const Value &node) {
143    const UInt *uint = dynamic_cast<const UInt *>(unwrap(node));
144    if (uint)
145       return uint->value;
146    assert(0);
147    return 0;
148 }
149
150
151 double asFloat(const Value &node) {
152    const Float *fl = dynamic_cast<const Float *>(unwrap(node));
153    assert(fl);
154    return fl->value;
155 }
156
157 static Void void_;
158
159 Value & Call::arg(const char *name) {
160    for (std::list<Arg>::iterator it = args.begin(); it != args.end(); ++it) {
161       if (it->first == name) {
162          return *it->second;
163       }
164    }
165    return void_;
166 }
167
168 std::ostream & operator <<(std::ostream &os, Call &call) {
169    const char *sep = "";
170    os << call.name << "(";
171    for (std::list<Arg>::iterator it = call.args.begin(); it != call.args.end(); ++it) {
172       os << sep << it->first << " = " << it->second;
173       sep = ", ";
174    }
175    os << ")";
176    if (call.ret) {
177       os << " = " << call.ret;
178    }
179    os << "\n";
180    return os;
181 }
182
183
184 } /* namespace Trace */