PSCF v1.5.0
Begin.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, David Morse (morse012@um.edu)
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Begin.h"
9#ifdef UTIL_MPI
10#include <util/mpi/MpiSendRecv.h>
11#endif
12
13#include <util/global.h>
14
15namespace Util
16{
17
18 /*
19 * Begin constructor
20 */
21 Begin::Begin(const char *label, bool isRequired)
22 : label_(isRequired),
23 isActive_(false)
24 {
25 std::string expected = label;
26 expected += "{";
27 label_.setString(expected);
28 }
29
30 /*
31 * Read label, check against expected value
32 */
33 void Begin::readParam(std::istream &in)
34 {
35 if (isIoProcessor()) {
36 in >> label_;
37
38 // If this parameter is required and the label string
39 // doesn't match, an Exception will be thrown by the
40 // operator >> for a Label, terminating this function.
41 // Test isMatched rather than isClear: the buffer is also
42 // clear if no string was left to read at the end of the file.
43
44 if (Label::isMatched()) {
45 // If the label string matches
46 isActive_ = true;
49 }
50 } else {
51 // If label does not match and this isOptional
52 isActive_ = false;
54 Log::file() << indent()
55 << label_.string() << " [absent] }"
56 << std::endl;
57 }
58 }
59 } else {
60 #ifdef UTIL_MPI
61 if (!hasIoCommunicator()) {
62 UTIL_THROW("Error: not isIoProcessor and not hasIoCommunicator");
63 }
64 #else
65 UTIL_THROW("Error: not isIoProcessor and no MPI");
66 #endif
67 }
68 #ifdef UTIL_MPI
69 if (hasIoCommunicator()) {
70 if (isRequired()) {
71 isActive_ = true;
72 } else {
73 bcast<bool>(ioCommunicator(), isActive_, 0);
74 }
75 }
76 #endif
77 }
78
79 /*
80 * Write label and opening bracket.
81 */
82 void Begin::writeParam(std::ostream &out) const
83 { out << indent() << label_.string() << std::endl; }
84
85 /*
86 * Do-nothing implementation of virtual resetIo function.
87 */
89 {}
90
91}
virtual void readParam(std::istream &in)
Read the opening line.
Definition Begin.cpp:33
bool isRequired() const
Is this the beginning line for a required element?
Definition Begin.h:78
Begin(const char *label, bool isRequired=true)
Constructor.
Definition Begin.cpp:21
virtual void writeParam(std::ostream &out) const
Write the opening line.
Definition Begin.cpp:82
virtual void resetParam()
Do-nothing implementation of virtual resetParam function.
Definition Begin.cpp:88
static bool isMatched()
Did the most recent attempt to match a Label succeed?
Definition Label.h:286
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
bool isIoProcessor() const
Can this processor do file I/O ?
Definition MpiFileIo.h:94
std::string indent() const
Return indent string for this object (string of spaces).
static bool echo()
Get echo parameter.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.