casacore
Loading...
Searching...
No Matches
RecordRep.h
Go to the documentation of this file.
1// # RecordRep.h: The representation of a Record
2// # Copyright (C) 1996,1997,2000,2001,2005
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_RECORDREP_H
27#define CASA_RECORDREP_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Containers/Block.h>
32#include <casacore/casa/Containers/RecordDesc.h>
33#include <casacore/casa/Containers/RecordInterface.h>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// # Forward Declarations
38class AipsIO;
39class IPosition;
40class String;
41
42// <summary>
43// The representation of a Record
44// </summary>
45
46// <use visibility=local>
47// <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tRecord">
48// </reviewed>
49
50// <prerequisite>
51// <li> <linkto class="Record">Record</linkto>.
52// </prerequisite>
53//
54// <etymology>
55// RecordRep is the REPresentation of a Record.
56// </etymology>
57//
58// <synopsis>
59// RecordRep is the actual implementation of a Record object.
60// It contains the description and the data. The data is stored as
61// a collection of void* pointers to the actual data. By storing
62// it in this indirect way, it is easier to extend the data block.
63// It also means that RecordFieldPtr objects always have the correct
64// pointer and do not need to be adjusted when the data block is extended.
65// <p>
66// Despite the fact that the data pointers have type void*, the
67// functions are completely type safe. This is done by passing the
68// type around using the DataType enumeration. The downpart is that
69// only types from that enumeration are supported (but that is also
70// required by the RecordDesc mechanics).
71// <p>
72// Note that RecordRep does not know anything about RecordFieldPtr
73// objects pointing to its data. Only its mother class Record
74// knows about them and handles all cases where the RecordFieldPtr's
75// have to be notified.
76// </synopsis>
77//
78// <example>
79// RecordRep mirrors all functions in Record.
80// </example>
81//
82// <motivation>
83// Having a separate RecordRep class makes copy-on-write possible.
84// It also allows derivation of other RecordRep classes (like TableRecordRep),
85// while their mother classes are not derived from each other.
86// </motivation>
87//
88// <todo asof="1996/03/12">
89// <li> An implementation where arrays are stored as T*'s would cut down on
90// instantiations (the Arrayness would come back through the creation
91// of the <src>RecordFieldPtr<Array<T> ></src>).
92// </todo>
93
94class RecordRep {
95 public:
96 // Create a record with no fields.
98
99 // Create a record with the given description. If it is not possible to
100 // create all fields (for example, if a field of an unsupported type is
101 // requested), an exception is thrown.
102 // All fields are checked by the field checking function (if defined).
104
105 // Create a copy of other using copy semantics.
106 RecordRep(const RecordRep& other);
107
108 // Copy all the data over.
110
111 // Delete all data.
112 virtual ~RecordRep();
113
114 // Get the comment for this field.
115 const String& comment(Int whichField) const;
116
117 // Set the comment for this field.
118 void setComment(Int whichField, const String& comment);
119
120 // Describes the current structure of this Record.
121 const RecordDesc& description() const;
122
123 // Change the structure of this Record to contain the fields in
124 // newDescription. After calling restructure, <src>description() ==
125 // newDescription</src>.
126 void restructure(const RecordDesc& newDescription, Bool recursive);
127
128 // Returns True if this and other have the same RecordDesc, other
129 // than different names for the fields. That is, the number, type and the
130 // order of the fields must be identical (recursively for fixed
131 // structured sub-Records in this).
132 // <note role=caution>
133 // <src>thisRecord.conform(thatRecord) == True</src> does not imply
134 // <br><src>thatRecord.conform(thisRecord) == True</src>, because
135 // a variable record in one conforms a fixed record in that, but
136 // not vice-versa.
137 // </note>
138 Bool conform(const RecordRep& other) const;
139
140 // Copy all data of the Record.
141 void copyData(const RecordRep& other);
142
143 // Copy a data field.
144 // This can only handle scalars and arrays.
145 void copyDataField(DataType type, Int whichField, const void* that) const;
146
147 // Remove a field from the record.
148 void removeField(Int whichField);
149
150 // Rename the given field.
151 void renameField(const String& newName, Int whichField);
152
153 // Add a field with the given name and value to the record.
154 // The data type of the field is determined by the data type of the value.
155 // For arrays it is possible to define if the shape is fixed.
156 // <group>
157 void addDataField(const String& name, DataType type, const IPosition& shape, Bool fixedShape,
158 const void* data);
159 void addField(const String& name, const Record& value, RecordInterface::RecordType type);
160 // </group>
161
162 // Define a value for the given field.
163 // Array conformance rules will not be applied for variable shaped arrays.
164 // When the field and value data type mismatch, type promotion
165 // of scalars will be done if possible. If not possible, an exception
166 // is thrown.
167 void defineDataField(Int whichField, DataType type, const void* value);
168
169 // Put the description and data of the Record.
170 // It also puts the fixedFlag attribute (of the mother object).
171 void putRecord(AipsIO& os, int recordType) const;
172
173 // Get the description and data of the Record.
174 // It also gets the fixedFlag attribute (of the mother object).
176
177 // Put the data of a record.
178 // This is used to write a subrecord, whose description has
179 // already been written.
180 void putData(AipsIO& os) const;
181
182 // Read the data of a record.
183 // This is used to read a subrecord, whose description has
184 // already been read.
185 void getData(AipsIO& os, uInt version);
186
187 // Used by the RecordFieldPtr classes to attach in a type-safe way to the
188 // correct field.
189 // <group>
190 void* get_pointer(Int whichField, DataType type) const;
191 void* get_pointer(Int whichField, DataType type, const String& recordType) const;
192 // </group>
193
194 // Merge a field from another record into this record.
195 void mergeField(const RecordRep& other, Int whichFieldFromOther, RecordInterface::DuplicatesFlag);
196
197 // Merge all fields from the other record into this record.
198 void merge(const RecordRep& other, RecordInterface::DuplicatesFlag);
199
200 // Print a record.
201 // Print the contents of the record.
202 // Only the first <src>maxNrValues</src> of an array will be printed.
203 // A value < 0 means the entire array.
204 void print(std::ostream&, Int maxNrValues = 25, const String& indent = "") const;
205
206 protected:
207 // Utility functions to avoid code duplication in the public member
208 // functions.
209 // <group>
211 void copy_other(const RecordRep& other);
212 // </group>
213
214 // Get the field number for a given name.
215 virtual Int fieldNumber(const String& name) const;
216
217 // Add the data pointer to the data block.
218 // The block is extended when needed.
219 void addDataPtr(void* ptr);
220
221 // Remove a data pointer add the given index.
222 void removeDataPtr(Int index);
223
224 // Check if the shape of the data array matches the shape of a
225 // fixed-shaped array in the description.
226 void checkShape(DataType type, const IPosition& shape, const void* value,
227 const String& fieldName);
228
229 // Add a field to the description.
230 virtual void addFieldToDesc(const String& name, DataType type, const IPosition& shape,
231 Bool fixedShape);
232
233 // Remove a data field.
234 virtual void removeData(Int whichField, void* ptr, void* vecptr);
235
236 // Remove a field from the description.
237 virtual void removeFieldFromDesc(Int whichField);
238
239 // Create a data field for the given type and shape.
240 // This can only handle scalars and arrays.
241 void* createDataField(DataType type, const IPosition& shape);
242
243 // Delete a data field.
244 // This can only handle scalars and arrays.
245 void deleteDataField(DataType type, void* ptr, void* vecptr);
246
247 // Copy a data field.
248 // This can only handle scalars and arrays.
249 void copyDataField(DataType type, void* ptr, const void* that) const;
250
251 // Print a data field.
252 // This can only handle scalars and arrays.
253 void printDataField(std::ostream& os, DataType type, const String& indent, Int maxNrValues,
254 const void* ptr) const;
255
256 // Put a data field.
257 // This can only handle scalars and arrays.
258 void putDataField(AipsIO& os, DataType type, const void* ptr) const;
259
260 // Get a data field.
261 // This can only handle scalars and arrays.
262 void getDataField(AipsIO& os, DataType type, void* ptr);
263
264 // Make an array for a scalar data field.
265 // It shares the data, so a change in the data is reflected in the array.
266 // It is used to be able to access a scalar as an 1D array.
267 void makeDataVec(Int whichField, DataType type);
268
269 // Get a Scalar/ArrayKeywordSet object as a Record.
270 // (type 0 = ScalarKeywordSet; type 1 = ArrayKeywordSet).
271 void getKeySet(AipsIO& os, uInt version, uInt type);
272
273 // Get the description of a keyword set as a RecordDesc.
274 void getKeyDesc(AipsIO& os, RecordDesc& desc);
275
276 // Get the scalar values of a keyword set.
278
279 // Get the array values of a keyword set.
281
282 // Holds the structure of this Record.
284 // Pointers to data values.
286 // Pointers to a vector of a scalar (to access a scalar as an array).
288 // #Entries used in data_p.
290};
291
292inline const RecordDesc& RecordRep::description() const { return desc_p; }
293
294inline const String& RecordRep::comment(Int whichField) const { return desc_p.comment(whichField); }
295
296inline void RecordRep::setComment(Int whichField, const String& comment) {
297 desc_p.setComment(whichField, comment);
298}
299
300inline void RecordRep::renameField(const String& newName, Int whichField) {
301 desc_p.renameField(newName, whichField);
302}
303
304} // namespace casacore
305
306#endif
void merge(const RecordRep &other, RecordInterface::DuplicatesFlag)
Merge all fields from the other record into this record.
Block< void * > datavec_p
Pointers to a vector of a scalar (to access a scalar as an array).
Definition RecordRep.h:287
void getRecord(AipsIO &os, Int &recordType)
Get the description and data of the Record.
void delete_myself(uInt nfields)
Utility functions to avoid code duplication in the public member functions.
void deleteDataField(DataType type, void *ptr, void *vecptr)
Delete a data field.
void copyDataField(DataType type, void *ptr, const void *that) const
Copy a data field.
void renameField(const String &newName, Int whichField)
Rename the given field.
Definition RecordRep.h:300
void printDataField(std::ostream &os, DataType type, const String &indent, Int maxNrValues, const void *ptr) const
Print a data field.
void copyDataField(DataType type, Int whichField, const void *that) const
Copy a data field.
Bool conform(const RecordRep &other) const
Returns True if this and other have the same RecordDesc, other than different names for the fields.
virtual ~RecordRep()
Delete all data.
void addField(const String &name, const Record &value, RecordInterface::RecordType type)
void print(std::ostream &, Int maxNrValues=25, const String &indent="") const
Print a record.
void defineDataField(Int whichField, DataType type, const void *value)
Define a value for the given field.
RecordRep & operator=(const RecordRep &other)
Copy all the data over.
void putDataField(AipsIO &os, DataType type, const void *ptr) const
Put a data field.
void removeDataPtr(Int index)
Remove a data pointer add the given index.
void getDataField(AipsIO &os, DataType type, void *ptr)
Get a data field.
void mergeField(const RecordRep &other, Int whichFieldFromOther, RecordInterface::DuplicatesFlag)
Merge a field from another record into this record.
const RecordDesc & description() const
Describes the current structure of this Record.
Definition RecordRep.h:292
void getScalarKeys(AipsIO &os)
Get the scalar values of a keyword set.
void removeField(Int whichField)
Remove a field from the record.
RecordRep()
Create a record with no fields.
void getArrayKeys(AipsIO &os)
Get the array values of a keyword set.
void checkShape(DataType type, const IPosition &shape, const void *value, const String &fieldName)
Check if the shape of the data array matches the shape of a fixed-shaped array in the description.
void makeDataVec(Int whichField, DataType type)
Make an array for a scalar data field.
void copy_other(const RecordRep &other)
virtual void removeFieldFromDesc(Int whichField)
Remove a field from the description.
void * get_pointer(Int whichField, DataType type, const String &recordType) const
void * get_pointer(Int whichField, DataType type) const
Used by the RecordFieldPtr classes to attach in a type-safe way to the correct field.
const String & comment(Int whichField) const
Get the comment for this field.
Definition RecordRep.h:294
void getData(AipsIO &os, uInt version)
Read the data of a record.
RecordRep(const RecordRep &other)
Create a copy of other using copy semantics.
void copyData(const RecordRep &other)
Copy all data of the Record.
void restructure(const RecordDesc &newDescription, Bool recursive)
Change the structure of this Record to contain the fields in newDescription.
virtual Int fieldNumber(const String &name) const
Get the field number for a given name.
void getKeyDesc(AipsIO &os, RecordDesc &desc)
Get the description of a keyword set as a RecordDesc.
void putRecord(AipsIO &os, int recordType) const
Put the description and data of the Record.
Block< void * > data_p
Pointers to data values.
Definition RecordRep.h:285
void setComment(Int whichField, const String &comment)
Set the comment for this field.
Definition RecordRep.h:296
RecordDesc desc_p
Holds the structure of this Record.
Definition RecordRep.h:283
void addDataPtr(void *ptr)
Add the data pointer to the data block.
void addDataField(const String &name, DataType type, const IPosition &shape, Bool fixedShape, const void *data)
Add a field with the given name and value to the record.
void getKeySet(AipsIO &os, uInt version, uInt type)
Get a Scalar/ArrayKeywordSet object as a Record.
virtual void addFieldToDesc(const String &name, DataType type, const IPosition &shape, Bool fixedShape)
Add a field to the description.
void * createDataField(DataType type, const IPosition &shape)
Create a data field for the given type and shape.
virtual void removeData(Int whichField, void *ptr, void *vecptr)
Remove a data field.
void putData(AipsIO &os) const
Put the data of a record.
RecordRep(const RecordDesc &description)
Create a record with the given description.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
uInt nfields() const override
How many fields does this structure have?
unsigned int uInt
Definition aipstype.h:49
IPosition shape(const RecordFieldId &) const
Get the actual shape of this field.
String name() const
Return the name of the field.
RecordType & recordType()
Give access to the RecordType flag (write-access is needed when a record is read back).
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
const String & comment() const
Get the comment of this field.