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:
// Member Data
//- The allocated stream pointer.
StreamType* allocatedPtr_;
//- The stream type
typedef StreamType stream_type;
//- The input/output stream.
stream_type stream_;
// Constructors
......@@ -65,56 +68,36 @@ protected:
//- Construct null
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)
:
allocatedPtr_(new StreamType(buffer))
stream_(buffer)
{}
//- Destructor
~StringStreamAllocator()
{
deallocate();
}
// Protected Member Functions
//- Delete the stream pointer
void deallocate()
{
if (allocatedPtr_)
{
delete allocatedPtr_;
allocatedPtr_ = nullptr;
}
}
{}
public:
// Public Member Functions
//- Get the string
//- Get the string - as Foam::string rather than std::string
Foam::string str() const
{
return allocatedPtr_->str();
return Foam::string(stream_.str());
}
//- Set the string
void str(const std::string& s)
{
allocatedPtr_->str(s);
stream_.str(s);
}
};
......@@ -143,7 +126,7 @@ public:
)
:
StringStreamAllocator<std::istringstream>(buffer),
ISstream(*allocatedPtr_, name, format, version)
ISstream(stream_, name, format, version)
{}
......@@ -157,7 +140,15 @@ public:
)
:
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:
// Member operators
//- Return a non-const reference to const Istream
// Needed for read-constructors where the stream argument is temporary:
// e.g. thing thisThing(IFstream("thingFileName")());
// Needed for read-constructors where the stream argument is temporary.
Istream& operator()() const
{
return const_cast<IStringStream&>(*this);
......@@ -214,14 +204,15 @@ public:
)
:
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()),
OSstream(*allocatedPtr_, oss.name(), oss.format(), oss.version())
StringStreamAllocator<std::ostringstream>(str.str()),
OSstream(stream_, str.name(), str.format(), str.version())
{}
......@@ -243,7 +234,7 @@ public:
void rewind()
{
// 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
......
......@@ -47,55 +47,57 @@ class osha1stream;
class OSHA1stream;
/*---------------------------------------------------------------------------*\
Class sha1streambuf Declaration
Class osha1stream Declaration
\*---------------------------------------------------------------------------*/
//- A streambuf class for calculating SHA1 digests
class sha1streambuf
//- A basic output stream for calculating SHA1 digests
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
SHA1 sha1_;
protected:
// 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
sha1streambuf()
{}
// Constructors
// Member Functions
//- Construct null
sha1buf()
{}
// Write
//- Process unbuffered
virtual std::streamsize xsputn(const char* str, std::streamsize n)
{
sha1_.append(str, n);
return n;
}
};
// Public member functions
//- 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
sha1streambuf sbuf_;
//- Reference to the underlying buffer
sha1buf buf_;
public:
......@@ -104,23 +106,24 @@ public:
//- Construct null
osha1stream()
:
std::ostream(&sbuf_)
std::ostream(&buf_)
{}
// Member Functions
// Access
//- This hides both signatures of std::basic_ios::rdbuf()
sha1streambuf* rdbuf()
sha1buf* rdbuf()
{
return &sbuf_;
return &buf_;
}
//- Full access to the sha1
SHA1& sha1()
{
return sbuf_.sha1_;
return buf_.sha1();
}
};
......@@ -135,10 +138,12 @@ class OSHA1streamAllocator
{
protected:
// Member data
// Protected data
typedef osha1stream stream_type;
//- The allocated stream pointer
osha1stream* allocatedPtr_;
//- The output stream
stream_type stream_;
// Constructors
......@@ -146,36 +151,37 @@ protected:
//- Construct null
OSHA1streamAllocator()
:
allocatedPtr_(new osha1stream())
stream_()
{}
//- Destructor
~OSHA1streamAllocator()
{
deallocate();
}
{}
public:
// Member Functions
//- Delete the stream pointer
void deallocate()
//- Full access to the sha1
SHA1& sha1()
{
if (allocatedPtr_)
{
delete allocatedPtr_;
allocatedPtr_ = nullptr;
}
return stream_.sha1();
}
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
public OSHA1streamAllocator,
public OSstream
{
typedef OSHA1streamAllocator allocator_type;
// Private Member Functions
......@@ -211,8 +218,8 @@ public:
versionNumber version=currentVersion
)
:
OSHA1streamAllocator(),
OSstream(*allocatedPtr_, "OSHA1stream", format, version)
allocator_type(),
OSstream(stream_, "sha1", format, version)
{}
......@@ -223,23 +230,6 @@ public:
// 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
// \deprecated use reset instead (deprecated Jul 2017)
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