Skip to content

Commit

Permalink
Properly handle I/O of pointer to std::string.
Browse files Browse the repository at this point in the history
Like the STL collection a nullptr std::string is stored the same as a pointer to an 'empty' std::string.
  • Loading branch information
pcanal committed Nov 7, 2016
1 parent 4faac4b commit c2c35e8
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 30 deletions.
4 changes: 2 additions & 2 deletions core/base/inc/TBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class TBuffer : public TObject {
virtual void ReadDouble(Double_t &d) = 0;
virtual void ReadCharP(Char_t *c) = 0;
virtual void ReadTString(TString &s) = 0;
virtual void ReadStdString(std::string &s) = 0;
virtual void ReadStdString(std::string *s) = 0;

virtual void WriteBool(Bool_t b) = 0;
virtual void WriteChar(Char_t c) = 0;
Expand All @@ -296,7 +296,7 @@ class TBuffer : public TObject {
virtual void WriteDouble(Double_t d) = 0;
virtual void WriteCharP(const Char_t *c) = 0;
virtual void WriteTString(const TString &s) = 0;
virtual void WriteStdString(const std::string &s) = 0;
virtual void WriteStdString(const std::string *s) = 0;

// Special basic ROOT objects and collections
virtual TProcessID *GetLastProcessID(TRefTable *reftable) const = 0;
Expand Down
4 changes: 2 additions & 2 deletions core/base/src/String.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ void std_string_streamer(TBuffer &b, void *objadd)
{
// Streamer function for std::string object.
if (b.IsReading()) {
b.ReadStdString(*(std::string*)objadd);
b.ReadStdString((std::string*)objadd);
} else {
b.WriteStdString(*(std::string*)objadd);
b.WriteStdString((std::string*)objadd);
}
}

Expand Down
4 changes: 2 additions & 2 deletions io/io/inc/TBufferFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class TBufferFile : public TBuffer {
virtual void ReadDouble(Double_t &d);
virtual void ReadCharP(Char_t *c);
virtual void ReadTString(TString &s);
virtual void ReadStdString(std::string &s);
virtual void ReadStdString(std::string *s);

virtual void WriteBool(Bool_t b);
virtual void WriteChar(Char_t c);
Expand All @@ -289,7 +289,7 @@ class TBufferFile : public TBuffer {
virtual void WriteDouble(Double_t d);
virtual void WriteCharP(const Char_t *c);
virtual void WriteTString(const TString &s);
virtual void WriteStdString(const std::string &s);
virtual void WriteStdString(const std::string *s);

// Special basic ROOT objects and collections
virtual TProcessID *GetLastProcessID(TRefTable *reftable) const;
Expand Down
4 changes: 2 additions & 2 deletions io/io/inc/TBufferJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class TBufferJSON : public TBuffer {
virtual void ReadDouble(Double_t &d);
virtual void ReadCharP(Char_t *c);
virtual void ReadTString(TString &s);
virtual void ReadStdString(std::string &s);
virtual void ReadStdString(std::string *s);

virtual void WriteBool(Bool_t b);
virtual void WriteChar(Char_t c);
Expand All @@ -218,7 +218,7 @@ class TBufferJSON : public TBuffer {
virtual void WriteDouble(Double_t d);
virtual void WriteCharP(const Char_t *c);
virtual void WriteTString(const TString &s);
virtual void WriteStdString(const std::string &s);
virtual void WriteStdString(const std::string *s);

virtual Int_t WriteClones(TClonesArray *a, Int_t nobjects);

Expand Down
16 changes: 11 additions & 5 deletions io/io/src/TBufferFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,11 @@ void TBufferFile::WriteTString(const TString &s)
////////////////////////////////////////////////////////////////////////////////
/// Read std::string from TBuffer.

void TBufferFile::ReadStdString(std::string &s)
void TBufferFile::ReadStdString(std::string *obj)
{
std::string *obj = &s;
if (obj == 0) {
Error("TBufferFile::ReadStdString","The std::string address is nullptr but should not");
}
Int_t nbig;
UChar_t nwh;
*this >> nwh;
Expand All @@ -323,10 +325,14 @@ void TBufferFile::ReadStdString(std::string &s)
////////////////////////////////////////////////////////////////////////////////
/// Write std::string to TBuffer.

void TBufferFile::WriteStdString(const std::string &s)
void TBufferFile::WriteStdString(const std::string *obj)
{
if (s==0) return;
const std::string *obj = &s;
if (obj==0) {
*this << (UChar_t)0;
WriteFastArray("",0);
return;
}

UChar_t nwh;
Int_t nbig = obj->length();
if (nbig > 254) {
Expand Down
8 changes: 5 additions & 3 deletions io/io/src/TBufferJSON.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2658,7 +2658,7 @@ void TBufferJSON::ReadTString(TString & /*s*/)
////////////////////////////////////////////////////////////////////////////////
/// Reads a std::string

void TBufferJSON::ReadStdString(std::string &/*s*/)
void TBufferJSON::ReadStdString(std::string */*s*/)
{
}

Expand Down Expand Up @@ -2815,11 +2815,13 @@ void TBufferJSON::WriteTString(const TString &s)
////////////////////////////////////////////////////////////////////////////////
/// Writes a std::string

void TBufferJSON::WriteStdString(const std::string &s)
void TBufferJSON::WriteStdString(const std::string *s)
{
TJSONPushValue();

JsonWriteConstChar(s.c_str(), s.length());
if (s) JsonWriteConstChar(s->c_str(), s->length());
else JsonWriteConstChar("",0);

}

////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions io/sql/inc/TBufferSQL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ friend class TSQLStructure;
virtual void ReadDouble(Double_t &d);
virtual void ReadCharP(Char_t *c);
virtual void ReadTString(TString &s);
virtual void ReadStdString(std::string &s);
virtual void ReadStdString(std::string *s);

virtual void WriteBool(Bool_t b);
virtual void WriteChar(Char_t c);
Expand All @@ -295,7 +295,7 @@ friend class TSQLStructure;
virtual void WriteDouble(Double_t d);
virtual void WriteCharP(const Char_t *c);
virtual void WriteTString(const TString &s);
virtual void WriteStdString(const std::string &s);
virtual void WriteStdString(const std::string *s);

virtual Int_t ApplySequence(const TStreamerInfoActions::TActionSequence &sequence, void *object);
virtual Int_t ApplySequenceVecPtr(const TStreamerInfoActions::TActionSequence &sequence, void *start_collection, void *end_collection);
Expand Down
4 changes: 2 additions & 2 deletions io/sql/src/TBufferSQL2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2239,15 +2239,15 @@ void TBufferSQL2::WriteTString(const TString &s)
////////////////////////////////////////////////////////////////////////////////
/// Read a std::string

void TBufferSQL2::ReadStdString(std::string &s)
void TBufferSQL2::ReadStdString(std::string *s)
{
TBufferFile::ReadStdString(s);
}

////////////////////////////////////////////////////////////////////////////////
/// Write a std::string

void TBufferSQL2::WriteStdString(const std::string &s)
void TBufferSQL2::WriteStdString(const std::string *s)
{
TBufferFile::WriteStdString(s);
}
Expand Down
4 changes: 2 additions & 2 deletions io/xml/inc/TBufferXML.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ friend class TKeyXML;
virtual void ReadDouble(Double_t &d);
virtual void ReadCharP(Char_t *c);
virtual void ReadTString(TString &s);
virtual void ReadStdString(std::string &s);
virtual void ReadStdString(std::string *s);

virtual void WriteBool(Bool_t b);
virtual void WriteChar(Char_t c);
Expand All @@ -222,7 +222,7 @@ friend class TKeyXML;
virtual void WriteDouble(Double_t d);
virtual void WriteCharP(const Char_t *c);
virtual void WriteTString(const TString &s);
virtual void WriteStdString(const std::string &s);
virtual void WriteStdString(const std::string *s);

virtual Int_t ApplySequence(const TStreamerInfoActions::TActionSequence &sequence, void *object);
virtual Int_t ApplySequenceVecPtr(const TStreamerInfoActions::TActionSequence &sequence, void *start_collection, void *end_collection);
Expand Down
9 changes: 5 additions & 4 deletions io/xml/src/TBufferXML.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2724,15 +2724,15 @@ void TBufferXML::ReadTString(TString &s)
////////////////////////////////////////////////////////////////////////////////
/// Reads a std::string

void TBufferXML::ReadStdString(std::string &s)
void TBufferXML::ReadStdString(std::string *s)
{
if (GetIOVersion()<3) {
TBufferFile::ReadStdString(s);
} else {
BeforeIOoperation();
const char* buf;
if ((buf = XmlReadValue(xmlio::String)))
s = buf;
if (s) *s = buf;
}
}

Expand Down Expand Up @@ -2873,13 +2873,14 @@ void TBufferXML::WriteTString(const TString &s)
////////////////////////////////////////////////////////////////////////////////
/// Writes a TString

void TBufferXML::WriteStdString(const std::string &s)
void TBufferXML::WriteStdString(const std::string *s)
{
if (GetIOVersion()<3) {
TBufferFile::WriteStdString(s);
} else {
BeforeIOoperation();
XmlWriteValue(s.c_str(), xmlio::String);
if (s) XmlWriteValue(s->c_str(), xmlio::String);
else XmlWriteValue("", xmlio::String);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tree/tree/inc/TBufferSQL.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TBufferSQL : public TBufferFile {
virtual void ReadDouble(Double_t &d);
virtual void ReadCharP(Char_t *c);
virtual void ReadTString(TString &s);
virtual void ReadStdString(std::string &s);
virtual void ReadStdString(std::string *s);

virtual void WriteBool(Bool_t b);
virtual void WriteChar(Char_t c);
Expand All @@ -85,7 +85,7 @@ class TBufferSQL : public TBufferFile {
virtual void WriteDouble(Double_t d);
virtual void WriteCharP(const Char_t *c);
virtual void WriteTString(const TString &s);
virtual void WriteStdString(const std::string &s);
virtual void WriteStdString(const std::string *s);


virtual void WriteFastArray(const Bool_t *b, Int_t n);
Expand Down
4 changes: 2 additions & 2 deletions tree/tree/src/TBufferSQL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ void TBufferSQL::WriteTString(const TString &s)
////////////////////////////////////////////////////////////////////////////////
/// Read a std::string

void TBufferSQL::ReadStdString(std::string &s)
void TBufferSQL::ReadStdString(std::string *s)
{
TBufferFile::ReadStdString(s);
}

////////////////////////////////////////////////////////////////////////////////
/// Write a std::string

void TBufferSQL::WriteStdString(const std::string &s)
void TBufferSQL::WriteStdString(const std::string *s)
{
TBufferFile::WriteStdString(s);
}
Expand Down

0 comments on commit c2c35e8

Please sign in to comment.