43G4Mutex g_stream_finalization_mutex;
50template <
typename DestinationPolicy,
typename DefaultPolicy>
51class G4strstreambuf :
public std::basic_streambuf<char>
57 buffer =
new char[size + 1];
60 ~G4strstreambuf()
override
65 G4strstreambuf(
const G4strstreambuf&) =
delete;
66 G4strstreambuf&
operator=(
const G4strstreambuf&) =
delete;
71 if (count >= size) result = sync();
73 buffer[count] = (char)c;
83 return ReceiveString();
87 virtual G4int underflow() {
return 0; }
90 void SetDestination(G4coutDestination* dest) { destination = dest; }
92 inline G4int ReceiveString()
94 G4String stringToSend(buffer);
95 if (destination !=
nullptr) {
96 return DestinationPolicy::PostMessage(destination, stringToSend);
98 return DefaultPolicy::PostMessage(stringToSend);
102 char* buffer =
nullptr;
105 G4coutDestination* destination =
nullptr;
111 static inline G4int PostMessage(G4coutDestination* d,
const G4String& s)
119 static inline G4int PostMessage(G4coutDestination* d,
const G4String& s)
127 static inline G4int PostMessage(G4coutDestination* d,
const G4String& s)
135 static inline G4int PostMessage(
const G4String& s)
137 std::cout << s << std::flush;
144 static inline G4int PostMessage(
const G4String& s)
146 std::cerr << s << std::flush;
151using G4debugstreambuf = G4strstreambuf<PostToG4debug, DefaultToCout>;
152using G4coutstreambuf = G4strstreambuf<PostToG4cout, DefaultToCout>;
153using G4cerrstreambuf = G4strstreambuf<PostToG4cerr, DefaultToCerr>;
156#ifdef G4MULTITHREADED
158G4debugstreambuf*& _G4debugbuf_p()
164G4coutstreambuf*& _G4coutbuf_p()
170G4cerrstreambuf*& _G4cerrbuf_p()
177std::ostream*& _G4debug_p()
183std::ostream*& _G4cout_p()
189std::ostream*& _G4cerr_p()
198 if (_G4debugbuf_p() ==
nullptr) {
199 _G4debugbuf_p() =
new G4debugstreambuf;
201 if (_G4coutbuf_p() ==
nullptr) {
202 _G4coutbuf_p() =
new G4coutstreambuf;
204 if (_G4cerrbuf_p() ==
nullptr) {
205 _G4cerrbuf_p() =
new G4cerrstreambuf;
209 if (_G4debug_p() == &std::cout || _G4debug_p() ==
nullptr) {
210 _G4debug_p() =
new std::ostream(_G4debugbuf_p());
212 if (_G4cout_p() == &std::cout || _G4cout_p() ==
nullptr) {
213 _G4cout_p() =
new std::ostream(_G4coutbuf_p());
215 if (_G4cerr_p() == &std::cerr || _G4cerr_p() ==
nullptr) {
216 _G4cerr_p() =
new std::ostream(_G4cerrbuf_p());
222 G4AutoLock lock_(&g_stream_finalization_mutex);
225 _G4debug_p()->flush();
226 _G4cout_p()->flush();
227 _G4cerr_p()->flush();
231 _G4debug_p() = &std::cout;
233 _G4cout_p() = &std::cout;
235 _G4cerr_p() = &std::cerr;
238 delete _G4debugbuf_p();
239 _G4debugbuf_p() =
nullptr;
240 delete _G4coutbuf_p();
241 _G4coutbuf_p() =
nullptr;
242 delete _G4cerrbuf_p();
243 _G4cerrbuf_p() =
nullptr;
246# define G4debugbuf (*_G4debugbuf_p())
247# define G4coutbuf (*_G4coutbuf_p())
248# define G4cerrbuf (*_G4cerrbuf_p())
273struct RAII_G4iosSystem {
277 ~RAII_G4iosSystem() {
284extern "C" RAII_G4iosSystem RAII_G4iosSystemObj;
G4TemplateAutoLock< G4Mutex > G4AutoLock
G4PVDivision & operator=(const G4PVDivision &)=delete
G4debugstreambuf G4debugbuf
void G4iosSetDestination(G4coutDestination *sink)
G4coutstreambuf G4coutbuf
G4cerrstreambuf G4cerrbuf
void G4iosInitialization()
G4GLOB_DLL std::ostream G4debug
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
void G4iosInitialization()
G4int ReceiveG4cout_(const G4String &msg)
G4int ReceiveG4debug_(const G4String &msg)
G4int ReceiveG4cerr_(const G4String &msg)
#define G4ThreadLocalStatic