@@ -679,13 +679,12 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
679
679
struct BuiltStyledStreamWriter : public StreamWriter
680
680
{
681
681
BuiltStyledStreamWriter (
682
- std::ostream* sout,
683
682
std::string const & indentation,
684
683
StreamWriter::CommentStyle::Enum cs,
685
684
std::string const & colonSymbol,
686
685
std::string const & nullSymbol,
687
686
std::string const & endingLineFeedSymbol);
688
- virtual int write (Value const & root);
687
+ virtual int write (Value const & root, std::ostream* sout );
689
688
private:
690
689
void writeValue (Value const & value);
691
690
void writeArrayValue (Value const & value);
@@ -713,14 +712,12 @@ struct BuiltStyledStreamWriter : public StreamWriter
713
712
bool indented_ : 1 ;
714
713
};
715
714
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
716
- std::ostream* sout,
717
715
std::string const & indentation,
718
716
StreamWriter::CommentStyle::Enum cs,
719
717
std::string const & colonSymbol,
720
718
std::string const & nullSymbol,
721
719
std::string const & endingLineFeedSymbol)
722
- : StreamWriter(sout)
723
- , rightMargin_(74 )
720
+ : rightMargin_(74 )
724
721
, indentation_(indentation)
725
722
, cs_(cs)
726
723
, colonSymbol_(colonSymbol)
@@ -730,8 +727,9 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
730
727
, indented_(false )
731
728
{
732
729
}
733
- int BuiltStyledStreamWriter::write (Value const & root)
730
+ int BuiltStyledStreamWriter::write (Value const & root, std::ostream* sout )
734
731
{
732
+ sout_ = sout;
735
733
addChildValues_ = false ;
736
734
indented_ = true ;
737
735
indentString_ = " " ;
@@ -740,7 +738,8 @@ int BuiltStyledStreamWriter::write(Value const& root)
740
738
indented_ = true ;
741
739
writeValue (root);
742
740
writeCommentAfterValueOnSameLine (root);
743
- sout_ << endingLineFeedSymbol_;
741
+ *sout_ << endingLineFeedSymbol_;
742
+ sout_ = NULL ;
744
743
return 0 ;
745
744
}
746
745
void BuiltStyledStreamWriter::writeValue (Value const & value) {
@@ -779,13 +778,13 @@ void BuiltStyledStreamWriter::writeValue(Value const& value) {
779
778
Value const & childValue = value[name];
780
779
writeCommentBeforeValue (childValue);
781
780
writeWithIndent (valueToQuotedString (name.c_str ()));
782
- sout_ << colonSymbol_;
781
+ * sout_ << colonSymbol_;
783
782
writeValue (childValue);
784
783
if (++it == members.end ()) {
785
784
writeCommentAfterValueOnSameLine (childValue);
786
785
break ;
787
786
}
788
- sout_ << " ," ;
787
+ * sout_ << " ," ;
789
788
writeCommentAfterValueOnSameLine (childValue);
790
789
}
791
790
unindent ();
@@ -821,23 +820,23 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
821
820
writeCommentAfterValueOnSameLine (childValue);
822
821
break ;
823
822
}
824
- sout_ << " ," ;
823
+ * sout_ << " ," ;
825
824
writeCommentAfterValueOnSameLine (childValue);
826
825
}
827
826
unindent ();
828
827
writeWithIndent (" ]" );
829
828
} else // output on a single line
830
829
{
831
830
assert (childValues_.size () == size);
832
- sout_ << " [" ;
833
- if (!indentation_.empty ()) sout_ << " " ;
831
+ * sout_ << " [" ;
832
+ if (!indentation_.empty ()) * sout_ << " " ;
834
833
for (unsigned index = 0 ; index < size; ++index ) {
835
834
if (index > 0 )
836
- sout_ << " , " ;
837
- sout_ << childValues_[index ];
835
+ * sout_ << " , " ;
836
+ * sout_ << childValues_[index ];
838
837
}
839
- if (!indentation_.empty ()) sout_ << " " ;
840
- sout_ << " ]" ;
838
+ if (!indentation_.empty ()) * sout_ << " " ;
839
+ * sout_ << " ]" ;
841
840
}
842
841
}
843
842
}
@@ -874,7 +873,7 @@ void BuiltStyledStreamWriter::pushValue(std::string const& value) {
874
873
if (addChildValues_)
875
874
childValues_.push_back (value);
876
875
else
877
- sout_ << value;
876
+ * sout_ << value;
878
877
}
879
878
880
879
void BuiltStyledStreamWriter::writeIndent () {
@@ -885,13 +884,13 @@ void BuiltStyledStreamWriter::writeIndent() {
885
884
886
885
if (!indentation_.empty ()) {
887
886
// In this case, drop newlines too.
888
- sout_ << ' \n ' << indentString_;
887
+ * sout_ << ' \n ' << indentString_;
889
888
}
890
889
}
891
890
892
891
void BuiltStyledStreamWriter::writeWithIndent (std::string const & value) {
893
892
if (!indented_) writeIndent ();
894
- sout_ << value;
893
+ * sout_ << value;
895
894
indented_ = false ;
896
895
}
897
896
@@ -911,11 +910,11 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
911
910
const std::string& comment = root.getComment (commentBefore);
912
911
std::string::const_iterator iter = comment.begin ();
913
912
while (iter != comment.end ()) {
914
- sout_ << *iter;
913
+ * sout_ << *iter;
915
914
if (*iter == ' \n ' &&
916
915
(iter != comment.end () && *(iter + 1 ) == ' /' ))
917
916
// writeIndent(); // would write extra newline
918
- sout_ << indentString_;
917
+ * sout_ << indentString_;
919
918
++iter;
920
919
}
921
920
indented_ = false ;
@@ -924,11 +923,11 @@ void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
924
923
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine (Value const & root) {
925
924
if (cs_ == CommentStyle::None) return ;
926
925
if (root.hasComment (commentAfterOnSameLine))
927
- sout_ << " " + root.getComment (commentAfterOnSameLine);
926
+ * sout_ << " " + root.getComment (commentAfterOnSameLine);
928
927
929
928
if (root.hasComment (commentAfter)) {
930
929
writeIndent ();
931
- sout_ << root.getComment (commentAfter);
930
+ * sout_ << root.getComment (commentAfter);
932
931
}
933
932
}
934
933
@@ -942,8 +941,8 @@ bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
942
941
// /////////////
943
942
// StreamWriter
944
943
945
- StreamWriter::StreamWriter (std::ostream* sout )
946
- : sout_(*sout )
944
+ StreamWriter::StreamWriter ()
945
+ : sout_(NULL )
947
946
{
948
947
}
949
948
StreamWriter::~StreamWriter ()
@@ -957,7 +956,7 @@ StreamWriterBuilder::StreamWriterBuilder()
957
956
}
958
957
StreamWriterBuilder::~StreamWriterBuilder ()
959
958
{}
960
- StreamWriter* StreamWriterBuilder::newStreamWriter (std::ostream* stream ) const
959
+ StreamWriter* StreamWriterBuilder::newStreamWriter () const
961
960
{
962
961
if (!validate (NULL )) throw std::runtime_error (" invalid settings" );
963
962
// TODO: Maybe serialize the invalid settings into the exception.
@@ -978,7 +977,7 @@ StreamWriter* StreamWriterBuilder::newStreamWriter(std::ostream* stream) const
978
977
}
979
978
std::string nullSymbol = " null" ;
980
979
std::string endingLineFeedSymbol = " " ;
981
- return new BuiltStyledStreamWriter (stream,
980
+ return new BuiltStyledStreamWriter (
982
981
indentation, cs,
983
982
colonSymbol, nullSymbol, endingLineFeedSymbol);
984
983
}
@@ -1015,24 +1014,7 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
1015
1014
// ! [StreamWriterBuilderDefaults]
1016
1015
}
1017
1016
1018
- /*
1019
- // This might become public someday.
1020
- class StreamWriterBuilderFactory {
1021
- public:
1022
- virtual ~StreamWriterBuilderFactory();
1023
- virtual StreamWriterBuilder* newStreamWriterBuilder() const;
1024
- };
1025
- StreamWriterBuilderFactory::~StreamWriterBuilderFactory()
1026
- {
1027
- }
1028
- StreamWriterBuilder* StreamWriterBuilderFactory::newStreamWriterBuilder() const
1029
- {
1030
- return new StreamWriterBuilder;
1031
- }
1032
- */
1033
-
1034
- StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter (
1035
- std::ostream* stream) const
1017
+ StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter () const
1036
1018
{
1037
1019
std::string colonSymbol = " : " ;
1038
1020
if (enableYAMLCompatibility_) {
@@ -1048,22 +1030,22 @@ StreamWriter* OldCompressingStreamWriterBuilder::newStreamWriter(
1048
1030
if (omitEndingLineFeed_) {
1049
1031
endingLineFeedSymbol = " " ;
1050
1032
}
1051
- return new BuiltStyledStreamWriter (stream,
1033
+ return new BuiltStyledStreamWriter (
1052
1034
" " , StreamWriter::CommentStyle::None,
1053
1035
colonSymbol, nullSymbol, endingLineFeedSymbol);
1054
1036
}
1055
1037
1056
1038
std::string writeString (StreamWriter::Factory const & builder, Value const & root) {
1057
1039
std::ostringstream sout;
1058
- StreamWriterPtr const writer (builder.newStreamWriter (&sout ));
1059
- writer->write (root);
1040
+ StreamWriterPtr const writer (builder.newStreamWriter ());
1041
+ writer->write (root, &sout );
1060
1042
return sout.str ();
1061
1043
}
1062
1044
1063
1045
std::ostream& operator <<(std::ostream& sout, Value const & root) {
1064
1046
StreamWriterBuilder builder;
1065
- StreamWriterPtr const writer (builder.newStreamWriter (&sout ));
1066
- writer->write (root);
1047
+ StreamWriterPtr const writer (builder.newStreamWriter ());
1048
+ writer->write (root, &sout );
1067
1049
return sout;
1068
1050
}
1069
1051
0 commit comments