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