Geant4-11
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes
G4UIbatch Class Reference

#include <G4UIbatch.hh>

Inheritance diagram for G4UIbatch:
G4UIsession G4coutDestination

Public Types

using Transformer = std::function< G4bool(G4String &)>
 

Public Member Functions

void AddCerrTransformer (const Transformer &t)
 
void AddCerrTransformer (Transformer &&t)
 
void AddCoutTransformer (const Transformer &t)
 
void AddCoutTransformer (Transformer &&t)
 
 G4UIbatch (const char *fileName, G4UIsession *prevSession=nullptr)
 
G4int GetLastReturnCode () const
 
G4UIsessionGetPreviousSession () const
 
virtual void PauseSessionStart (const G4String &Prompt)
 
virtual G4int ReceiveG4cerr (const G4String &cerrString)
 
G4int ReceiveG4cerr_ (const G4String &msg)
 
virtual G4int ReceiveG4cout (const G4String &coutString)
 
G4int ReceiveG4cout_ (const G4String &msg)
 
virtual void ResetTransformers ()
 
virtual G4UIsessionSessionStart ()
 
 ~G4UIbatch ()
 

Static Public Member Functions

static G4int InSession ()
 

Protected Attributes

G4int ifBatch = 0
 
G4int lastRC = 0
 
std::vector< TransformertransformersCerr
 
std::vector< TransformertransformersCout
 

Static Protected Attributes

static G4ICOMS_DLL G4int inSession = 0
 
static G4MTGLOB_DLL G4coutDestinationmasterG4coutDestination = nullptr
 

Private Member Functions

G4int ExecCommand (const G4String &command)
 
G4String ReadCommand ()
 

Private Attributes

G4bool isOpened = false
 
std::ifstream macroStream
 
G4UIsessionpreviousSession = nullptr
 

Detailed Description

Definition at line 46 of file G4UIbatch.hh.

Member Typedef Documentation

◆ Transformer

Definition at line 61 of file G4coutDestination.hh.

Constructor & Destructor Documentation

◆ G4UIbatch()

G4UIbatch::G4UIbatch ( const char *  fileName,
G4UIsession prevSession = nullptr 
)

Definition at line 66 of file G4UIbatch.cc.

67 : G4UIsession(1)
68 , previousSession(prevSession)
69{
70 macroStream.open(fileName, std::ios::in);
71 if(macroStream.fail())
72 {
73 G4cerr << "ERROR: Can not open a macro file <" << fileName
74 << ">. Set macro path with \"/control/macroPath\" if needed."
75 << G4endl;
77 }
78 else
79 {
80 isOpened = true;
81 }
82
84}
@ fParameterUnreadable
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4UIsession * previousSession
Definition: G4UIbatch.hh:67
std::ifstream macroStream
Definition: G4UIbatch.hh:69
G4bool isOpened
Definition: G4UIbatch.hh:70
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
void SetSession(G4UIsession *const value)
Definition: G4UImanager.hh:190
G4int lastRC
Definition: G4UIsession.hh:69

References fParameterUnreadable, G4cerr, G4endl, G4UImanager::GetUIpointer(), isOpened, G4UIsession::lastRC, macroStream, and G4UImanager::SetSession().

◆ ~G4UIbatch()

G4UIbatch::~G4UIbatch ( )

Definition at line 87 of file G4UIbatch.cc.

88{
89 if(isOpened)
90 macroStream.close();
91}

References isOpened, and macroStream.

Member Function Documentation

◆ AddCerrTransformer() [1/2]

void G4coutDestination::AddCerrTransformer ( const Transformer t)
inlineinherited

Definition at line 67 of file G4coutDestination.hh.

68 {
69 transformersCerr.push_back(t);
70 }
std::vector< Transformer > transformersCerr

References G4coutDestination::transformersCerr.

Referenced by G4MTcoutDestination::HandleFileCerr().

◆ AddCerrTransformer() [2/2]

void G4coutDestination::AddCerrTransformer ( Transformer &&  t)
inlineinherited

Definition at line 71 of file G4coutDestination.hh.

71{ transformersCerr.push_back(t); }

References G4coutDestination::transformersCerr.

◆ AddCoutTransformer() [1/2]

void G4coutDestination::AddCoutTransformer ( const Transformer t)
inlineinherited

◆ AddCoutTransformer() [2/2]

void G4coutDestination::AddCoutTransformer ( Transformer &&  t)
inlineinherited

Definition at line 66 of file G4coutDestination.hh.

66{ transformersCout.push_back(t); }

References G4coutDestination::transformersCout.

◆ ExecCommand()

G4int G4UIbatch::ExecCommand ( const G4String command)
private

Definition at line 179 of file G4UIbatch.cc.

180{
182 G4int rc = UI->ApplyCommand(command);
183
184 switch(rc)
185 {
187 break;
188 case fCommandNotFound:
189 G4cerr << "***** COMMAND NOT FOUND <" << command << "> *****" << G4endl;
190 break;
192 G4cerr << "***** Illegal application state <" << command << "> *****"
193 << G4endl;
194 break;
195 default:
196 G4int pn = rc % 100;
197 G4cerr << "***** Illegal parameter (" << pn << ") <" << command
198 << "> *****" << G4endl;
199 }
200
201 return rc;
202}
int G4int
Definition: G4Types.hh:85
@ fCommandNotFound
@ fIllegalApplicationState
@ fCommandSucceeded
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:485

References G4UImanager::ApplyCommand(), fCommandNotFound, fCommandSucceeded, fIllegalApplicationState, G4cerr, G4endl, G4UImanager::GetUIpointer(), and G4InuclParticleNames::pn.

Referenced by SessionStart().

◆ GetLastReturnCode()

G4int G4UIsession::GetLastReturnCode ( ) const
inlineinherited

Definition at line 63 of file G4UIsession.hh.

63{ return lastRC; }

References G4UIsession::lastRC.

◆ GetPreviousSession()

G4UIsession * G4UIbatch::GetPreviousSession ( ) const
inline

Definition at line 77 of file G4UIbatch.hh.

78{
79 return previousSession;
80}

References previousSession.

◆ InSession()

G4int G4UIsession::InSession ( )
staticinherited

Definition at line 68 of file G4UIsession.cc.

69{
70 return inSession;
71}
static G4ICOMS_DLL G4int inSession
Definition: G4UIsession.hh:67

References G4UIsession::inSession.

Referenced by G4UIcontrolMessenger::SetNewValue().

◆ PauseSessionStart()

void G4UIbatch::PauseSessionStart ( const G4String Prompt)
virtual

Reimplemented from G4UIsession.

Definition at line 243 of file G4UIbatch.cc.

244{
245 G4cout << "Pause session <" << Prompt << "> start." << G4endl;
246
247 SessionStart();
248
249 G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
250}
G4GLOB_DLL std::ostream G4cout
virtual G4UIsession * SessionStart()
Definition: G4UIbatch.cc:205

References G4cout, G4endl, and SessionStart().

◆ ReadCommand()

G4String G4UIbatch::ReadCommand ( )
private

Definition at line 94 of file G4UIbatch.cc.

95{
96 enum
97 {
98 BUFSIZE = 4096
99 };
100 static G4ThreadLocal char* linebuf = 0;
101 if(!linebuf)
102 linebuf = new char[BUFSIZE];
103 const char ctrM = 0x0d;
104
105 G4String cmdtotal = "";
106 G4bool qcontinued = false;
107 while(macroStream.good())
108 {
109 macroStream.getline(linebuf, BUFSIZE);
110
111 G4String cmdline(linebuf);
112
113 // TAB-> ' ' conversion
114 G4String::size_type nb = 0;
115 while((nb = cmdline.find('\t', nb)) != G4String::npos)
116 {
117 cmdline.replace(nb, 1, " ");
118 }
119
120 // strip
121 G4StrUtil::strip(cmdline);
122 G4StrUtil::rstrip(cmdline, ctrM);
123
124 // skip null line if single line
125 if(!qcontinued && cmdline.size() == 0)
126 continue;
127
128 // '#' is treated as echoing something
129 if(cmdline[(std::size_t) 0] == '#')
130 return cmdline;
131
132 // tokenize...
133 std::vector<G4String> tokens;
134 Tokenize(cmdline, tokens);
135 qcontinued = false;
136 for(G4int i = 0; i < G4int(tokens.size()); ++i)
137 {
138 // string after '#" is ignored
139 if(tokens[i][(std::size_t) 0] == '#')
140 break;
141 // '\' or '_' is treated as continued line.
142 if(tokens[i] == "\\" || tokens[i] == "_")
143 {
144 qcontinued = true;
145 // check nothing after line continuation character
146 if(i != G4int(tokens.size()) - 1)
147 {
148 G4Exception("G4UIbatch::ReadCommand", "UI0003", JustWarning,
149 "unexpected character after line continuation character");
150 }
151 break; // stop parsing
152 }
153 cmdtotal += tokens[i];
154 cmdtotal += " ";
155 }
156
157 if(qcontinued)
158 continue; // read the next line
159
160 if(cmdtotal.size() != 0)
161 break;
162 if(macroStream.eof())
163 break;
164 }
165
166 // strip again
167 G4StrUtil::strip(cmdtotal);
168
169 // finally,
170 if(macroStream.eof() && cmdtotal.size() == 0)
171 {
172 return "exit";
173 }
174
175 return cmdtotal;
176}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
bool G4bool
Definition: G4Types.hh:86
static void Tokenize(const G4String &str, std::vector< G4String > &tokens)
Definition: G4UIbatch.cc:37
#define BUFSIZE
Definition: liblist.c:40
void strip(G4String &str, char c=' ')
Remove leading and trailing characters from string.
void rstrip(G4String &str, char c=' ')
Remove trailing characters from string.
#define G4ThreadLocal
Definition: tls.hh:77

References BUFSIZE, G4Exception(), G4ThreadLocal, JustWarning, macroStream, G4StrUtil::rstrip(), G4StrUtil::strip(), and Tokenize().

Referenced by SessionStart().

◆ ReceiveG4cerr()

G4int G4UIsession::ReceiveG4cerr ( const G4String cerrString)
virtualinherited

Reimplemented from G4coutDestination.

Reimplemented in G4UIQt, G4UIXm, G4UIWin32, G4ZMQServer, and G4UIterminal.

Definition at line 81 of file G4UIsession.cc.

82{
83 std::cerr << cerrString << std::flush;
84 return 0;
85}

◆ ReceiveG4cerr_()

G4int G4coutDestination::ReceiveG4cerr_ ( const G4String msg)
inherited

Definition at line 84 of file G4coutDestination.cc.

85{
86 if(transformersCout.size() > 0)
87 {
88 G4String m = msg;
89 std::for_each(transformersCerr.begin(), transformersCerr.end(),
90 [&m](const Transformer& t) { t(m); }
91 // Call transforming function on message
92 );
93 return ReceiveG4cerr(m);
94 }
95 else
96 {
97 return ReceiveG4cerr(msg);
98 }
99}
static constexpr double m
Definition: G4SIunits.hh:109
virtual G4int ReceiveG4cerr(const G4String &msg)
std::function< G4bool(G4String &)> Transformer

References m, G4coutDestination::ReceiveG4cerr(), G4coutDestination::transformersCerr, and G4coutDestination::transformersCout.

Referenced by G4MasterForwardcoutDestination::ReceiveG4cerr().

◆ ReceiveG4cout()

G4int G4UIsession::ReceiveG4cout ( const G4String coutString)
virtualinherited

Reimplemented from G4coutDestination.

Reimplemented in G4UIQt, G4UIXm, G4UIWin32, G4ZMQServer, and G4UIterminal.

Definition at line 74 of file G4UIsession.cc.

75{
76 std::cout << coutString << std::flush;
77 return 0;
78}

◆ ReceiveG4cout_()

G4int G4coutDestination::ReceiveG4cout_ ( const G4String msg)
inherited

Definition at line 62 of file G4coutDestination.cc.

63{
64 // Avoid copy of string if not necessary
65 if(transformersCout.size() > 0)
66 {
67 G4String m = msg;
68 G4bool result = true;
69 for(const auto& el : transformersCout)
70 {
71 result &= el(m);
72 if(!result)
73 break;
74 }
75 return (result ? ReceiveG4cout(m) : 0);
76 }
77 else
78 {
79 return ReceiveG4cout(msg);
80 }
81}
virtual G4int ReceiveG4cout(const G4String &msg)

References m, G4coutDestination::ReceiveG4cout(), and G4coutDestination::transformersCout.

Referenced by G4MasterForwardcoutDestination::ReceiveG4cout().

◆ ResetTransformers()

void G4coutDestination::ResetTransformers ( )
virtualinherited

Definition at line 41 of file G4coutDestination.cc.

42{
43 transformersCout.clear();
44 transformersCerr.clear();
45}

References G4coutDestination::transformersCerr, and G4coutDestination::transformersCout.

◆ SessionStart()

G4UIsession * G4UIbatch::SessionStart ( )
virtual

Reimplemented from G4UIsession.

Definition at line 205 of file G4UIbatch.cc.

206{
207 if(!isOpened)
208 return previousSession;
209
210 while(1)
211 {
212 G4String newCommand = ReadCommand();
213
214 if(newCommand == "exit")
215 {
216 break;
217 }
218
219 // just echo something
220 if(newCommand[(std::size_t) 0] == '#')
221 {
222 if(G4UImanager::GetUIpointer()->GetVerboseLevel() == 2)
223 {
224 G4cout << newCommand << G4endl;
225 }
226 continue;
227 }
228
229 // execute command
230 G4int rc = ExecCommand(newCommand);
231 if(rc != fCommandSucceeded)
232 {
233 G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl;
234 lastRC = rc;
235 break;
236 }
237 }
238
239 return previousSession;
240}
G4String ReadCommand()
Definition: G4UIbatch.cc:94
G4int ExecCommand(const G4String &command)
Definition: G4UIbatch.cc:179

References ExecCommand(), fCommandSucceeded, G4cerr, G4cout, G4endl, G4UImanager::GetUIpointer(), isOpened, G4UIsession::lastRC, previousSession, and ReadCommand().

Referenced by PauseSessionStart().

Field Documentation

◆ ifBatch

G4int G4UIsession::ifBatch = 0
protectedinherited

Definition at line 68 of file G4UIsession.hh.

Referenced by G4UIsession::~G4UIsession().

◆ inSession

G4int G4UIsession::inSession = 0
staticprotectedinherited

◆ isOpened

G4bool G4UIbatch::isOpened = false
private

Definition at line 70 of file G4UIbatch.hh.

Referenced by G4UIbatch(), SessionStart(), and ~G4UIbatch().

◆ lastRC

G4int G4UIsession::lastRC = 0
protectedinherited

Definition at line 69 of file G4UIsession.hh.

Referenced by G4UIbatch(), G4UIsession::GetLastReturnCode(), and SessionStart().

◆ macroStream

std::ifstream G4UIbatch::macroStream
private

Definition at line 69 of file G4UIbatch.hh.

Referenced by G4UIbatch(), ReadCommand(), and ~G4UIbatch().

◆ masterG4coutDestination

G4coutDestination * G4coutDestination::masterG4coutDestination = nullptr
staticprotectedinherited

◆ previousSession

G4UIsession* G4UIbatch::previousSession = nullptr
private

Definition at line 67 of file G4UIbatch.hh.

Referenced by GetPreviousSession(), and SessionStart().

◆ transformersCerr

std::vector<Transformer> G4coutDestination::transformersCerr
protectedinherited

◆ transformersCout

std::vector<Transformer> G4coutDestination::transformersCout
protectedinherited

The documentation for this class was generated from the following files: