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