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p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /global/management/src/G4ios.cc (Version 11.3.0) and /global/management/src/G4ios.cc (Version ReleaseNotes)


** Warning: Cannot open xref database.

  1 //                                                  1 
  2 // *******************************************    
  3 // * License and Disclaimer                       
  4 // *                                              
  5 // * The  Geant4 software  is  copyright of th    
  6 // * the Geant4 Collaboration.  It is provided    
  7 // * conditions of the Geant4 Software License    
  8 // * LICENSE and available at  http://cern.ch/    
  9 // * include a list of copyright holders.         
 10 // *                                              
 11 // * Neither the authors of this software syst    
 12 // * institutes,nor the agencies providing fin    
 13 // * work  make  any representation or  warran    
 14 // * regarding  this  software system or assum    
 15 // * use.  Please see the license in the file     
 16 // * for the full disclaimer and the limitatio    
 17 // *                                              
 18 // * This  code  implementation is the result     
 19 // * technical work of the GEANT4 collaboratio    
 20 // * By using,  copying,  modifying or  distri    
 21 // * any work based  on the software)  you  ag    
 22 // * use  in  resulting  scientific  publicati    
 23 // * acceptance of all terms of the Geant4 Sof    
 24 // *******************************************    
 25 //                                                
 26 // G4ios implementation                           
 27 //                                                
 28 // Authors: H.Yoshida, M.Nagamatu - November 1    
 29 // -------------------------------------------    
 30                                                   
 31 #include "G4ios.hh"                               
 32                                                   
 33 #include "G4coutDestination.hh"                   
 34                                                   
 35 #include <iostream>                               
 36                                                   
 37 namespace                                         
 38 {                                                 
 39 // Concrete streambuf redirecting output to G4    
 40 // Templated on two policy types to determine:    
 41 // - DestinationPolicy: which member member fu    
 42 // - DefaultPolicy: what to do if G4coutDestin    
 43 template <typename DestinationPolicy, typename    
 44 class G4strstreambuf : public std::basic_strea    
 45 {                                                 
 46  public:                                          
 47   G4strstreambuf()                                
 48   {                                               
 49     size = 4095;                                  
 50     buffer = new char[size + 1];                  
 51   }                                               
 52                                                   
 53   ~G4strstreambuf() override                      
 54   {                                               
 55     delete[] buffer;                              
 56   }                                               
 57                                                   
 58   G4strstreambuf(const G4strstreambuf&) = dele    
 59   G4strstreambuf& operator=(const G4strstreamb    
 60                                                   
 61   G4int overflow(G4int c = EOF) override          
 62   {                                               
 63     G4int result = 0;                             
 64     if (count >= size) result = sync();           
 65                                                   
 66     buffer[count] = (char)c;                      
 67     count++;                                      
 68                                                   
 69     return result;                                
 70   }                                               
 71                                                   
 72   G4int sync() override                           
 73   {                                               
 74     buffer[count] = '\0';                         
 75     count = 0;                                    
 76     return ReceiveString();                       
 77   }                                               
 78                                                   
 79 #ifdef WIN32                                      
 80   virtual G4int underflow() { return 0; }         
 81 #endif                                            
 82                                                   
 83   void SetDestination(G4coutDestination* dest)    
 84                                                   
 85   inline G4int ReceiveString()                    
 86   {                                               
 87     G4String stringToSend(buffer);                
 88     if (destination != nullptr) {                 
 89       return DestinationPolicy::PostMessage(de    
 90     }                                             
 91     return DefaultPolicy::PostMessage(stringTo    
 92   }                                               
 93                                                   
 94  private:                                         
 95   char* buffer = nullptr;                         
 96   G4int count = 0;                                
 97   G4int size = 0;                                 
 98   G4coutDestination* destination = nullptr;       
 99 };                                                
100                                                   
101 // Policies                                       
102 struct PostToG4debug                              
103 {                                                 
104   static inline G4int PostMessage(G4coutDestin    
105   {                                               
106     return d->ReceiveG4debug_(s);                 
107   }                                               
108 };                                                
109                                                   
110 struct PostToG4cout                               
111 {                                                 
112   static inline G4int PostMessage(G4coutDestin    
113   {                                               
114     return d->ReceiveG4cout_(s);                  
115   }                                               
116 };                                                
117                                                   
118 struct PostToG4cerr                               
119 {                                                 
120   static inline G4int PostMessage(G4coutDestin    
121   {                                               
122     return d->ReceiveG4cerr_(s);                  
123   }                                               
124 };                                                
125                                                   
126 struct DefaultToCout                              
127 {                                                 
128   static inline G4int PostMessage(const G4Stri    
129   {                                               
130     std::cout << s << std::flush;                 
131     return 0;                                     
132   }                                               
133 };                                                
134                                                   
135 struct DefaultToCerr                              
136 {                                                 
137   static inline G4int PostMessage(const G4Stri    
138   {                                               
139     std::cerr << s << std::flush;                 
140     return 0;                                     
141   }                                               
142 };                                                
143                                                   
144 using G4debugstreambuf = G4strstreambuf<PostTo    
145 using G4coutstreambuf = G4strstreambuf<PostToG    
146 using G4cerrstreambuf = G4strstreambuf<PostToG    
147 }  // namespace                                   
148                                                   
149 #ifdef G4MULTITHREADED                            
150 // --- StreamBuffers                              
151 G4debugstreambuf*& _G4debugbuf_p()                
152 {                                                 
153   G4ThreadLocalStatic auto* _instance = new G4    
154   return _instance;                               
155 }                                                 
156                                                   
157 G4coutstreambuf*& _G4coutbuf_p()                  
158 {                                                 
159   G4ThreadLocalStatic auto* _instance = new G4    
160   return _instance;                               
161 }                                                 
162                                                   
163 G4cerrstreambuf*& _G4cerrbuf_p()                  
164 {                                                 
165   G4ThreadLocalStatic auto* _instance = new G4    
166   return _instance;                               
167 }                                                 
168                                                   
169 // --- Streams                                    
170 std::ostream*& _G4debug_p()                       
171 {                                                 
172   G4ThreadLocalStatic auto* _instance = new st    
173   return _instance;                               
174 }                                                 
175                                                   
176 std::ostream*& _G4cout_p()                        
177 {                                                 
178   G4ThreadLocalStatic auto* _instance = new st    
179   return _instance;                               
180 }                                                 
181                                                   
182 std::ostream*& _G4cerr_p()                        
183 {                                                 
184   G4ThreadLocalStatic auto* _instance = new st    
185   return _instance;                               
186 }                                                 
187                                                   
188 void G4iosInitialization()                        
189 {                                                 
190   // --- Stream Buffers                           
191   if (_G4debugbuf_p() == nullptr) {               
192     _G4debugbuf_p() = new G4debugstreambuf;       
193   }                                               
194   if (_G4coutbuf_p() == nullptr) {                
195     _G4coutbuf_p() = new G4coutstreambuf;         
196   }                                               
197   if (_G4cerrbuf_p() == nullptr) {                
198     _G4cerrbuf_p() = new G4cerrstreambuf;         
199   }                                               
200                                                   
201   // --- Streams                                  
202   if (_G4debug_p() == &std::cout || _G4debug_p    
203     _G4debug_p() = new std::ostream(_G4debugbu    
204   }                                               
205   if (_G4cout_p() == &std::cout || _G4cout_p()    
206     _G4cout_p() = new std::ostream(_G4coutbuf_    
207   }                                               
208   if (_G4cerr_p() == &std::cerr || _G4cerr_p()    
209     _G4cerr_p() = new std::ostream(_G4cerrbuf_    
210   }                                               
211 }                                                 
212                                                   
213 void G4iosFinalization()                          
214 {                                                 
215   // Reverse order                                
216   // --- Flush                                    
217   _G4debug_p()->flush();                          
218   _G4cout_p()->flush();                           
219   _G4cerr_p()->flush();                           
220                                                   
221   // --- Streams                                  
222   delete _G4debug_p();                            
223   _G4debug_p() = &std::cout;                      
224   delete _G4cout_p();                             
225   _G4cout_p() = &std::cout;                       
226   delete _G4cerr_p();                             
227   _G4cerr_p() = &std::cerr;                       
228                                                   
229   // --- Stream Buffers                           
230   delete _G4debugbuf_p();                         
231   _G4debugbuf_p() = nullptr;                      
232   delete _G4coutbuf_p();                          
233   _G4coutbuf_p() = nullptr;                       
234   delete _G4cerrbuf_p();                          
235   _G4cerrbuf_p() = nullptr;                       
236 }                                                 
237                                                   
238 #  define G4debugbuf (*_G4debugbuf_p())           
239 #  define G4coutbuf (*_G4coutbuf_p())             
240 #  define G4cerrbuf (*_G4cerrbuf_p())             
241                                                   
242 // We want to trigger initialization at load t    
243 // finalization at unloading time.  Directly m    
244 // the `.init/.fini` section using the `__attr    
245 // (beside that it does not work on Windows) d    
246 // where Geant4 is built with static libraries    
247 // package is built with shared library and ha    
248 // that are linking against those shared libra    
249 // to the Geant4 static libraries (for example    
250 // code calls Geant4 directly in addition to i    
251 // the shared library).                           
252 // In this example, the explicit `.init/.fini`    
253 // added twice (because the linker is asked to    
254 // file twice and each time needs to add the i    
255 // This issue appear also for the initializati    
256 // including those declared as function static    
257 // To get the behavior we need (where the G4io    
258 // and G4iosFinalization are called exactly on    
259 // associate the initialization with a symbol     
260 // the linker is told to de-duplicate even in     
261 // above.  So we use an extern object that is     
262 // initialized once.                              
263 namespace                                         
264 {                                                 
265 struct RAII_G4iosSystem {                         
266    RAII_G4iosSystem() {                           
267      G4iosInitialization();                       
268    }                                              
269    ~RAII_G4iosSystem() {                          
270      G4iosFinalization();                         
271    }                                              
272 };                                                
273 }  // namespace                                   
274 // Extern but not user reachable (anonymous ty    
275 // We want the linker to only create one of th    
276 extern "C" RAII_G4iosSystem RAII_G4iosSystemOb    
277                                                   
278 #else  // Sequential                              
279                                                   
280 G4debugstreambuf G4debugbuf;                      
281 G4coutstreambuf G4coutbuf;                        
282 G4cerrstreambuf G4cerrbuf;                        
283                                                   
284 std::ostream G4debug(&G4debugbuf);                
285 std::ostream G4cout(&G4coutbuf);                  
286 std::ostream G4cerr(&G4cerrbuf);                  
287                                                   
288 void G4iosInitialization() {}                     
289 void G4iosFinalization() {                        
290   G4debug.flush();                                
291   G4cout.flush();                                 
292   G4cerr.flush();                                 
293 }                                                 
294                                                   
295 #endif                                            
296                                                   
297 void G4iosSetDestination(G4coutDestination* si    
298 {                                                 
299   G4debugbuf.SetDestination(sink);                
300   G4coutbuf.SetDestination(sink);                 
301   G4cerrbuf.SetDestination(sink);                 
302 }                                                 
303