PSCF v1.1
FArrayParam.h
1#ifndef UTIL_F_ARRAY_PARAM_H
2#define UTIL_F_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/FArray.h> // member
13#include <util/global.h>
14
15#include <iomanip>
16
17namespace Util
18{
19
25 template <typename Type, int Capacity>
26 class FArrayParam : public ArrayParam<Type>
27 {
28
29 public:
30
38 FArrayParam(const char *label, FArray<Type, Capacity>& array,
39 bool isRequired = true);
40
46 void writeParam(std::ostream &out) const;
47
48 using ArrayParam<Type>::readParam;
49
50 protected:
51
55 Type& element(int i)
56 { return (*arrayPtr_)[i]; }
57
61 Type const & element(int i) const
62 { return (*arrayPtr_)[i]; }
63
69 virtual void readValue(std::istream& in);
70
76 virtual void loadValue(Serializable::IArchive& ar);
77
83 virtual void saveValue(Serializable::OArchive& ar);
84
85 #ifdef UTIL_MPI
89 virtual void bcastValue();
90 #endif
91
92 using ArrayParam<Type>::n;
93
94 private:
95
97 FArray<Type, Capacity>* arrayPtr_;
98
99 };
100
101 /*
102 * FArrayParam<Type, Capacity> constructor.
103 */
104 template <typename Type, int Capacity>
107 bool isRequired)
108 : ArrayParam<Type>(label, Capacity, isRequired),
109 arrayPtr_(&array)
110 {}
111
112 /*
113 * Write a FArray to a parameter file.
114 */
115 template <typename Type, int Capacity>
116 void FArrayParam<Type, Capacity>::writeParam(std::ostream &out) const
117 {
118 if (Parameter::isActive()) {
119 if (Capacity != n()) {
120 UTIL_THROW("Error: FArray capacity != n in writeParam");
121 }
123 }
124 }
125
126 /*
127 * Read array of values from isteam.
128 */
129 template <typename Type, int Capacity>
131 {
132 if (Capacity != n()) {
133 UTIL_THROW("Error: FArray capacity != n");
134 }
136 }
137
138 /*
139 * Load a FArray from input archive.
140 */
141 template <typename Type, int Capacity>
143 {
144 if (Capacity != n()) {
145 UTIL_THROW("Error: FArray capacity != n");
146 }
147 ar >> *arrayPtr_;
148 }
149
150 /*
151 * Save a FArray to an output archive.
152 */
153 template <typename Type, int Capacity>
155 {
156 if (Capacity != n()) {
157 UTIL_THROW("Error: FArray capacity != n");
158 }
159 ar << *arrayPtr_;
160 }
161
162 #ifdef UTIL_MPI
163 /*
164 * Broadcast a FArray.
165 */
166 template <typename Type, int Capacity>
168 { bcast<Type>(ioCommunicator(), *arrayPtr_, Capacity, 0); }
169 #endif
170
171}
172#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
virtual void readValue(std::istream &in)
Read array of element values from an input stream.
Definition: ArrayParam.tpp:103
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 FArray container.
Definition: FArrayParam.h:27
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: FArrayParam.h:167
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: FArrayParam.h:154
void writeParam(std::ostream &out) const
Write parameter to stream.
Definition: FArrayParam.h:116
FArrayParam(const char *label, FArray< Type, Capacity > &array, bool isRequired=true)
Constructor.
Definition: FArrayParam.h:105
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: FArrayParam.h:130
Type & element(int i)
Return a reference to one element of the array (non-const).
Definition: FArrayParam.h:55
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: FArrayParam.h:142
Type const & element(int i) const
Return a reference to one element of the array (const).
Definition: FArrayParam.h:61
A fixed size (static) contiguous array template.
Definition: FArray.h:47
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_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1