@@ -671,56 +671,51 @@ struct BuiltStyledStreamWriter : public StreamWriter
671
671
std::ostream* sout,
672
672
std::string const & indentation,
673
673
StreamWriter::CommentStyle cs);
674
- virtual int write (Value const & root) const ;
674
+ virtual int write (Value const & root);
675
675
private:
676
- void writeValue (const Value& value);
677
- void writeArrayValue (const Value& value);
678
- bool isMultineArray (const Value& value);
679
- void pushValue (const std::string& value);
676
+ void writeValue (Value const & value);
677
+ void writeArrayValue (Value const & value);
678
+ bool isMultineArray (Value const & value);
679
+ void pushValue (std::string const & value);
680
680
void writeIndent ();
681
- void writeWithIndent (const std::string& value);
681
+ void writeWithIndent (std::string const & value);
682
682
void indent ();
683
683
void unindent ();
684
- void writeCommentBeforeValue (const Value& root);
685
- void writeCommentAfterValueOnSameLine (const Value& root);
684
+ void writeCommentBeforeValue (Value const & root);
685
+ void writeCommentAfterValueOnSameLine (Value const & root);
686
686
bool hasCommentForValue (const Value& value);
687
- static std::string normalizeEOL (const std::string& text);
687
+ static std::string normalizeEOL (std::string const & text);
688
688
689
689
typedef std::vector<std::string> ChildValues;
690
690
691
691
ChildValues childValues_;
692
- std::ostream* document_;
693
692
std::string indentString_;
694
693
int rightMargin_;
695
694
std::string indentation_;
695
+ CommentStyle cs_;
696
696
bool addChildValues_;
697
697
};
698
698
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
699
699
std::ostream* sout,
700
700
std::string const & indentation,
701
701
StreamWriter::CommentStyle cs)
702
702
: StreamWriter(sout)
703
- , indentation_(indentation)
704
703
, rightMargin_(74 )
704
+ , indentation_(indentation)
705
+ , cs_(cs)
705
706
{
706
707
}
707
- int BuiltStyledStreamWriter::write (Value const & root) const
708
+ int BuiltStyledStreamWriter::write (Value const & root)
708
709
{
709
- write (sout_, root);
710
- return 0 ;
711
- }
712
- void BuiltStyledStreamWriter::write (std::ostream& out, const Value& root) {
713
- document_ = &out;
714
710
addChildValues_ = false ;
715
711
indentString_ = " " ;
716
712
writeCommentBeforeValue (root);
717
713
writeValue (root);
718
714
writeCommentAfterValueOnSameLine (root);
719
- *document_ << " \n " ;
720
- document_ = NULL ; // Forget the stream, for safety.
715
+ sout_ << " \n " ;
716
+ return 0 ;
721
717
}
722
-
723
- void BuiltStyledStreamWriter::writeValue (const Value& value) {
718
+ void BuiltStyledStreamWriter::writeValue (Value const & value) {
724
719
switch (value.type ()) {
725
720
case nullValue:
726
721
pushValue (" null" );
@@ -752,17 +747,17 @@ void BuiltStyledStreamWriter::writeValue(const Value& value) {
752
747
indent ();
753
748
Value::Members::iterator it = members.begin ();
754
749
for (;;) {
755
- const std::string& name = *it;
756
- const Value& childValue = value[name];
750
+ std::string const & name = *it;
751
+ Value const & childValue = value[name];
757
752
writeCommentBeforeValue (childValue);
758
753
writeWithIndent (valueToQuotedString (name.c_str ()));
759
- *document_ << " : " ;
754
+ sout_ << " : " ;
760
755
writeValue (childValue);
761
756
if (++it == members.end ()) {
762
757
writeCommentAfterValueOnSameLine (childValue);
763
758
break ;
764
759
}
765
- *document_ << " ," ;
760
+ sout_ << " ," ;
766
761
writeCommentAfterValueOnSameLine (childValue);
767
762
}
768
763
unindent ();
@@ -772,7 +767,7 @@ void BuiltStyledStreamWriter::writeValue(const Value& value) {
772
767
}
773
768
}
774
769
775
- void BuiltStyledStreamWriter::writeArrayValue (const Value& value) {
770
+ void BuiltStyledStreamWriter::writeArrayValue (Value const & value) {
776
771
unsigned size = value.size ();
777
772
if (size == 0 )
778
773
pushValue (" []" );
@@ -784,7 +779,7 @@ void BuiltStyledStreamWriter::writeArrayValue(const Value& value) {
784
779
bool hasChildValue = !childValues_.empty ();
785
780
unsigned index = 0 ;
786
781
for (;;) {
787
- const Value& childValue = value[index];
782
+ Value const & childValue = value[index];
788
783
writeCommentBeforeValue (childValue);
789
784
if (hasChildValue)
790
785
writeWithIndent (childValues_[index]);
@@ -796,31 +791,31 @@ void BuiltStyledStreamWriter::writeArrayValue(const Value& value) {
796
791
writeCommentAfterValueOnSameLine (childValue);
797
792
break ;
798
793
}
799
- *document_ << " ," ;
794
+ sout_ << " ," ;
800
795
writeCommentAfterValueOnSameLine (childValue);
801
796
}
802
797
unindent ();
803
798
writeWithIndent (" ]" );
804
799
} else // output on a single line
805
800
{
806
801
assert (childValues_.size () == size);
807
- *document_ << " [ " ;
802
+ sout_ << " [ " ;
808
803
for (unsigned index = 0 ; index < size; ++index) {
809
804
if (index > 0 )
810
- *document_ << " , " ;
811
- *document_ << childValues_[index];
805
+ sout_ << " , " ;
806
+ sout_ << childValues_[index];
812
807
}
813
- *document_ << " ]" ;
808
+ sout_ << " ]" ;
814
809
}
815
810
}
816
811
}
817
812
818
- bool BuiltStyledStreamWriter::isMultineArray (const Value& value) {
813
+ bool BuiltStyledStreamWriter::isMultineArray (Value const & value) {
819
814
int size = value.size ();
820
815
bool isMultiLine = size * 3 >= rightMargin_;
821
816
childValues_.clear ();
822
817
for (int index = 0 ; index < size && !isMultiLine; ++index) {
823
- const Value& childValue = value[index];
818
+ Value const & childValue = value[index];
824
819
isMultiLine =
825
820
isMultiLine || ((childValue.isArray () || childValue.isObject ()) &&
826
821
childValue.size () > 0 );
@@ -840,32 +835,32 @@ bool BuiltStyledStreamWriter::isMultineArray(const Value& value) {
840
835
return isMultiLine;
841
836
}
842
837
843
- void BuiltStyledStreamWriter::pushValue (const std::string& value) {
838
+ void BuiltStyledStreamWriter::pushValue (std::string const & value) {
844
839
if (addChildValues_)
845
840
childValues_.push_back (value);
846
841
else
847
- *document_ << value;
842
+ sout_ << value;
848
843
}
849
844
850
845
void BuiltStyledStreamWriter::writeIndent () {
851
846
/*
852
847
Some comments in this method would have been nice. ;-)
853
848
854
- if ( !document_ .empty() )
849
+ if ( !sout_ .empty() )
855
850
{
856
- char last = document_[document_ .length()-1];
851
+ char last = sout_[sout_ .length()-1];
857
852
if ( last == ' ' ) // already indented
858
853
return;
859
854
if ( last != '\n' ) // Comments may add new-line
860
- *document_ << '\n';
855
+ sout_ << '\n';
861
856
}
862
857
*/
863
- *document_ << ' \n ' << indentString_;
858
+ sout_ << ' \n ' << indentString_;
864
859
}
865
860
866
- void BuiltStyledStreamWriter::writeWithIndent (const std::string& value) {
861
+ void BuiltStyledStreamWriter::writeWithIndent (std::string const & value) {
867
862
writeIndent ();
868
- *document_ << value;
863
+ sout_ << value;
869
864
}
870
865
871
866
void BuiltStyledStreamWriter::indent () { indentString_ += indentation_; }
@@ -875,21 +870,21 @@ void BuiltStyledStreamWriter::unindent() {
875
870
indentString_.resize (indentString_.size () - indentation_.size ());
876
871
}
877
872
878
- void BuiltStyledStreamWriter::writeCommentBeforeValue (const Value& root) {
873
+ void BuiltStyledStreamWriter::writeCommentBeforeValue (Value const & root) {
879
874
if (!root.hasComment (commentBefore))
880
875
return ;
881
- *document_ << root.getComment (commentBefore);
882
- *document_ << " \n " ;
876
+ sout_ << root.getComment (commentBefore);
877
+ sout_ << " \n " ;
883
878
}
884
879
885
- void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine (const Value& root) {
880
+ void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine (Value const & root) {
886
881
if (root.hasComment (commentAfterOnSameLine))
887
- *document_ << " " + root.getComment (commentAfterOnSameLine);
882
+ sout_ << " " + root.getComment (commentAfterOnSameLine);
888
883
889
884
if (root.hasComment (commentAfter)) {
890
- *document_ << " \n " ;
891
- *document_ << root.getComment (commentAfter);
892
- *document_ << " \n " ;
885
+ sout_ << " \n " ;
886
+ sout_ << root.getComment (commentAfter);
887
+ sout_ << " \n " ;
893
888
}
894
889
}
895
890
@@ -910,7 +905,7 @@ struct MyStreamWriter : public StreamWriter {
910
905
public:
911
906
MyStreamWriter (std::ostream* sout);
912
907
virtual ~MyStreamWriter ();
913
- virtual int write (Value const & root) const = 0;
908
+ virtual int write (Value const & root) = 0;
914
909
};
915
910
MyStreamWriter::MyStreamWriter (std::ostream* sout)
916
911
: StreamWriter(sout)
@@ -919,7 +914,7 @@ MyStreamWriter::MyStreamWriter(std::ostream* sout)
919
914
MyStreamWriter::~MyStreamWriter ()
920
915
{
921
916
}
922
- int MyStreamWriter::write (Value const & root) const
917
+ int MyStreamWriter::write (Value const & root)
923
918
{
924
919
sout_ << root;
925
920
return 0 ;
@@ -988,7 +983,7 @@ std::string writeString(Value const& root, StreamWriterBuilder const& builder) {
988
983
return sout.str ();
989
984
}
990
985
991
- std::ostream& operator <<(std::ostream& sout, const Value& root) {
986
+ std::ostream& operator <<(std::ostream& sout, Value const & root) {
992
987
StreamWriterBuilderFactory f;
993
988
StreamWriter::Builder builder (&f);
994
989
builder.setCommentStyle (StreamWriter::CommentStyle::Some);
0 commit comments