Skip to content

Commit fda9523

Browse files
committed
abstract-tree: introduce AbstractTreeEncoder
... as an abstraction over various tree formats No changes in behavior intended by this commit.
1 parent a6a02f6 commit fda9523

File tree

2 files changed

+91
-46
lines changed

2 files changed

+91
-46
lines changed

src/abstract-tree.hh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ class AbstractTreeDecoder {
5555
= 0;
5656
};
5757

58+
/// abstraction for higher-level encoders for various tree-based file formats
59+
class AbstractTreeEncoder {
60+
public:
61+
virtual ~AbstractTreeEncoder() { }
62+
63+
/// import supported scan properties
64+
virtual void importScanProps(const TScanProps &) { }
65+
66+
/// append single defect
67+
virtual void appendDef(const Defect &) = 0;
68+
69+
/// write everything to the given output stream
70+
virtual void writeTo(std::ostream &) = 0;
71+
};
72+
5873
template <typename TNode>
5974
bool findChildOf(TNode **pDst, TNode &node, const char *key)
6075
{

src/json-writer.cc

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "json-writer.hh"
2121

22+
#include "abstract-tree.hh"
2223
#include "regex.hh"
2324
#include "shared-string-ptree.hh"
2425

@@ -30,45 +31,35 @@
3031

3132
typedef SharedStringPTree PTree;
3233

33-
struct JsonWriter::Private {
34-
std::ostream &str;
35-
std::queue<Defect> defQueue;
36-
TScanProps scanProps;
34+
class SimpleTreeEncoder: public AbstractTreeEncoder {
35+
public:
36+
/// import supported scan properties
37+
void importScanProps(const TScanProps &) override;
3738

38-
Private(std::ostream &str_):
39-
str(str_)
40-
{
41-
}
42-
};
39+
/// append single defect
40+
void appendDef(const Defect &) override;
4341

44-
JsonWriter::JsonWriter(std::ostream &str, const EFileFormat format):
45-
d(new Private(str))
46-
{
47-
switch (format) {
48-
case FF_JSON:
49-
break;
42+
/// write everything to the given output stream
43+
void writeTo(std::ostream &) override;
5044

51-
default:
52-
throw std::runtime_error("unknown output format");
53-
}
54-
}
45+
private:
46+
PTree root_;
47+
PTree *pDefects_ = nullptr;
48+
};
5549

56-
JsonWriter::~JsonWriter()
50+
void SimpleTreeEncoder::importScanProps(const TScanProps &scanProps)
5751
{
58-
delete d;
59-
}
52+
if (scanProps.empty())
53+
return;
6054

61-
const TScanProps& JsonWriter::getScanProps() const
62-
{
63-
return d->scanProps;
64-
}
55+
PTree scan;
56+
for (TScanProps::const_reference prop : scanProps)
57+
scan.put<std::string>(prop.first, prop.second);
6558

66-
void JsonWriter::setScanProps(const TScanProps &scanProps)
67-
{
68-
d->scanProps = scanProps;
59+
root_.put_child("scan", scan);
6960
}
7061

71-
void appendDefectNode(PTree &dst, const Defect &def)
62+
void SimpleTreeEncoder::appendDef(const Defect &def)
7263
{
7364
using std::string;
7465

@@ -113,8 +104,58 @@ void appendDefectNode(PTree &dst, const Defect &def)
113104
defNode.put<int>("key_event_idx", def.keyEventIdx);
114105
defNode.put_child("events", evtList);
115106

107+
// create the node representing the list of defects
108+
if (!pDefects_)
109+
pDefects_ = &root_.put_child("defects", PTree());
110+
116111
// append the node to the list
117-
dst.push_back(std::make_pair("", defNode));
112+
pDefects_->push_back(std::make_pair("", defNode));
113+
}
114+
115+
void SimpleTreeEncoder::writeTo(std::ostream &str)
116+
{
117+
write_json(str, root_);
118+
}
119+
120+
struct JsonWriter::Private {
121+
std::ostream &str;
122+
std::queue<Defect> defQueue;
123+
TScanProps scanProps;
124+
AbstractTreeEncoder *encoder;
125+
126+
Private(std::ostream &str_):
127+
str(str_)
128+
{
129+
}
130+
};
131+
132+
JsonWriter::JsonWriter(std::ostream &str, const EFileFormat format):
133+
d(new Private(str))
134+
{
135+
switch (format) {
136+
case FF_JSON:
137+
d->encoder = new SimpleTreeEncoder;
138+
break;
139+
140+
default:
141+
throw std::runtime_error("unknown output format");
142+
}
143+
}
144+
145+
JsonWriter::~JsonWriter()
146+
{
147+
delete d->encoder;
148+
delete d;
149+
}
150+
151+
const TScanProps& JsonWriter::getScanProps() const
152+
{
153+
return d->scanProps;
154+
}
155+
156+
void JsonWriter::setScanProps(const TScanProps &scanProps)
157+
{
158+
d->scanProps = scanProps;
118159
}
119160

120161
void JsonWriter::handleDef(const Defect &def)
@@ -143,24 +184,13 @@ void JsonWriter::flush()
143184

144185
str.push(d->str);
145186

146-
// encode scan properties if we have some
147-
PTree root;
148-
if (!d->scanProps.empty()) {
149-
PTree scan;
150-
for (TScanProps::const_reference prop : d->scanProps)
151-
scan.put<std::string>(prop.first, prop.second);
152-
153-
root.put_child("scan", scan);
154-
}
155-
156-
// node representing the list of defects
157-
root.put_child("defects", PTree());
158-
PTree &defects = root.get_child("defects");
187+
// transfer scan properties if available
188+
d->encoder->importScanProps(d->scanProps);
159189

160190
// go through the queue and move defects one by one to the property tree
161191
for (; !d->defQueue.empty(); d->defQueue.pop())
162-
appendDefectNode(defects, d->defQueue.front());
192+
d->encoder->appendDef(d->defQueue.front());
163193

164194
// finally encode the tree as JSON
165-
write_json(str, root);
195+
d->encoder->writeTo(str);
166196
}

0 commit comments

Comments
 (0)