https://mooseframework.inl.gov
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
StringInputStream Class Reference

Helper class that hands out input streams to a stringstream. More...

#include <StringInputStream.h>

Inheritance diagram for StringInputStream:
[legend]

Public Member Functions

 StringInputStream (std::unique_ptr< std::stringstream > stream)
 
virtual std::shared_ptr< std::istream > get () const override final
 Gets an input stream to the underlying stream. More...
 
std::unique_ptr< std::stringstream > release ()
 Releases the owned stringstream. More...
 
bool inUse () const
 Whether or not anything is still using this stream. More...
 
virtual std::optional< std::filesystem::path > getFilename () const
 Gets the underlying filename, if any. More...
 

Protected Member Functions

void addSharedStream (const std::weak_ptr< std::istream > stream) const
 Internal method to be called by derived classes to add a shared stream to _shared_streams (so that it can be tracked). More...
 

Private Attributes

std::unique_ptr< std::stringstream > _stream
 The underlying stringstream. More...
 

Detailed Description

Helper class that hands out input streams to a stringstream.

Definition at line 19 of file StringInputStream.h.

Constructor & Destructor Documentation

◆ StringInputStream()

StringInputStream::StringInputStream ( std::unique_ptr< std::stringstream >  stream)

Definition at line 14 of file StringInputStream.C.

15  : InputStream(), _stream(std::move(stream))
16 {
17 }
std::unique_ptr< std::stringstream > _stream
The underlying stringstream.
Helper class that hands out input streams to an underlying, managed stream of arbitrary type...
Definition: InputStream.h:22

Member Function Documentation

◆ addSharedStream()

void InputStream::addSharedStream ( const std::weak_ptr< std::istream >  stream) const
protectedinherited

Internal method to be called by derived classes to add a shared stream to _shared_streams (so that it can be tracked).

Should be called in the overridden get() by derived classes.

Definition at line 21 of file InputStream.C.

Referenced by FileInputStream::get(), and get().

22 {
23  mooseAssert(!stream.expired(), "Stream is expired");
24 
25  for (auto & entry : _shared_streams)
26  if (entry.expired())
27  {
28  entry = stream;
29  return;
30  }
31  _shared_streams.push_back(stream);
32 }
std::vector< std::weak_ptr< std::istream > > _shared_streams
The streams that have been handed out by get()
Definition: InputStream.h:60

◆ get()

std::shared_ptr< std::istream > StringInputStream::get ( ) const
finaloverridevirtual

Gets an input stream to the underlying stream.

This is returned as a shared_ptr so that this class can tell whether or not another object is still using the stream.

Implements InputStream.

Definition at line 20 of file StringInputStream.C.

21 {
22  mooseAssert(_stream, "Not valid");
23 
24  std::shared_ptr<std::istream> stream = std::make_unique<std::istream>(_stream->rdbuf());
25  addSharedStream(stream);
26  return stream;
27 }
std::unique_ptr< std::stringstream > _stream
The underlying stringstream.
void addSharedStream(const std::weak_ptr< std::istream > stream) const
Internal method to be called by derived classes to add a shared stream to _shared_streams (so that it...
Definition: InputStream.C:21

◆ getFilename()

std::optional< std::filesystem::path > InputStream::getFilename ( ) const
virtualinherited

Gets the underlying filename, if any.

Reimplemented in FileInputStream.

Definition at line 44 of file InputStream.C.

Referenced by RestartableDataReader::deserializeValue(), and RestartableDataReader::readHeader().

45 {
46  return {};
47 }

◆ inUse()

bool InputStream::inUse ( ) const
inherited

Whether or not anything is still using this stream.

This is checked by seeing whether or not the use count of any of the streams handed out by get() is > 1 (with the only use count being the one stored in _shared_streams)

Definition at line 35 of file InputStream.C.

Referenced by release(), and InputStream::~InputStream().

36 {
37  for (auto & stream : _shared_streams)
38  if (!stream.expired())
39  return true;
40  return false;
41 }
std::vector< std::weak_ptr< std::istream > > _shared_streams
The streams that have been handed out by get()
Definition: InputStream.h:60

◆ release()

std::unique_ptr< std::stringstream > StringInputStream::release ( )

Releases the owned stringstream.

Will error if an object still has access to a stream obtained via get().

Leaves the owned stream in an undefined state.

Definition at line 30 of file StringInputStream.C.

31 {
32  if (inUse())
33  mooseError("StringInputStream::release(): Cannot release; still in use");
34  return std::move(_stream);
35 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
std::unique_ptr< std::stringstream > _stream
The underlying stringstream.
bool inUse() const
Whether or not anything is still using this stream.
Definition: InputStream.C:35

Member Data Documentation

◆ _stream

std::unique_ptr<std::stringstream> StringInputStream::_stream
private

The underlying stringstream.

Definition at line 37 of file StringInputStream.h.

Referenced by get(), and release().


The documentation for this class was generated from the following files: