mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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.
This commit is contained in:
@ -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
|
||||
|
||||
@ -46,43 +46,6 @@ namespace Foam
|
||||
class osha1stream;
|
||||
class OSHA1stream;
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class sha1streambuf Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
//- A streambuf class for calculating SHA1 digests
|
||||
class sha1streambuf
|
||||
:
|
||||
public std::streambuf
|
||||
{
|
||||
// Private data
|
||||
|
||||
//- This does all the work and has its own buffering
|
||||
SHA1 sha1_;
|
||||
|
||||
friend class osha1stream;
|
||||
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct null
|
||||
sha1streambuf()
|
||||
{}
|
||||
|
||||
// Member Functions
|
||||
|
||||
// Write
|
||||
|
||||
//- Process unbuffered
|
||||
virtual std::streamsize xsputn(const char* str, std::streamsize n)
|
||||
{
|
||||
sha1_.append(str, n);
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class osha1stream Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
@ -93,9 +56,48 @@ class osha1stream
|
||||
virtual public std::ios,
|
||||
public std::ostream
|
||||
{
|
||||
//- A streambuf class for calculating SHA1 digests
|
||||
class sha1buf
|
||||
:
|
||||
public std::streambuf
|
||||
{
|
||||
//- This does all the work and has its own buffering
|
||||
SHA1 sha1_;
|
||||
|
||||
protected:
|
||||
// Protected members
|
||||
|
||||
//- Put sequence of characters
|
||||
virtual std::streamsize xsputn(const char* str, std::streamsize n)
|
||||
{
|
||||
sha1_.append(str, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct null
|
||||
sha1buf()
|
||||
{}
|
||||
|
||||
|
||||
// Public member functions
|
||||
|
||||
//- Full access to the sha1
|
||||
inline SHA1& sha1()
|
||||
{
|
||||
return sha1_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 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
|
||||
|
||||
//- The allocated stream pointer
|
||||
osha1stream* allocatedPtr_;
|
||||
typedef osha1stream stream_type;
|
||||
|
||||
//- The output stream
|
||||
stream_type stream_;
|
||||
|
||||
|
||||
// Constructors
|
||||
@ -146,36 +151,37 @@ protected:
|
||||
//- Construct null
|
||||
OSHA1streamAllocator()
|
||||
:
|
||||
allocatedPtr_(new osha1stream())
|
||||
stream_()
|
||||
{}
|
||||
|
||||
|
||||
//- Destructor
|
||||
~OSHA1streamAllocator()
|
||||
{
|
||||
deallocate();
|
||||
}
|
||||
|
||||
|
||||
// Member Functions
|
||||
|
||||
//- Delete the stream pointer
|
||||
void deallocate()
|
||||
{
|
||||
if (allocatedPtr_)
|
||||
{
|
||||
delete allocatedPtr_;
|
||||
allocatedPtr_ = nullptr;
|
||||
}
|
||||
}
|
||||
{}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// Member Functions
|
||||
|
||||
//- Full access to the sha1
|
||||
SHA1& sha1()
|
||||
{
|
||||
return allocatedPtr_->sha1();
|
||||
return stream_.sha1();
|
||||
}
|
||||
|
||||
|
||||
//- Return SHA1::Digest for the data processed until now
|
||||
SHA1Digest digest()
|
||||
{
|
||||
return stream_.sha1().digest();
|
||||
}
|
||||
|
||||
|
||||
//- Clear the SHA1 calculation
|
||||
void reset()
|
||||
{
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user