From fe3979cd8a94c4a23994171a6e48c6905cef05b5 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sat, 24 Jan 2015 13:54:28 -0600 Subject: [PATCH] drop StreamWriterBuilderFactory, for now --- include/json/writer.h | 14 +++++--------- src/jsontestrunner/main.cpp | 3 +-- src/lib_json/json_writer.cpp | 19 +++++++++++++++---- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/include/json/writer.h b/include/json/writer.h index b207849..6e46cf1 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -30,26 +30,20 @@ Usage: using namespace Json; Value value; - StreamWriterBuilderFactory f; - StreamWriter::Builder builder(&f); + StreamWriter::Builder builder; builder.setCommentStyle(StreamWriter::CommentStyle::None); std::shared_ptr writer( builder.newStreamWriter(&std::cout)); writer->write(value); std::cout.flush(); */ -class JSON_API StreamWriterBuilderFactory { -public: - virtual ~StreamWriterBuilderFactory(); - virtual StreamWriterBuilder* newStreamWriterBuilder() const; -}; - class JSON_API StreamWriter { protected: std::ostream& sout_; // not owned; will not delete public: enum class CommentStyle {None, Some, All}; + /// Keep a reference, but do not take ownership of `sout`. StreamWriter(std::ostream* sout); virtual ~StreamWriter(); /// Write Value into document as configured in sub-class. @@ -62,8 +56,10 @@ public: /// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility class Builder { StreamWriterBuilder* own_; + Builder(Builder const&); // noncopyable + void operator=(Builder const&); // noncopyable public: - Builder(StreamWriterBuilderFactory const*); + Builder(); ~Builder(); // delete underlying StreamWriterBuilder void setCommentStyle(CommentStyle cs); /// default: All diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp index 61411b4..28894ce 100644 --- a/src/jsontestrunner/main.cpp +++ b/src/jsontestrunner/main.cpp @@ -184,8 +184,7 @@ static std::string useStyledStreamWriter( static std::string useBuiltStyledStreamWriter( Json::Value const& root) { - Json::StreamWriterBuilderFactory f; - Json::StreamWriter::Builder builder(&f); + Json::StreamWriter::Builder builder; builder.setCommentStyle(Json::StreamWriter::CommentStyle::All); return writeString(root, builder); } diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index b6127d3..a3fa50d 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -967,6 +967,13 @@ StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const { return new BuiltStyledStreamWriter(stream, indentation_, cs_); } + +// This might become public someday. +class StreamWriterBuilderFactory { +public: + virtual ~StreamWriterBuilderFactory(); + virtual StreamWriterBuilder* newStreamWriterBuilder() const; +}; StreamWriterBuilderFactory::~StreamWriterBuilderFactory() { } @@ -975,14 +982,19 @@ StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder() const return new StreamWriterBuilder; } -StreamWriter::Builder::Builder(StreamWriterBuilderFactory const* f) - : own_(f->newStreamWriterBuilder()) +StreamWriter::Builder::Builder() + : own_(StreamWriterBuilderFactory().newStreamWriterBuilder()) { } StreamWriter::Builder::~Builder() { delete own_; } +StreamWriter::Builder::Builder(Builder const&) + : own_(nullptr) +{abort();} +void StreamWriter::Builder::operator=(Builder const&) +{abort();} void StreamWriter::Builder::setCommentStyle(CommentStyle v) { own_->setCommentStyle(v); @@ -1006,8 +1018,7 @@ std::string writeString(Value const& root, StreamWriter::Builder const& builder) } std::ostream& operator<<(std::ostream& sout, Value const& root) { - StreamWriterBuilderFactory f; - StreamWriter::Builder builder(&f); + StreamWriter::Builder builder; builder.setCommentStyle(StreamWriter::CommentStyle::All); std::shared_ptr writer(builder.newStreamWriter(&sout)); writer->write(root);