PSCF v1.1
pspc/field/Field.h
1#ifndef PSPC_FIELD_H
2#define PSPC_FIELD_H
3
4/*
5* PSCF Package
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/global.h>
12
13namespace Pscf {
14namespace Pspc
15{
16
17 using namespace Util;
18
24 template <typename Data>
25 class Field
26 {
27
28 public:
29
34
40 virtual ~Field();
41
49 void allocate(int capacity);
50
56 void deallocate();
57
61 bool isAllocated() const;
62
68 int capacity() const;
69
78 Data & operator[] (int i);
79
88 Data const & operator[] (int i) const;
89
93 Data* cField();
94
98 Data const * cField() const;
99
106 template <class Archive>
107 void serialize(Archive& ar, const unsigned int version);
108
109 protected:
110
112 Data* data_;
113
116
117 private:
118
122 Field(Field const & other);
123
127 Field& operator = (Field const & other);
128
129 };
130
131 /*
132 * Return allocated size.
133 */
134 template <typename Data>
135 inline int Field<Data>::capacity() const
136 { return capacity_; }
137
138 /*
139 * Get an element by reference (C-array subscripting)
140 */
141 template <typename Data>
142 inline Data& Field<Data>::operator[] (int i)
143 {
144 assert(data_ != 0);
145 assert(i >= 0);
146 assert(i < capacity_);
147 return *(data_ + i);
148 }
149
150 /*
151 * Get an element by const reference (C-array subscripting)
152 */
153 template <typename Data>
154 inline Data const & Field<Data>::operator[] (int i) const
155 {
156 assert(data_ != 0);
157 assert(i >= 0 );
158 assert(i < capacity_);
159 return *(data_ + i);
160 }
161
162 /*
163 * Get a pointer to the underlying C array.
164 */
165 template <typename Data>
166 inline Data* Field<Data>::cField()
167 { return data_; }
168
169 /*
170 * Get a pointer to const to the underlying C array.
171 */
172 template <typename Data>
173 inline
174 Data const * Field<Data>::cField() const
175 { return data_; }
176
177 /*
178 * Return true if the Field has been allocated, false otherwise.
179 */
180 template <typename Data>
181 inline bool Field<Data>::isAllocated() const
182 { return (bool)data_; }
183
184 /*
185 * Serialize a Field to/from an Archive.
186 */
187 template <typename Data>
188 template <class Archive>
189 void Field<Data>::serialize(Archive& ar, const unsigned int version)
190 {
191 int capacity;
192 if (Archive::is_saving()) {
193 capacity = capacity_;
194 }
195 ar & capacity;
196 if (Archive::is_loading()) {
197 if (!isAllocated()) {
198 if (capacity > 0) {
199 allocate(capacity);
200 }
201 } else {
202 if (capacity != capacity_) {
203 UTIL_THROW("Inconsistent Field capacities");
204 }
205 }
206 }
207 if (isAllocated()) {
208 for (int i = 0; i < capacity_; ++i) {
209 ar & data_[i];
210 }
211 }
212 }
213
214}
215}
216#include "Field.tpp"
217#endif
Base class template for a field defined on a spatial grid.
Field< T > & operator=(Field< T > const &other)
Assignment operator.
Field()
Constructor.
Definition: Field.tpp:26
int capacity() const
Return allocated size.
Data * data_
Pointer to an array of Data elements.
Data & operator[](int i)
Get an element by non-const reference.
void serialize(Archive &ar, const unsigned int version)
Serialize a Field to/from an Archive.
bool isAllocated() const
Return true if the Field has been allocated, false otherwise.
Field()
Default constructor.
Data * cField()
Return pointer to underlying C array.
int capacity_
Allocated size of the data_ array.
int capacity() const
Return allocated size.
Definition: Array.h:159
Data & operator[](int i)
Get an element by non-const reference.
Definition: Array.h:190
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:199
void deallocate()
Dellocate the underlying C array.
Definition: DArray.h:217
bool isAllocated() const
Return true if this DArray has been allocated, false otherwise.
Definition: DArray.h:247
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
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1