NIST Biometric Evaluation Framework
Software components for biometric technology evaluations
be_memory_autobuffer.h
Go to the documentation of this file.
1/*
2 * This software was developed at the National Institute of Standards and
3 * Technology (NIST) by employees of the Federal Government in the course
4 * of their official duties. Pursuant to title 17 Section 105 of the
5 * United States Code, this software is not subject to copyright protection
6 * and is in the public domain. NIST assumes no responsibility whatsoever for
7 * its use by other parties, and makes no guarantees, expressed or implied,
8 * about its quality, reliability, or any other characteristic.
9 */
10
11#ifndef __BE_MEMORY_AUTOBUFFER__
12#define __BE_MEMORY_AUTOBUFFER__
13
14#include <be_error_exception.h>
15
16namespace BiometricEvaluation {
17
18 namespace Memory {
19 template<class T>
20 class AutoBuffer {
21
54 public:
55
56 using value_type = T;
57
58 using reference = T&;
59 using const_reference = const T&;
60
61 operator T*();
63
65
67 /* You already have an allocated buffer */
68 AutoBuffer(T* data);
69 /* Constructor, Destructor, Copy Copy Constructor */
70 AutoBuffer(int (*ctor)(T**), void (*dtor)(T*),
71 int (*copyCtor)(T**, T*)=nullptr);
72 AutoBuffer(const AutoBuffer& copy);
73
75
76 private:
77
78 /* Pointer to allocated data */
79 T* _data;
80 /* Allocator function pointer */
81 int (*_ctor)(T**);
82 /* Destructor function pointer */
83 void (*_dtor)(T*);
84 /* Copy constructor function pointer */
85 int (*_copyCtor)(T**, T*);
86 /*
87 * True if we passed in preallocated data. Don't
88 * perform any memory management, just keep track of
89 * the pointer.
90 */
91 bool _handsOff;
92
93 };
94 }
95}
96
97/******************************************************************************/
98/* Implementation. */
99/******************************************************************************/
100
101/******************************************************************************/
102/* Method implementations. */
103/******************************************************************************/
104
105/*
106 * Operators.
107 */
108template<class T>
110{
111 return _data;
112}
113
114template<class T>
116{
117 return _data;
118}
119
120template<class T>
124{
125 if (this != &copy) {
126 /* Copy function pointers, they aren't changing */
127 _ctor = copy._ctor;
128 _dtor = copy._dtor;
129 _copyCtor = copy._copyCtor;
130 _handsOff = copy._handsOff;
131
132 if (_handsOff)
133 /* Just copy the pointer */
134 _data = copy._data;
135 else {
136 /*
137 * Use copy constructor on the allocated memory to
138 * duplicate.
139 */
140 if (_copyCtor == nullptr)
141 throw Error::ParameterError("Copy "
142 "constructor is nullptr");
143 if ((_copyCtor)(&_data, copy._data) != 0)
144 throw Error::DataError("Data could not be "
145 "allocated");
146 }
147 }
148
149 return *this;
150}
151
152/******************************************************************************/
153/* Constructors. */
154/******************************************************************************/
155template<class T>
157{
158 _data = nullptr;
159 _handsOff = true;
160}
161
162template<class T>
164 int (*ctor)(T**),
165 void (*dtor)(T*),
166 int (*copyCtor)(T**, T*))
167{
168 if (ctor != nullptr)
169 _ctor = ctor;
170 else
171 throw Error::ParameterError("Allocator is nullptr");
172
173 if (dtor != nullptr)
174 _dtor = dtor;
175 else
176 throw Error::ParameterError("Destructor is nullptr");
177
178 /* Don't require copy constructor, user might never make copy */
179 _copyCtor = copyCtor;
180
181 /* Initial allocation the data */
182 if ((_ctor)(&_data) != 0)
183 throw Error::DataError("Data could not be allocated");
184
185 _handsOff = false;
186}
187
188template<class T>
190{
191 /*
192 * With this constructor, the AutoBuffer is essentially nothing more
193 * than a bloated pointer. The caller still must free memory manually.
194 * This just allows for uniform usage in classes that can take an
195 * allocated buffer or can create one.
196 */
197 _data = data;
198 _handsOff = true;
199}
200
201template<class T>
203{
204 /* Copy function pointers, they aren't changing */
205 _ctor = copy._ctor;
206 _dtor = copy._dtor;
207 _copyCtor = copy._copyCtor;
208 _handsOff = copy._handsOff;
209
210 if (_handsOff)
211 /* Just copy the pointer and pray the user hasn't freed it */
212 _data = copy._data;
213 else {
214 /* Use copy constructor on the allocated memory to duplicate */
215 if (_copyCtor == nullptr)
216 throw Error::ParameterError("Copy constructor is "
217 "nullptr");
218
219 if ((_copyCtor)(&_data, copy._data) != 0)
220 throw Error::DataError("Data could not be "
221 "allocated");
222 }
223}
224
225/******************************************************************************/
226/* Destructor. */
227/******************************************************************************/
228template<class T>
230{
231 if (!_handsOff)
232 (_dtor)(_data);
233}
234
235#endif /* __BE_MEMORY_AUTOBUFFER__ */
Error when reading data from an external source.
An invalid parameter was passed to a constructor or method.
AutoBuffer(int(*ctor)(T **), void(*dtor)(T *), int(*copyCtor)(T **, T *)=nullptr)
AutoBuffer & operator=(const AutoBuffer &other)
This software was developed at the National Institute of Standards and Technology (NIST) by employees...