]> git.cworth.org Git - apitrace/blob - trace_model.cpp
Support glTexImage through blobs.
[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 void Blob::visit(Visitor &visitor) {
66    visitor.visit(this);
67 }
68
69
70 class Dumper : public Visitor
71 {
72 public:
73    std::ostream &os;
74
75    Dumper() : os(std::cout) {}
76
77    Dumper(std::ostream &_os) : os(_os) {}
78
79    void visit(Void *node) {
80    }
81
82    void visit(Bool *node) {
83       os << (node->value ? "true" : "false");
84    }
85
86    void visit(SInt *node) {
87       os << node->value;
88    }
89
90    void visit(UInt *node) {
91       os << node->value;
92    }
93
94    void visit(Float *node) {
95       os << node->value;
96    }
97
98    void visit(String *node) {
99       os << '"' << node->value << '"';
100    }
101
102    void visit(Const *node) {
103       os << node->name;
104    }
105
106    void visit(Array *node) {
107       const char *sep = "";
108       os << "{";
109       for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
110          os << sep;
111          (*it)->visit(*this);
112          sep = ", ";
113       }
114       os << "}";
115    }
116    
117    void visit(Blob *blob) {
118       os << "... " << blob->size;
119    }
120 };
121
122
123 std::ostream & operator <<(std::ostream &os, Value *value) {
124    Dumper d(os);
125    if (value) {
126       value->visit(d);
127    }
128    return os;
129 }
130
131
132 static inline const Value *unwrap(const Value *node) {
133    const Const *c = dynamic_cast<const Const *>(node);
134    if (c)
135       return c->value;
136    return node;
137 }
138
139
140 Value::operator signed long long(void) const {
141    const SInt *sint = dynamic_cast<const SInt *>(unwrap(this));
142    if (sint)
143       return sint->value;
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 Value::operator unsigned long long(void) const {
152    const UInt *uint = dynamic_cast<const UInt *>(unwrap(this));
153    if (uint)
154       return uint->value;
155    assert(0);
156    return 0;
157 }
158
159
160 Value::operator double(void) const {
161    const Float *fl = dynamic_cast<const Float *>(unwrap(this));
162    assert(fl);
163    return fl->value;
164 }
165
166 static Void void_;
167
168 const Value & Value::operator[](size_t index) const {
169     const Array *array = dynamic_cast<const Array *>(unwrap(this));
170     if (array) {
171         if (index < array->values.size()) {
172             return *array->values[index];
173         }
174     }
175     return void_;
176 }
177
178 Value::operator void *(void) const {
179    const Blob *blob = dynamic_cast<const Blob *>(unwrap(this));
180    assert(blob);
181    return blob->buf;
182 }
183
184 Value & Call::arg(const char *name) {
185    for (std::list<Arg>::iterator it = args.begin(); it != args.end(); ++it) {
186       if (it->first == name) {
187          return *it->second;
188       }
189    }
190    return void_;
191 }
192
193 std::ostream & operator <<(std::ostream &os, Call &call) {
194    const char *sep = "";
195    os << call.name << "(";
196    for (std::list<Arg>::iterator it = call.args.begin(); it != call.args.end(); ++it) {
197       os << sep << it->first << " = " << it->second;
198       sep = ", ";
199    }
200    os << ")";
201    if (call.ret) {
202       os << " = " << call.ret;
203    }
204    os << "\n";
205    return os;
206 }
207
208
209 } /* namespace Trace */