Line data Source code
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 13 : class OutputWarehouse; 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 : 27 : /** 28 : * A helper class for re-directing output streams to Console output objects form MooseObjects 29 : */ 30 : class ConsoleStream 31 : { 32 : public: 33 : /** 34 : * Constructor 35 : * @param output_warehouse A reference to the OutputWarehouse containing the Console outputs 36 : * 37 : * ConsoleStreamInterface contains an instance of this object, which allows message streams to be 38 : * transferred to Console output objects. This class simply provides an operator<< method 39 : * that passes the stream to the Console objects. 40 : */ 41 : ConsoleStream(OutputWarehouse & output_warehouse); 42 : 43 : /** 44 : * The output stream operator 45 : * @param s The data to be output to the Console objects 46 : * 47 : * This allows any object to uses _console to write to the Console: 48 : * _console << "The combination to the air lock is " << 12345 << std::endl; 49 : */ 50 : template <typename StreamType> 51 : const ConsoleStream & operator<<(const StreamType & s) const; 52 : 53 : /** 54 : * This overload is here to handle the the std::endl manipulator 55 : */ 56 : const ConsoleStream & operator<<(const StandardEndLine & manip) const; 57 : 58 : /** 59 : * Unset format flags 60 : */ 61 : void unsetf(std::ios_base::fmtflags mask) const; 62 : 63 : std::streampos tellp() const { return _oss->tellp(); } 64 : 65 : /** 66 : * Return the current precision 67 : */ 68 : std::streamsize precision() const; 69 : 70 : /** 71 : * Set the precision and return the old precision 72 : */ 73 : std::streamsize precision(std::streamsize new_precision) const; 74 : 75 : /** 76 : * Return the current flags 77 : */ 78 : std::ios_base::fmtflags flags() const; 79 : 80 : /** 81 : * Set the flags and return the old flags 82 : */ 83 : std::ios_base::fmtflags flags(std::ios_base::fmtflags new_flags) const; 84 : 85 : /** 86 : * The number of times something has been printed 87 : */ 88 : unsigned long long int numPrinted() const; 89 : 90 : private: 91 : /// Reference to the OutputWarhouse that contains the Console output objects 92 : OutputWarehouse & _output_warehouse; 93 : 94 : /// The stream for buffering the message 95 : /// This stupidly has to be a shared pointer because 96 : /// of something in AutomaticMortarGeneration that requires 97 : /// this to be trivially copyable. 98 : mutable std::shared_ptr<std::ostringstream> _oss; 99 : 100 : /// Mutex to prevent concurrent read/writes, write/writes 101 : static std::mutex _stream_mutex; 102 : }; 103 : 104 : template <typename StreamType> 105 : const ConsoleStream & 106 92661726 : ConsoleStream::operator<<(const StreamType & s) const 107 : { 108 92661726 : std::lock_guard<std::mutex> lock(_stream_mutex); 109 : 110 92661726 : (*_oss) << s; 111 92661726 : return *this; 112 92661726 : }