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

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

#include <FileInputStream.h>

Inheritance diagram for FileInputStream:
[legend]

Public Member Functions

 FileInputStream (const std::string &filename)
 
virtual std::shared_ptr< std::istream > get () const override final
 Gets an input stream to the underlying stream. More...
 
virtual std::optional< std::filesystem::path > getFilename () const override final
 Gets the underlying filename, if any. More...
 
bool inUse () const
 Whether or not anything is still using this stream. 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...
 

Protected Attributes

const std::filesystem::path _filename
 The name of the file. More...
 

Detailed Description

Helper class that hands out input streams to a file.

Definition at line 17 of file FileInputStream.h.

Constructor & Destructor Documentation

◆ FileInputStream()

FileInputStream::FileInputStream ( const std::string &  filename)

Definition at line 16 of file FileInputStream.C.

16  : InputStream(), _filename(filename)
17 {
18 }
Helper class that hands out input streams to an underlying, managed stream of arbitrary type...
Definition: InputStream.h:22
const std::filesystem::path _filename
The name of the file.

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 get(), and StringInputStream::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 > FileInputStream::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 21 of file FileInputStream.C.

22 {
23  std::shared_ptr<std::istream> stream = std::make_unique<std::ifstream>();
24  addSharedStream(stream);
25 
26  auto & in = *static_cast<std::ifstream *>(stream.get());
27  in.open(_filename.c_str(), std::ios::in | std::ios::binary);
28  if (in.fail())
29  mooseError("Unable to open file ", std::filesystem::absolute(_filename));
30 
31  return stream;
32 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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
const std::filesystem::path _filename
The name of the file.

◆ getFilename()

std::optional< std::filesystem::path > FileInputStream::getFilename ( ) const
finaloverridevirtual

Gets the underlying filename, if any.

Reimplemented from InputStream.

Definition at line 35 of file FileInputStream.C.

36 {
37  return _filename;
38 }
const std::filesystem::path _filename
The name of the file.

◆ 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 StringInputStream::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

Member Data Documentation

◆ _filename

const std::filesystem::path FileInputStream::_filename
protected

The name of the file.

Definition at line 28 of file FileInputStream.h.

Referenced by get(), and getFilename().


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