https://mooseframework.inl.gov
ConsoleStream.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
14 
15 // C++ includes
16 #include <iostream>
17 #include <sstream>
18 #include <memory>
19 #include <mutex>
20 
21 // this is the type of s t d :: c o u t
22 typedef std::basic_ostream<char, std::char_traits<char>> CoutType;
23 
24 // this is the function signature of std::endl
25 typedef CoutType & (*StandardEndLine)(CoutType &);
26 
31 {
32 public:
41  ConsoleStream(OutputWarehouse & output_warehouse);
42 
50  template <typename StreamType>
51  const ConsoleStream & operator<<(const StreamType & s) const;
52 
56  const ConsoleStream & operator<<(const StandardEndLine & manip) const;
57 
61  void unsetf(std::ios_base::fmtflags mask) const;
62 
63  std::streampos tellp() const { return _oss->tellp(); }
64 
68  std::streamsize precision() const;
69 
73  std::streamsize precision(std::streamsize new_precision) const;
74 
78  std::ios_base::fmtflags flags() const;
79 
83  std::ios_base::fmtflags flags(std::ios_base::fmtflags new_flags) const;
84 
88  unsigned long long int numPrinted() const;
89 
90 private:
93 
98  mutable std::shared_ptr<std::ostringstream> _oss;
99 
101  static std::mutex _stream_mutex;
102 };
103 
104 template <typename StreamType>
105 const ConsoleStream &
106 ConsoleStream::operator<<(const StreamType & s) const
107 {
108  std::lock_guard<std::mutex> lock(_stream_mutex);
109 
110  (*_oss) << s;
111  return *this;
112 }
A helper class for re-directing output streams to Console output objects form MooseObjects.
Definition: ConsoleStream.h:30
std::ios_base::fmtflags flags() const
Return the current flags.
Definition: ConsoleStream.C:56
CoutType &(* StandardEndLine)(CoutType &)
Definition: ConsoleStream.h:25
OutputWarehouse & _output_warehouse
Reference to the OutputWarhouse that contains the Console output objects.
Definition: ConsoleStream.h:92
void unsetf(std::ios_base::fmtflags mask) const
Unset format flags.
Definition: ConsoleStream.C:38
std::basic_ostream< char, std::char_traits< char > > CoutType
Definition: ConsoleStream.h:13
std::streampos tellp() const
Definition: ConsoleStream.h:63
unsigned long long int numPrinted() const
The number of times something has been printed.
Definition: ConsoleStream.C:68
std::shared_ptr< std::ostringstream > _oss
The stream for buffering the message This stupidly has to be a shared pointer because of something in...
Definition: ConsoleStream.h:98
Class for storing and utilizing output objects.
std::streamsize precision() const
Return the current precision.
Definition: ConsoleStream.C:44
static std::mutex _stream_mutex
Mutex to prevent concurrent read/writes, write/writes.
const ConsoleStream & operator<<(const StreamType &s) const
The output stream operator.
ConsoleStream(OutputWarehouse &output_warehouse)
Constructor.
Definition: ConsoleStream.C:17