Skip to content

Commit 6065a1c

Browse files
committed
make StreamWriterBuilder concrete
1 parent 28a2091 commit 6065a1c

File tree

3 files changed

+34
-106
lines changed

3 files changed

+34
-106
lines changed

include/json/writer.h

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,58 +58,43 @@ class JSON_API StreamWriter {
5858
/// \throw std::exception possibly, depending on configuration
5959
virtual int write(Value const& root) = 0;
6060

61-
/// Because this Builder is non-virtual, we can safely add
62-
/// methods without a major version bump.
63-
/// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
64-
class JSON_API Builder {
65-
StreamWriterBuilder* own_;
66-
Builder(Builder const&); // noncopyable
67-
void operator=(Builder const&); // noncopyable
68-
public:
69-
Builder();
70-
~Builder(); // delete underlying StreamWriterBuilder
71-
72-
Builder& withCommentStyle(CommentStyle cs); /// default: All
73-
/** \brief Write in human-friendly style.
74-
75-
If "", then skip all indentation, newlines, and comments,
76-
which implies CommentStyle::None.
77-
Default: "\t"
78-
*/
79-
Builder& withIndentation(std::string indentation);
80-
81-
/// Do not take ownership of sout, but maintain a reference.
82-
StreamWriter* newStreamWriter(std::ostream* sout) const;
83-
};
84-
8561
/** \brief A simple abstract factory.
8662
*/
8763
class JSON_API Factory {
8864
public:
8965
virtual ~Factory();
90-
/* Because this is only a trivial API (the Factory pattern), we will
91-
* never need to add virtual methods, so we do not need a concrete wrapper.
92-
* This is better than the Builder above, but not everyone will agree.
93-
*/
94-
9566
/// Do not take ownership of sout, but maintain a reference.
9667
virtual StreamWriter* newStreamWriter(std::ostream* sout) const = 0;
97-
};
68+
}; // Factory
69+
}; // StreamWriter
70+
71+
/// \brief Write into stringstream, then return string, for convenience.
72+
std::string writeString(Value const& root, StreamWriter::Factory const& factory);
9873

99-
/** \brief Extensions of this are used to create a StreamWriter::Factory.
74+
75+
/** \brief Build a StreamWriter implementation.
76+
*/
77+
class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
78+
// typedef StreamWriter::CommentStyle CommentStyle;
79+
public:
80+
// Note: We cannot add data-members to this class without a major version bump.
81+
// So these might as well be completely exposed.
82+
83+
/** \brief How to write comments.
84+
* Default: All
10085
*/
101-
class JSON_API FactoryFactory {
102-
virtual ~FactoryFactory();
103-
virtual Factory* newFactory() const = 0;
104-
/* This class will seem strange to some developers, but it actually
105-
* simplifies our library maintenance.
106-
*/
107-
};
86+
StreamWriter::CommentStyle cs_ = StreamWriter::CommentStyle::All;
87+
/** \brief Write in human-friendly style.
10888
109-
};
89+
If "", then skip all indentation and newlines.
90+
In that case, you probably want CommentStyle::None also.
91+
Default: "\t"
92+
*/
93+
std::string indentation_ = "\t";
11094

111-
/// \brief Write into stringstream, then return string, for convenience.
112-
std::string writeString(Value const& root, StreamWriter::Builder const& builder);
95+
/// Do not take ownership of sout, but maintain a reference.
96+
StreamWriter* newStreamWriter(std::ostream* sout) const;
97+
};
11398

11499
/** \brief Build a StreamWriter implementation.
115100
* Comments are not written, and most whitespace is omitted.
@@ -124,7 +109,7 @@ std::string writeString(Value const& root, StreamWriter::Builder const& builder)
124109
* delete w;
125110
* \endcode
126111
*/
127-
class JSON_API OldCompressingStreamWriterBuilder
112+
class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
128113
{
129114
public:
130115
// Note: We cannot add data-members to this class without a major version bump.

src/jsontestrunner/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ static std::string useStyledStreamWriter(
184184
static std::string useBuiltStyledStreamWriter(
185185
Json::Value const& root)
186186
{
187-
Json::StreamWriter::Builder builder;
188-
builder.withCommentStyle(Json::StreamWriter::CommentStyle::All);
187+
Json::StreamWriterBuilder builder;
189188
return writeString(root, builder);
190189
}
191190
static int rewriteValueTree(

src/lib_json/json_writer.cpp

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -959,34 +959,8 @@ int MyStreamWriter::write(Value const& root)
959959
sout_ << root;
960960
return 0;
961961
}
962-
class StreamWriterBuilder {
963-
typedef StreamWriter::CommentStyle CommentStyle;
964-
CommentStyle cs_;
965-
std::string indentation_;
966-
public:
967-
StreamWriterBuilder();
968-
virtual ~StreamWriterBuilder();
969-
virtual void setCommentStyle(CommentStyle cs);
970-
virtual void setIndentation(std::string indentation);
971-
virtual StreamWriter* newStreamWriter(std::ostream* sout) const;
972-
};
973-
StreamWriterBuilder::StreamWriterBuilder()
974-
: cs_(CommentStyle::All)
975-
, indentation_("\t")
976-
{
977-
}
978-
StreamWriterBuilder::~StreamWriterBuilder()
979-
{
980-
}
981-
void StreamWriterBuilder::setCommentStyle(CommentStyle v)
982-
{
983-
cs_ = v;
984-
}
985-
void StreamWriterBuilder::setIndentation(std::string v)
986-
{
987-
indentation_ = v;
988-
if (indentation_.empty()) cs_ = CommentStyle::None;
989-
}
962+
StreamWriter::Factory::~Factory()
963+
{}
990964
StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
991965
{
992966
std::string colonSymbol = " : ";
@@ -999,7 +973,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
999973
indentation_, cs_,
1000974
colonSymbol, nullSymbol, endingLineFeedSymbol);
1001975
}
1002-
976+
/*
1003977
// This might become public someday.
1004978
class StreamWriterBuilderFactory {
1005979
public:
@@ -1013,35 +987,7 @@ StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder() const
1013987
{
1014988
return new StreamWriterBuilder;
1015989
}
1016-
1017-
StreamWriter::Builder::Builder()
1018-
: own_(StreamWriterBuilderFactory().newStreamWriterBuilder())
1019-
{
1020-
}
1021-
StreamWriter::Builder::~Builder()
1022-
{
1023-
delete own_;
1024-
}
1025-
StreamWriter::Builder::Builder(Builder const&)
1026-
: own_(nullptr)
1027-
{abort();}
1028-
void StreamWriter::Builder::operator=(Builder const&)
1029-
{abort();}
1030-
StreamWriter::Builder& StreamWriter::Builder::withCommentStyle(CommentStyle v)
1031-
{
1032-
own_->setCommentStyle(v);
1033-
return *this;
1034-
}
1035-
StreamWriter::Builder& StreamWriter::Builder::withIndentation(std::string v)
1036-
{
1037-
own_->setIndentation(v);
1038-
return *this;
1039-
}
1040-
StreamWriter* StreamWriter::Builder::newStreamWriter(
1041-
std::ostream* sout) const
1042-
{
1043-
return own_->newStreamWriter(sout);
1044-
}
990+
*/
1045991

1046992
StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
1047993
std::ostream* stream) const
@@ -1065,17 +1011,15 @@ StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
10651011
colonSymbol, nullSymbol, endingLineFeedSymbol);
10661012
}
10671013

1068-
std::string writeString(Value const& root, StreamWriter::Builder const& builder) {
1014+
std::string writeString(Value const& root, StreamWriter::Factory const& builder) {
10691015
std::ostringstream sout;
10701016
std::unique_ptr<StreamWriter> const sw(builder.newStreamWriter(&sout));
10711017
sw->write(root);
10721018
return sout.str();
10731019
}
10741020

10751021
std::ostream& operator<<(std::ostream& sout, Value const& root) {
1076-
StreamWriter::Builder builder;
1077-
builder.withCommentStyle(StreamWriter::CommentStyle::All);
1078-
builder.withIndentation("\t");
1022+
StreamWriterBuilder builder;
10791023
std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&sout));
10801024
writer->write(root);
10811025
return sout;

0 commit comments

Comments
 (0)