Geant4  10.00.p02
G4strstreambuf.icc
Go to the documentation of this file.
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 // $Id: G4strstreambuf.icc 67970 2013-03-13 10:10:06Z gcosmo $
28 // ====================================================================
29 // G4strstreambuf.icc
30 //
31 // ====================================================================
32 
33 ///////////////////////////////////////
34 inline G4strstreambuf::G4strstreambuf()
35  : std::basic_streambuf<char>(),
36  count(0), destination(0)
37 ///////////////////////////////////////
38 {
39  size= 4095;
40  buffer= new char[size+1];
41 }
42 
43 
44 ////////////////////////////////////////
45 inline G4strstreambuf::~G4strstreambuf()
46 ////////////////////////////////////////
47 {
48  // flushing buffer...
49  // std::cout is used because destination object may not be alive.
50  if(count !=0) std::cout << buffer;
51 
52  delete [] buffer;
53 }
54 
55 
56 //////////////////////////////////////////////////////////////////
57 inline G4strstreambuf::G4strstreambuf(const G4strstreambuf& right)
58  : std::basic_streambuf<char>(),
59  buffer(right.buffer),
60  count(right.count), size(right.size),
61  destination(right.destination)
62 //////////////////////////////////////////////////////////////////
63 {
64 }
65 
66 
67 /////////////////////////////////////////////////////////////////////////////
68 inline G4strstreambuf& G4strstreambuf::operator=(const G4strstreambuf& right)
69 /////////////////////////////////////////////////////////////////////////////
70 {
71  if(&right==this) return *this;
72 
73  destination= right.destination;
74  buffer= right.buffer;
75  count= right.count;
76  size= right.size;
77 
78  return *this;
79 }
80 
81 
82 //////////////////////////////////////////////
83 inline G4int G4strstreambuf::overflow(G4int c)
84 //////////////////////////////////////////////
85 {
86  G4int result= 0;
87  if(count>=size) result= sync();
88 
89  buffer[count]= c;
90  count++;
91 
92  return result;
93 }
94 
95 ///////////////////////////////////
96 inline G4int G4strstreambuf::sync()
97 ///////////////////////////////////
98 {
99  buffer[count] = '\0';
100  count= 0;
101  return ReceiveString();
102 }
103 
104 
105 #ifdef WIN32
106 ////////////////////////////////////////
107 inline G4int G4strstreambuf::underflow()
108 ////////////////////////////////////////
109 {
110  return 0;
111 }
112 #endif
113 
114 
115 ///////////////////////////////////////////////////////////////////
116 inline void G4strstreambuf::SetDestination(G4coutDestination* dest)
117 ///////////////////////////////////////////////////////////////////
118 {
119  destination= dest;
120 }
121 
122 
123 /////////////////////////////////////////////
124 inline G4int G4strstreambuf::ReceiveString ()
125 /////////////////////////////////////////////
126 {
127  G4String stringToSend(buffer);
128  G4int result= 0;
129 
130  if(this == &G4coutbuf && destination != 0) {
131  result= destination-> ReceiveG4cout(stringToSend);
132 
133  } else if(this == &G4cerrbuf && destination != 0) {
134  result= destination-> ReceiveG4cerr(stringToSend);
135 
136  } else if(this == &G4coutbuf && destination == 0) {
137  std::cout << stringToSend << std::flush;
138  result= 0;
139 
140  } else if(this == &G4cerrbuf && destination == 0) {
141  std::cerr << stringToSend << std::flush;
142  result= 0;
143  }
144 
145  return result;
146 }
147