PSCF v1.3
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
ArrayParam(const char *label, int n, bool isRequired=true)
Constructor.
std::string label() const
Return label string.
void writeParam(std::ostream &out) const
Write an array-valued parameter to stream.
void readEndBracket(std::istream &in)
Read a closing bracket, if necessary.
bool isRequired() const
Is this an optional parameter?
A Parameter associated with a FSArray container.
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Type & element(int i)
Return a reference to one element of the array (non-const).
void writeParam(std::ostream &out) const
Write parameter to stream.
FSArrayParam(const char *label, FSArray< Type, Capacity > &array, int n, bool isRequired=true)
Constructor.
Type const & element(int i) const
Return a reference to one element of the array (const).
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
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?
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
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.