PSCF v1.1
MatrixParam.tpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "MatrixParam.h"
9#include "BracketPolicy.h"
10
11#ifdef UTIL_MPI
13#endif
14
15#include <iomanip>
16
17namespace Util
18{
19
20 /*
21 * Constructor.
22 */
23 template <typename Type>
24 MatrixParam<Type>::MatrixParam(const char *label, int m, int n,
25 bool isRequired)
26 : Parameter(label, isRequired),
27 name_(label),
28 lBracket_("["),
29 rBracket_("]"),
30 m_(m),
31 n_(n),
32 isRequired_(isRequired),
33 hasBrackets_(false)
34 {
35 if (BracketPolicy::get() != BracketPolicy::Forbidden) {
36 std::string name = name_;
37 name += lBracket_;
38 label_.setString(name);
39 hasBrackets_ = true;
40 }
41 }
42
43 /*
44 * Destructor.
45 */
46 template <typename Type>
48 {}
49
50 /*
51 * Set left and right bracket delimiter strings.
52 */
53 template <typename Type>
54 void MatrixParam<Type>::setBrackets(std::string lBracket,
55 std::string rBracket)
56 {
57 // Set bracket string values
58 lBracket_ = lBracket;
59 rBracket_ = rBracket;
60
61 // Reset label_ and hasBrackets to default for output
62 std::string string = name_;
63 if (BracketPolicy::get() == BracketPolicy::Forbidden) {
64 hasBrackets_ = false;
65 } else {
66 string += lBracket_;
67 hasBrackets_ = true;
68 }
69 label_.setString(string);
70 }
71
72 /*
73 * Read the array label.
74 */
75 template <typename Type>
76 void MatrixParam<Type>::readLabel(std::istream &in)
77 {
78 isActive_ = false;
79
80 if (BracketPolicy::get() == BracketPolicy::Optional) {
81
82 // First try to match array name with an appended left bracket
83 std::string string = name_;
84 string += lBracket_;
85 label_.setString(string); // Tentatively reset label string
86 label_.setIsRequired(false); // Temporarily set isRequired false
87 hasBrackets_ = true;
88 in >> label_;
89 // This will not throw an exception, because isRequired is false
90
91 // Restore original isRequired value of label
92 label_.setIsRequired(isRequired_);
93
94 if (!Label::isMatched()) {
95 // Try to match array name without appended ( bracket
96 label_.setString(name_);
97 hasBrackets_ = false;
98 in >> label_;
99 }
100
101 } else
102 if (BracketPolicy::get() == BracketPolicy::Required) {
103
104 std::string string = name_;
105 string += lBracket_;
106 label_.setString(string);
107 hasBrackets_ = true;
108 in >> label_;
109
110 } else
111 if (BracketPolicy::get() == BracketPolicy::Forbidden) {
112
113 std::string string = name_;
114 label_.setString(string);
115 hasBrackets_ = false;
116 in >> label_;
117
118 }
119
120 }
121
122 /*
123 * Read end bracket delimiter, if any.
124 */
125 template <class Type>
127 {
128 // If necessary, attempt to read closing bracket
129 if (hasBrackets_) {
130 UTIL_CHECK(BracketPolicy::get() != BracketPolicy::Forbidden);
131 in >> Label(rBracket_);
132 // Operator >> will throw an Exception if bracket is not matched
133 }
134
135 // Reset label_ and hasBrackets to default for output
136 std::string string = name_;
137 if (BracketPolicy::get() == BracketPolicy::Forbidden) {
138 hasBrackets_ = false;
139 } else {
140 string += lBracket_;
141 hasBrackets_ = true;
142 }
143 label_.setString(string);
144 }
145
146 /*
147 * Write end bracket delimiter, if any.
148 */
149 template <class Type>
150 void MatrixParam<Type>::writeEndBracket(std::ostream& out) const
151 {
152 if (hasBrackets_) {
153 out << indent() << rBracket_ << std::endl;
154 }
155 }
156
157}
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
A label string in a file format.
Definition: Label.h:73
static bool isMatched()
Did the most recent attempt to match a Label succeed?
Definition: Label.h:274
void setString(std::string string)
Set the label string.
Definition: Label.cpp:99
void setBrackets(std::string lBracket, std::string rBracket)
Set left and right bracket / delimiter strings.
Definition: MatrixParam.tpp:54
virtual void readLabel(std::istream &in)
Read parameter label from an input stream.
Definition: MatrixParam.tpp:76
virtual ~MatrixParam()
Destructor.
Definition: MatrixParam.tpp:47
void readEndBracket(std::istream &in)
Read the closing delimiter, if any.
void writeEndBracket(std::ostream &out) const
Write the end bracket delimiter, if any.
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
MatrixParam(const char *label, int m, int n, bool isRequired=true)
Constructor.
Definition: MatrixParam.tpp:24
A single variable in a parameter file.
Definition: Parameter.h:46
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
BracketPolicy::Type get()
Get value of bracket policy.
Utility classes for scientific computation.
Definition: accumulators.mod:1