PSCF v1.1
FSArrayParam.h
1#ifndef UTIL_F_S_ARRAY_PARAM_H
2#define UTIL_F_S_ARRAY_PARAM_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/ArrayParam.h> // base class
12#include <util/containers/FSArray.h> // member
13#include <util/global.h>
14
15#include <iomanip>
16
17namespace Util
18{
19
25 template <typename Type, int Capacity>
26 class FSArrayParam : public ArrayParam<Type>
27 {
28
29 public:
30
39 FSArrayParam(const char *label, FSArray<Type, Capacity>& array,
40 int n, bool isRequired = true);
41
47 void writeParam(std::ostream &out) const;
48
49 using ArrayParam<Type>::readParam;
50
51 protected:
52
56 Type& element(int i)
57 { return (*arrayPtr_)[i]; }
58
62 Type const & element(int i) const
63 { return (*arrayPtr_)[i]; }
64
70 virtual void readValue(std::istream& in);
71
77 virtual void loadValue(Serializable::IArchive& ar);
78
84 virtual void saveValue(Serializable::OArchive& ar);
85
86 #ifdef UTIL_MPI
90 virtual void bcastValue();
91 #endif
92
93 using ArrayParam<Type>::n;
94
95 private:
96
98 FSArray<Type, Capacity>* arrayPtr_;
99
100 };
101
102 /*
103 * FSArrayParam<Type, Capacity> constructor.
104 */
105 template <typename Type, int Capacity>
108 int n,
109 bool isRequired)
110 : ArrayParam<Type>(label, n, isRequired),
111 arrayPtr_(&array)
112 {}
113
114 /*
115 * Write a FSArray to a parameter file.
116 */
117 template <typename Type, int Capacity>
118 void FSArrayParam<Type, Capacity>::writeParam(std::ostream &out) const
119 {
120 if (Parameter::isActive()) {
121 UTIL_CHECK(n() <= Capacity);
122 UTIL_CHECK(n() == arrayPtr_->size());
124 }
125 }
126
127 /*
128 * Read array of element values from istream.
129 */
130 template <typename Type, int Capacity>
132 {
133 UTIL_CHECK(n() <= Capacity);
134 arrayPtr_->clear();
135 Type value;
136 for (int i = 0; i < n(); ++i) {
137 in >> value;
138 arrayPtr_->append(value);
139 }
140 UTIL_CHECK(n() == arrayPtr_->size());
142 }
143
144 /*
145 * Load an FSArray from input archive.
146 */
147 template <typename Type, int Capacity>
149 {
150 UTIL_CHECK(n() <= Capacity);
151 ar >> *arrayPtr_;
152 }
153
154 /*
155 * Save an FSArray to an output archive.
156 */
157 template <typename Type, int Capacity>
159 {
160 UTIL_CHECK(n() <= Capacity);
161 ar << *arrayPtr_;
162 }
163
164 #ifdef UTIL_MPI
165 /*
166 * Broadcast a FSArray.
167 */
168 template <typename Type, int Capacity>
170 { bcast<Type>(ioCommunicator(), *arrayPtr_, Capacity, 0); }
171 #endif
172
173}
174#endif
An array-valued parameter in a parameter file.
Definition: ArrayParam.h:56
int n() const
Get the logical array dimension.
Definition: ArrayParam.h:86
std::string label() const
Return label string.
Definition: Parameter.cpp:164
void writeParam(std::ostream &out) const
Write an array-valued parameter to stream.
Definition: ArrayParam.tpp:139
void readEndBracket(std::istream &in)
Read a closing bracket, if necessary.
Definition: ArrayParam.tpp:115
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:170
Saving archive for binary istream.
Saving / output archive for binary ostream.
A Parameter associated with a FSArray container.
Definition: FSArrayParam.h:27
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: FSArrayParam.h:158
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: FSArrayParam.h:169
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: FSArrayParam.h:148
Type & element(int i)
Return a reference to one element of the array (non-const).
Definition: FSArrayParam.h:56
void writeParam(std::ostream &out) const
Write parameter to stream.
Definition: FSArrayParam.h:118
FSArrayParam(const char *label, FSArray< Type, Capacity > &array, int n, bool isRequired=true)
Constructor.
Definition: FSArrayParam.h:106
Type const & element(int i) const
Return a reference to one element of the array (const).
Definition: FSArrayParam.h:62
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: FSArrayParam.h:131
A fixed capacity (static) contiguous array with a variable logical size.
Definition: FSArray.h:38
virtual void readParam(std::istream &in)
Read a label and (if the label matches) a parameter value.
Definition: Parameter.cpp:42
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:176
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
Utility classes for scientific computation.
Definition: accumulators.mod:1