PSCF v1.5.0
ConstArray.h
1#ifndef UTIL_CONST_ARRAY_H
2#define UTIL_CONST_ARRAY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/ConstArrayIterator.h>
12#include <util/global.h>
13
14namespace Util
15{
16
36 template <typename Data>
37 class ConstArray
38 {
39
40 public:
41
42 // Protected default construction and destructor (see below)
43
44 // Prohibit copy construction
45 ConstArray(ConstArray const & other) = delete;
46
47 // Prohibit assignment
48 ConstArray<Data>&
49 operator = (ConstArray<Data> const & other) = delete;
50
59 Data const & operator [] (int i) const;
60
69 void begin(ConstArrayIterator<Data>& iterator) const;
70
74 Data const * cArray() const;
75
81 bool isAllocated() const;
82
90 int size() const;
91
97 int capacity() const;
98
99 protected:
100
106 ConstArray();
107
111 ~ConstArray();
112
114 Data* data_;
115
118
119 };
120
121 /*
122 * Default constructor (protected).
123 */
124 template <typename Data> inline
126 : data_(nullptr),
127 capacity_(0)
128 {}
129
130 /*
131 * Destructor (do-hothing, and protected).
132 *
133 * Subclasses are responsible for memory management.
134 */
135 template <typename Data>
138
139 /*
140 * Get an element by const reference (C-array subscripting)
141 */
142 template <typename Data> inline
143 Data const & ConstArray<Data>::operator [] (int i) const
144 {
145 assert(data_);
146 assert(i >= 0 );
147 assert(i < capacity_);
148 return *(data_ + i);
149 }
150
151 /*
152 * Set a ConstArrayIterator to begin this ConstArray.
153 */
154 template <typename Data> inline
156 {
157 assert(data_);
158 assert(capacity_ > 0);
159 iterator.setCurrent(data_);
160 iterator.setEnd(data_ + capacity_);
161 }
162
163 /*
164 * Get a pointer to the underlying C array (pointer to const data).
165 */
166 template <typename Data> inline
167 Data const * ConstArray<Data>::cArray() const
168 { return data_; }
169
170 /*
171 * Return true iff the data pointer is non-null, false otherwise.
172 */
173 template <typename Data> inline
175 { return (bool)data_; }
176
177 /*
178 * Return logical size of this array.
179 */
180 template <typename Data> inline
182 { return capacity_; }
183
184 /*
185 * Return allocated capacity.
186 */
187 template <typename Data> inline
189 { return capacity_; }
190
191}
192#endif
Forward const iterator for an Array or a C array.
void setEnd(Data *ptr)
Set the value of the end pointer.
void setCurrent(Data *ptr)
Set the current pointer value.
~ConstArray()
Destructor (protected to prevent direct destruction).
Definition ConstArray.h:136
Data const & operator[](int i) const
Get an element by const reference.
Definition ConstArray.h:143
int size() const
Return logical size of this array.
Definition ConstArray.h:181
bool isAllocated() const
Does this array have associated data?
Definition ConstArray.h:174
int capacity_
Allocated size of the data_ array.
Definition ConstArray.h:117
int capacity() const
Return allocated size of this array.
Definition ConstArray.h:188
Data * data_
Pointer to an array of Data elements.
Definition ConstArray.h:114
ConstArray()
Constructor (protected to prevent direct instantiation).
Definition ConstArray.h:125
Data const * cArray() const
Return a pointer to the underlying C array (pointer to const data).
Definition ConstArray.h:167
void begin(ConstArrayIterator< Data > &iterator) const
Set a const iterator to begin this ConstArray.
Definition ConstArray.h:155
File containing preprocessor macros for error handling.
Utility classes for scientific computation.