PSCF v1.1
|
A label string in a file format. More...
#include <Label.h>
Public Member Functions | |
Label (bool isRequired=true) | |
Constructor. More... | |
Label (std::string string, bool isRequired=true) | |
Constructor. More... | |
Label (const char *string, bool isRequired=true) | |
Constructor. More... | |
Label (const Label &other) | |
Copy constructor. More... | |
virtual | ~Label () |
Destructor. More... | |
void | setString (std::string string) |
Set the label string. More... | |
void | setIsRequired (bool isRequired) |
Set the isRequired boolean flag. More... | |
bool | match (std::istream &in) |
Read and attempt to match next word in an input stream. More... | |
std::string | string () const |
Return label string. More... | |
bool | isRequired () const |
Is this the label for a required component? More... | |
Static Public Member Functions | |
static void | read (std::istream &in) |
Read a string without checking its value. More... | |
static void | clear () |
Clear the input buffer. More... | |
static void | setIsMatched (bool isMatched) |
Explicitly set the isMatched flag. More... | |
static std::string | buffer () |
Get the string that is currently in the input buffer. More... | |
static bool | isClear () |
Is the input buffer clear? More... | |
static bool | isMatched () |
Did the most recent attempt to match a Label succeed? More... | |
Static Public Attributes | |
static const int | LabelWidth = 20 |
Width of label field in file output format. More... | |
Friends | |
std::istream & | operator>> (std::istream &in, Label label) |
Extractor for Label. More... | |
std::ostream & | operator<< (std::ostream &out, Label label) |
Inserter for Label. More... | |
A label string in a file format.
The operator >> for a label checks if the expected label was found. The operator << outputs the expected label.
Every Label constructor takes an optional parameter isRequired that determines whether the label must be matched (isRequired == true), or if it is optional (isRequired == false). The isRequired parameter is true by default. If the input value read by the >> operator does not match the expected value and isRequired is true, the >> operator will print an error message to the Log::file() and then throw an Exception. If the input value does not match and isRequired is false, the >> operator stores the input value in a buffer string for later use. If the >> operator is invoked when the buffer is not clear (e.g., after a failed match to an optional label), the operator does not read from file and compares the expected label to the string in the buffer.
The class is designed to allow either of two methods to be used to read and check a label, referred to below as cases A and B:
Case A (the usual case, and concise) : The user constructs a Label object using a constructor that takes an expected label string as as argument, then uses the >> operator to attempt to read a string from file into the Label. For example, to read a required label that must match the expected label string "thing", one might use the line
where "in" is a symbol for an input stream. The overloaded ">>" operator reads a string from input stream into a buffer if and only if the buffer is empty. It then compares the string in the buffer to the label string (e.g., "thing"). If the strings match, the buffer is cleared and isMatch is set true. If the strings do not match and is isRequired is true, an Exception is thrown. If the strings do not match but isRequired is false, the buffer is left unchanged and isMatched is set false.
Case B (less common, and more verbose) : A user can use several static functions to perform a sequence of operations similar to those peformed by the >> operator. In this case, the user must call the static Label::read function to read a string from an input stream iff the buffer is empty, then call Label::buffer to retrieve the buffer string, and explicitly check if that string matches one or more allowed possible labels. If a match is found, call Label::clear to clear the buffer and call Label::setIsMatched to set isMatched false. This more verbose sequence of operations is used in the readObject function of the Factory class template, in which a single input string is * compared to a list of several possible valid labels.
|
explicit |
Util::Label::Label | ( | std::string | string, |
bool | isRequired = true |
||
) |
Util::Label::Label | ( | const char * | string, |
bool | isRequired = true |
||
) |
Util::Label::Label | ( | const Label & | other | ) |
|
static |
Read a string without checking its value.
This function reads a string from input stream in if and only if the buffer is empty. It does not compare the string to an expected value. If matching is desired, use >> operator.
in | input stream |
Definition at line 35 of file Label.cpp.
References UTIL_THROW.
Referenced by pscfpp.param.Composite::__init__(), pscfpp.field.Field::__init__(), pscfpp.command.Script::__init__(), pscfpp.output.Thermo::__init__(), pscfpp.param.Matrix::__init__(), pscfpp.param.Array::__init__(), and Util::Factory< Data >::readObject().
|
static |
Clear the input buffer.
Definition at line 25 of file Label.cpp.
Referenced by Util::Factory< Data >::readObject().
|
inlinestatic |
Explicitly set the isMatched flag.
isMatched | boolean value to store in isMatched_. |
Definition at line 280 of file Label.h.
References isMatched().
Referenced by Util::Factory< Data >::readObject().
|
inlinestatic |
Get the string that is currently in the input buffer.
Definition at line 286 of file Label.h.
Referenced by Util::Factory< Data >::readObject().
|
inlinestatic |
Is the input buffer clear?
Definition at line 267 of file Label.h.
Referenced by Util::Factory< Data >::readObject(), and Util::Begin::readParam().
|
inlinestatic |
Did the most recent attempt to match a Label succeed?
Returns true after a successful match by operator >> or the match() function. Returns false before any attempt to match any Label, or after a failed attempt to match either a required or optional label.
Definition at line 274 of file Label.h.
Referenced by Util::ArrayParam< Type >::readLabel(), Util::MatrixParam< Type >::readLabel(), Util::Parameter::readParam(), and setIsMatched().
void Util::Label::setString | ( | std::string | string | ) |
Set the label string.
string | label string that precedes value in file format |
Definition at line 99 of file Label.cpp.
References string().
Referenced by Util::ArrayParam< Type >::ArrayParam(), Util::Begin::Begin(), and Util::MatrixParam< Type >::MatrixParam().
void Util::Label::setIsRequired | ( | bool | isRequired | ) |
Set the isRequired boolean flag.
isRequired | boolean that is true iff the label is required. |
Definition at line 105 of file Label.cpp.
References isRequired().
bool Util::Label::match | ( | std::istream & | in | ) |
Read and attempt to match next word in an input stream.
This is a convenience function that invokes operator >> to read a word and then returns the value of Label::isMatched(). For an optional Label, this returns true upon a successful match and false otherwise. For a required label, returns true upon a successful match or throws an Exception.
std::string Util::Label::string | ( | ) | const |
Return label string.
Definition at line 111 of file Label.cpp.
Referenced by Util::ArrayParam< Type >::save(), setString(), and Util::Begin::writeParam().
|
inline |
Is this the label for a required component?
Definition at line 261 of file Label.h.
Referenced by Util::Begin::isRequired(), Util::Parameter::isRequired(), and setIsRequired().
|
friend |
|
friend |
|
static |