Skip to content
Snippets Groups Projects
Commit 5aa783f1 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

STYLE: simplify stream types

- reduce coding clutter, avoiding allocated pointers when possible.
  IFstream and OFstream continue to use pointers since they handle
  compressed files, other streams can do without them.
parent 204ce366
Branches
Tags
1 merge request!156low-level binary Ostream output, additional stream classes
...@@ -56,8 +56,11 @@ protected: ...@@ -56,8 +56,11 @@ protected:
// Member Data // Member Data
//- The allocated stream pointer. //- The stream type
StreamType* allocatedPtr_; typedef StreamType stream_type;
//- The input/output stream.
stream_type stream_;
// Constructors // Constructors
...@@ -65,56 +68,36 @@ protected: ...@@ -65,56 +68,36 @@ protected:
//- Construct null //- Construct null
StringStreamAllocator() StringStreamAllocator()
: :
allocatedPtr_(new StreamType()) stream_()
{} {}
//- Construct from pointer, taking ownership
StringStreamAllocator(StreamType* ptr)
:
allocatedPtr_(ptr)
{}
//- Construct from string //- Copy construct from string
StringStreamAllocator(const std::string& buffer) StringStreamAllocator(const std::string& buffer)
: :
allocatedPtr_(new StreamType(buffer)) stream_(buffer)
{} {}
//- Destructor //- Destructor
~StringStreamAllocator() ~StringStreamAllocator()
{ {}
deallocate();
}
// Protected Member Functions
//- Delete the stream pointer
void deallocate()
{
if (allocatedPtr_)
{
delete allocatedPtr_;
allocatedPtr_ = nullptr;
}
}
public: public:
// Public Member Functions // Public Member Functions
//- Get the string //- Get the string - as Foam::string rather than std::string
Foam::string str() const Foam::string str() const
{ {
return allocatedPtr_->str(); return Foam::string(stream_.str());
} }
//- Set the string //- Set the string
void str(const std::string& s) void str(const std::string& s)
{ {
allocatedPtr_->str(s); stream_.str(s);
} }
}; };
...@@ -143,7 +126,7 @@ public: ...@@ -143,7 +126,7 @@ public:
) )
: :
StringStreamAllocator<std::istringstream>(buffer), StringStreamAllocator<std::istringstream>(buffer),
ISstream(*allocatedPtr_, name, format, version) ISstream(stream_, name, format, version)
{} {}
...@@ -157,7 +140,15 @@ public: ...@@ -157,7 +140,15 @@ public:
) )
: :
StringStreamAllocator<std::istringstream>(buffer), StringStreamAllocator<std::istringstream>(buffer),
ISstream(*allocatedPtr_, name, format, version) ISstream(stream_, name, format, version)
{}
//- Construct as copy of content
IStringStream(const IStringStream& str)
:
StringStreamAllocator<std::istringstream>(str.str()),
ISstream(stream_, str.name(), str.format(), str.version())
{} {}
...@@ -182,8 +173,7 @@ public: ...@@ -182,8 +173,7 @@ public:
// Member operators // Member operators
//- Return a non-const reference to const Istream //- Return a non-const reference to const Istream
// Needed for read-constructors where the stream argument is temporary: // Needed for read-constructors where the stream argument is temporary.
// e.g. thing thisThing(IFstream("thingFileName")());
Istream& operator()() const Istream& operator()() const
{ {
return const_cast<IStringStream&>(*this); return const_cast<IStringStream&>(*this);
...@@ -214,14 +204,15 @@ public: ...@@ -214,14 +204,15 @@ public:
) )
: :
StringStreamAllocator<std::ostringstream>(), StringStreamAllocator<std::ostringstream>(),
OSstream(*allocatedPtr_, "output", format, version) OSstream(stream_, "output", format, version)
{} {}
//- Construct as copy
OStringStream(const OStringStream& oss) //- Construct as copy of content
OStringStream(const OStringStream& str)
: :
StringStreamAllocator<std::ostringstream>(oss.str()), StringStreamAllocator<std::ostringstream>(str.str()),
OSstream(*allocatedPtr_, oss.name(), oss.format(), oss.version()) OSstream(stream_, str.name(), str.format(), str.version())
{} {}
...@@ -243,7 +234,7 @@ public: ...@@ -243,7 +234,7 @@ public:
void rewind() void rewind()
{ {
// pubseekpos() instead of seekp() for symmetry with other classes // pubseekpos() instead of seekp() for symmetry with other classes
allocatedPtr_->rdbuf()->pubseekpos(0, std::ios_base::out); stream_.rdbuf()->pubseekpos(0, std::ios_base::out);
} }
//- Print description to Ostream //- Print description to Ostream
......
...@@ -47,55 +47,57 @@ class osha1stream; ...@@ -47,55 +47,57 @@ class osha1stream;
class OSHA1stream; class OSHA1stream;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class sha1streambuf Declaration Class osha1stream Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
//- A streambuf class for calculating SHA1 digests //- A basic output stream for calculating SHA1 digests
class sha1streambuf class osha1stream
: :
public std::streambuf virtual public std::ios,
public std::ostream
{ {
// Private data //- A streambuf class for calculating SHA1 digests
class sha1buf
:
public std::streambuf
{
//- This does all the work and has its own buffering
SHA1 sha1_;
//- This does all the work and has its own buffering protected:
SHA1 sha1_; // Protected members
friend class osha1stream; //- Put sequence of characters
virtual std::streamsize xsputn(const char* str, std::streamsize n)
{
sha1_.append(str, n);
return n;
}
public:
// Constructors public:
//- Construct null // Constructors
sha1streambuf()
{}
// Member Functions //- Construct null
sha1buf()
{}
// Write
//- Process unbuffered // Public member functions
virtual std::streamsize xsputn(const char* str, std::streamsize n)
{
sha1_.append(str, n);
return n;
}
};
//- Full access to the sha1
inline SHA1& sha1()
{
return sha1_;
}
};
/*---------------------------------------------------------------------------*\
Class osha1stream Declaration
\*---------------------------------------------------------------------------*/
//- A basic output stream for calculating SHA1 digests
class osha1stream
:
virtual public std::ios,
public std::ostream
{
// Private data // Private data
sha1streambuf sbuf_; //- Reference to the underlying buffer
sha1buf buf_;
public: public:
...@@ -104,23 +106,24 @@ public: ...@@ -104,23 +106,24 @@ public:
//- Construct null //- Construct null
osha1stream() osha1stream()
: :
std::ostream(&sbuf_) std::ostream(&buf_)
{} {}
// Member Functions // Member Functions
// Access // Access
//- This hides both signatures of std::basic_ios::rdbuf() //- This hides both signatures of std::basic_ios::rdbuf()
sha1streambuf* rdbuf() sha1buf* rdbuf()
{ {
return &sbuf_; return &buf_;
} }
//- Full access to the sha1 //- Full access to the sha1
SHA1& sha1() SHA1& sha1()
{ {
return sbuf_.sha1_; return buf_.sha1();
} }
}; };
...@@ -135,10 +138,12 @@ class OSHA1streamAllocator ...@@ -135,10 +138,12 @@ class OSHA1streamAllocator
{ {
protected: protected:
// Member data // Protected data
typedef osha1stream stream_type;
//- The allocated stream pointer //- The output stream
osha1stream* allocatedPtr_; stream_type stream_;
// Constructors // Constructors
...@@ -146,36 +151,37 @@ protected: ...@@ -146,36 +151,37 @@ protected:
//- Construct null //- Construct null
OSHA1streamAllocator() OSHA1streamAllocator()
: :
allocatedPtr_(new osha1stream()) stream_()
{} {}
//- Destructor //- Destructor
~OSHA1streamAllocator() ~OSHA1streamAllocator()
{ {}
deallocate();
}
public:
// Member Functions // Member Functions
//- Delete the stream pointer //- Full access to the sha1
void deallocate() SHA1& sha1()
{ {
if (allocatedPtr_) return stream_.sha1();
{
delete allocatedPtr_;
allocatedPtr_ = nullptr;
}
} }
public: //- Return SHA1::Digest for the data processed until now
SHA1Digest digest()
{
return stream_.sha1().digest();
}
//- Full access to the sha1
SHA1& sha1() //- Clear the SHA1 calculation
void reset()
{ {
return allocatedPtr_->sha1(); return stream_.sha1().clear();
} }
}; };
...@@ -191,6 +197,7 @@ class OSHA1stream ...@@ -191,6 +197,7 @@ class OSHA1stream
public OSHA1streamAllocator, public OSHA1streamAllocator,
public OSstream public OSstream
{ {
typedef OSHA1streamAllocator allocator_type;
// Private Member Functions // Private Member Functions
...@@ -211,8 +218,8 @@ public: ...@@ -211,8 +218,8 @@ public:
versionNumber version=currentVersion versionNumber version=currentVersion
) )
: :
OSHA1streamAllocator(), allocator_type(),
OSstream(*allocatedPtr_, "OSHA1stream", format, version) OSstream(stream_, "sha1", format, version)
{} {}
...@@ -223,23 +230,6 @@ public: ...@@ -223,23 +230,6 @@ public:
// Member functions // Member functions
// Access
//- Return SHA1::Digest for the data processed until now
SHA1Digest digest()
{
return sha1().digest();
}
// Edit
//- Clear the SHA1 calculation
void reset()
{
sha1().clear();
}
//- Clear the SHA1 calculation //- Clear the SHA1 calculation
// \deprecated use reset instead (deprecated Jul 2017) // \deprecated use reset instead (deprecated Jul 2017)
void rewind() void rewind()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment