ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ios.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4ios.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //
28 //
29 // --------------------------------------------------------------
30 // GEANT 4 class implementation file
31 //
32 // G4ios.cc
33 //
34 // History 1998 Nov. 3 Masayasu Nagamatu
35 
36 #include "G4ios.hh"
37 #include "G4strstreambuf.hh"
38 #include <iostream>
39 
40 #ifdef G4MULTITHREADED
41 
42 G4strstreambuf*& _G4coutbuf_p()
43 {
45  return _instance;
46 }
47 
48 G4strstreambuf*& _G4cerrbuf_p()
49 {
51  return _instance;
52 }
53 
54 std::ostream*& _G4cout_p()
55 {
56  G4ThreadLocalStatic std::ostream* _instance = new std::ostream(_G4coutbuf_p());
57  return _instance;
58 }
59 
60 std::ostream*& _G4cerr_p()
61 {
62  G4ThreadLocalStatic std::ostream* _instance = new std::ostream(_G4cerrbuf_p());
63  return _instance;
64 }
65 
66 #define G4coutbuf (*_G4coutbuf_p())
67 #define G4cerrbuf (*_G4cerrbuf_p())
68 #define G4cout (*_G4cout_p())
69 #define G4cerr (*_G4cerr_p())
70 
72 {
73  if (_G4coutbuf_p() == 0) _G4coutbuf_p() = new G4strstreambuf;
74  if (_G4cerrbuf_p() == 0) _G4cerrbuf_p() = new G4strstreambuf;
75  if (_G4cout_p() == &std::cout || _G4cout_p() == 0) _G4cout_p() = new std::ostream(_G4coutbuf_p());
76  if (_G4cerr_p() == &std::cerr || _G4cerr_p() == 0) _G4cerr_p() = new std::ostream(_G4cerrbuf_p());
77 }
78 
79 void G4iosFinalization()
80 {
81  delete _G4cout_p(); _G4cout_p() = &std::cout;
82  delete _G4cerr_p(); _G4cerr_p() = &std::cerr;
83  delete _G4coutbuf_p(); _G4coutbuf_p() = nullptr;
84  delete _G4cerrbuf_p(); _G4cerrbuf_p() = nullptr;
85 }
86 
87 // These two functions are guaranteed to be called at load and
88 // unload of the library containing this code.
89 namespace
90 {
91 #ifndef WIN32
92  void setupG4ioSystem(void) __attribute__ ((constructor));
93  void cleanupG4ioSystem(void) __attribute__((destructor));
94 #endif
95  void setupG4ioSystem(void) { G4iosInitialization(); }
96  void cleanupG4ioSystem(void) { G4iosFinalization(); }
97 }
98 
99 #else // Sequential
100 
103 std::ostream G4cout(&G4coutbuf);
104 std::ostream G4cerr(&G4cerrbuf);
105 
108 
109 #endif