]> git.cworth.org Git - apitrace/blob - common/trace_writer_model.cpp
Rename trace writer files to group nicely.
[apitrace] / common / trace_writer_model.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
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_writer.hpp"
28
29
30 namespace Trace {
31
32
33 class ModelWriter : public Visitor
34 {
35 protected:
36     Writer &writer;
37
38 public:
39     ModelWriter(Writer &_writer) :
40         writer(_writer) {
41     }
42
43     void visit(Null *) {
44         writer.writeNull();
45     }
46
47     void visit(Bool *node) {
48         writer.writeBool(node->value);
49     }
50
51     void visit(SInt *node) {
52         writer.writeSInt(node->value);
53     }
54
55     void visit(UInt *node) {
56         writer.writeUInt(node->value);
57     }
58
59     void visit(Float *node) {
60         writer.writeFloat(node->value);
61     }
62
63     void visit(String *node) {
64         writer.writeString(node->value);
65     }
66
67     void visit(Enum *node) {
68         writer.writeEnum(node->sig);
69     }
70
71     void visit(Bitmask *node) {
72         writer.writeBitmask(node->sig, node->value);
73     }
74
75     void visit(Struct *node) {
76         writer.beginStruct(node->sig);
77         for (unsigned i = 0; i < node->sig->num_members; ++i) {
78             _visit(node->members[i]);
79         }
80         writer.endStruct();
81     }
82
83     void visit(Array *node) {
84         writer.beginArray(node->values.size());
85         for (std::vector<Value *>::iterator it = node->values.begin(); it != node->values.end(); ++it) {
86             _visit(*it);
87         }
88         writer.endArray();
89     }
90
91     void visit(Blob *node) {
92         writer.writeBlob(node->buf, node->size);
93     }
94
95     void visit(Pointer *node) {
96         writer.writeOpaque((const void *) (size_t) node->value);
97     }
98
99     void visit(Call *call) {
100         unsigned call_no = writer.beginEnter(call->sig);
101         for (unsigned i = 0; i < call->args.size(); ++i) {
102             if (call->args[i]) {
103                 writer.beginArg(i);
104                 _visit(call->args[i]);
105                 writer.endArg();
106             }
107         }
108         writer.endEnter();
109         writer.beginLeave(call_no);
110         if (call->ret) {
111             writer.beginReturn();
112             _visit(call->ret);
113             writer.endReturn();
114         }
115         writer.endLeave();
116     }
117 };
118
119
120 void Writer::writeCall(Call *call) {
121     ModelWriter visitor(*this);
122     visitor.visit(call);
123 }
124
125
126 } /* namespace Trace */
127