casacore
Loading...
Searching...
No Matches
RecordDescRep.h
Go to the documentation of this file.
1//# RecordDescRep.h: Representation of a RecordDesc
2//# Copyright (C) 1996,1997,1998,1999,2000,2001
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
27#ifndef CASA_RECORDDESCREP_H
28#define CASA_RECORDDESCREP_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Utilities/DataType.h>
33#include <casacore/casa/Containers/Block.h>
34#include <casacore/casa/Arrays/IPosition.h>
35#include <casacore/casa/iosfwd.h>
36#include <map>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40//# Forward Declarations
41class RecordDesc;
42class AipsIO;
43
44
45// <summary>
46// Representation of a RecordDesc
47// </summary>
48
49// <use visibility=local>
50// <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tRecordDesc">
51// </reviewed>
52
53// <prerequisite>
54// <li> <linkto group="DataType.h#DataType">DataType</linkto>
55// <li> <linkto class="RecordDesc">RecordDesc</linkto>
56// </prerequisite>
57
58// <etymology>
59// Rep is an often used abbreviation for representation.
60// Thus RecordDescRep is the representation of a RecordDesc.
61// </etymology>
62
63// <synopsis>
64// RecordDescRep is used by RecordDesc
65// to implement its copy-on-write semantics. RecordDesc is the interface
66// to the user, while RecordDescRep contains the actual implementation.
67// See <linkto class=RecordDesc>RecordDesc</linkto> for a more detailed
68// description of a record description.
69// </synopsis>
70
71// <example>
72// See the example in the description of the
73// <linkto class="Record:example1">Record</linkto> class.
74// </example>
75
76// <motivation>
77// RecordDescRep is needed to make copy-on-write semantics possible in
78// class RecordDesc.
79// </motivation>
80
81// <todo asof="1995/06/01">
82// <li> Should the strategy wrt. field names be changed (not used in
83// field description equality, must be unique at a given level?).
84// <li> Perhaps we should be able to more conveniently change the description
85// of an existing field.
86// </todo>
87
89{
90public:
91 // Create a description with no fields.
93
94 // Create a description which is a copy of other.
96
97 // Replace this description with other.
99
100 virtual ~RecordDescRep();
101
102 // Add scalar or array field. If of array type, the shape is set to [-1],
103 // which indicates a variable sized array. Returns the number of fields in
104 // the description.
105 uInt addField (const String& fieldName, DataType scalarOrArrayType);
106
107 // Add an array field of the indicated type. The DataType is promoted
108 // from a scalar type to an array type if necessary, e.g.,
109 // <src>TpInt ->TpArrayInt</src>. Returns the number of fields in
110 // the description.
111 uInt addArray (const String& fieldName, DataType scalarOrArrayType,
112 const IPosition& shape);
113
114 // Add a Record field to the description. This allows hierarchical
115 // descriptions to be developed. Returns the number of fields in the
116 // description.
117 uInt addRecord (const String& fieldName, const RecordDesc& subDesc);
118
119 // Add a Table field to the description. The Table description has the
120 // given name. Returns the number of fields in the description.
121 uInt addTable (const String& fieldName, const String& tableDescName);
122
123 // Get the comment for this field.
124 const String& comment (Int whichField) const;
125
126 // Set the comment for this field.
127 void setComment (Int whichField, const String& comment);
128
129 // Set the shape for this field.
130 // An exception will be thrown if the field is no array.
131 void setShape (Int whichField, const IPosition& shape);
132
133 // Merge a single field from other. If allowDuplicates is True, silently
134 // throw away fields if one with the same name and type already exists,
135 // otherwise an exception is thrown. Conflicting types always cause an
136 // exception. Returns the number of fields in the description.
137 uInt mergeField (const RecordDescRep& other, Int whichFieldFromOther,
138 int duplicateAction);
139
140 // Add all the fields from another RecordDescRep to the current objects.
141 uInt merge (const RecordDescRep& other, int duplicateAction);
142
143 // Remove the given field from the description.
144 virtual uInt removeField (Int whichField);
145
146 // Rename the given field.
147 virtual void renameField (const String& newName, Int whichField);
148
149 // Returns the index of the field named fieldName. Returns -1 if fieldName
150 // does not exist.
151 Int fieldNumber (const String& fieldName) const;
152
153 // Number of fields in the description.
154 uInt nfields() const;
155
156 // What is the type of the given field. Returns TpRecord if the field is
157 // a sub-Record.
158 DataType type (Int whichField) const;
159
160 // What is the name of the given field.
161 const String& name (Int whichField) const;
162
163 // Create a name for a field defined by index as *i (similar to glish).
164 // It takes care that the resulting name is unique by adding a suffix _j
165 // when needed.
166 String makeName (Int whichField) const;
167
168 // Make the given name unique by adding a suffix _j when needed.
169 // j is the minimal number needed to make it unique.
170 String uniqueName (const String& name) const;
171
172 // Returns True if whichField is an array.
173 Bool isArray (Int whichField) const;
174
175 // Returns True if whichField is a scalar.
176 Bool isScalar (Int whichField) const;
177
178 // Returns True if whichField is a sub-record.
179 Bool isSubRecord (Int whichField) const;
180
181 // Returns True if whichField is a table.
182 Bool isTable (Int whichField) const;
183
184 // What is the shape of the given field. Returns [1] if the field is a
185 // scalar, table or, sub-record, [-1] if it is a variable length array,
186 // and the actual shape for a fixed length array.
187 const IPosition& shape (Int whichField) const;
188
189 // What is the name of the table description associated with a table.
190 const String& tableDescName (Int whichField) const;
191
192 // If whichField is a sub-record with a description,
193 // return its description. Otherwise an exception is thrown.
194 // <group>
195 const RecordDesc& subRecord (Int whichField) const;
196 RecordDesc& subRecord (Int whichField);
197 // </group>
198
199 // <group>
200 // This and other compare equal if the field types and shapes are identical
201 // (recursively if there are described sub-records or tables).
202 // The field names are not used.
203 Bool operator== (const RecordDescRep& other) const;
204 Bool operator!= (const RecordDescRep& other) const;
205 // </group>
206
207 // Test if this description conforms the other.
208 // It is similar to operator==. However, a subrecord in that description
209 // always conforms an arbitrary (i.e. empty) subrecord in this
210 // description.
211 // <br>This is used by Record, to see if another record can be assigned
212 // to this record.
213 Bool conform (const RecordDescRep& other) const;
214
215 // Test if this description equals another one.
216 // It is equal if the number of fields is equal and all field names in
217 // this description occur in the other too. The order of the fields
218 // is not important.
219 // <br>The flag equalDataTypes is set to True if the data types
220 // of all fields match.
221 // <br>Use function operator== if order and types are important,
222 // but names are not.
223 Bool isEqual (const RecordDescRep& other, Bool& equalDataTypes) const;
224
225 // Test if this description is a subset of another one.
226 // It is similar to isEqual above.
227 Bool isSubset (const RecordDescRep& other, Bool& equalDataTypes) const;
228
229 // Test if this description is a strict subset of another one, thus
230 // if it is a subset and not equal.
232 Bool& equalDataTypes) const;
233
234 // Test if the set of field names in this and other record description
235 // is disjoint (i.e. if they do not share names).
236 Bool isDisjoint (const RecordDescRep& other) const;
237
238protected:
239 // Add a field name and its type.
240 // It checks if the name is unique and it extends the various blocks
241 // using increment_length.
242 void addFieldName (const String& fieldName, DataType type);
243
244 // Add a field from another Record description.
245 // This is used by the merge functions.
246 virtual void addRepField (const RecordDescRep& other,
247 const String& newName, Int whichField);
248
249 // Add the field info. These are helper functions for the add functions
250 // and can be used in derived classes too.
251 // <group>
252 void addFieldAny (DataType scalarOrArrayType);
253 void addFieldArray (DataType scalarOrArrayType, const IPosition& shape);
254 // </group>
255
256 // Set the shape (for a derived class).
257 void setShape (const IPosition& shape, Int whichField);
258
259 // Helper functions
260 // <group>
261 virtual void increment_length();
262 void copy_other (const RecordDescRep& other);
263 // </group>
264
265private:
266 // Test if all fields are part of the other description.
267 // The flag equalDataTypes is set to True if the data types of the
268 // fields in both descriptions are the same.
269 Bool allExist (const RecordDescRep&, Bool& equalDataTypes) const;
270
271 // Number of fields in the description.
273 // The DataType of each field.
275 // The name of each field.
277 // The description of the subrecords. Null if the field is not a subrecord.
278 // This isn't the most efficient representation. If this is ever an issue
279 // we could calculate these, or store them in one Block, or implement
280 // copy-on-write semantics.
282 // The shape of the field [1] for scalars and sub-records.
284 // True if the corresponding field is an array.
286 // Table description name for table fields.
288 // Comments for each field.
290 // Mapping of field name to field number.
291 std::map<String,Int> name_map_p;
292};
293
295{
296 return n_p;
297}
298
299inline DataType RecordDescRep::type (Int whichField) const
300{
301 return DataType(types_p[whichField]);
302}
303
304inline const String& RecordDescRep::name (Int whichField) const
305{
306 return names_p[whichField];
307}
308
309inline const IPosition& RecordDescRep::shape (Int whichField) const
310{
311 return shapes_p[whichField];
312}
313
314inline Bool RecordDescRep::isArray (Int whichField) const
315{
316 return is_array_p[whichField];
317}
318
319inline Bool RecordDescRep::isScalar (Int whichField) const
320{
321 return isScalarFun (DataType(types_p[whichField]));
322}
323
324inline Bool RecordDescRep::isSubRecord (Int whichField) const
325{
326 return (types_p[whichField] == TpRecord);
327}
328
329inline Bool RecordDescRep::isTable (Int whichField) const
330{
331 return (types_p[whichField] == TpTable);
332}
333
334inline const RecordDesc& RecordDescRep::subRecord (Int whichField) const
335{
336 //# The cast to non-const is completely safe.
337 return ((RecordDescRep*)this)->subRecord (whichField);
338}
339
340inline const String& RecordDescRep::tableDescName (Int whichField) const
341{
342 return tableDescNames_p[whichField];
343}
344
345
346
347} //# NAMESPACE CASACORE - END
348
349#endif
simple 1-D array
Definition Block.h:198
A drop-in replacement for Block<T*>.
Definition Block.h:812
String uniqueName(const String &name) const
Make the given name unique by adding a suffix _j when needed.
Bool isDisjoint(const RecordDescRep &other) const
Test if the set of field names in this and other record description is disjoint (i....
const RecordDesc & subRecord(Int whichField) const
If whichField is a sub-record with a description, return its description.
uInt addArray(const String &fieldName, DataType scalarOrArrayType, const IPosition &shape)
Add an array field of the indicated type.
RecordDesc & subRecord(Int whichField)
Bool isArray(Int whichField) const
Returns True if whichField is an array.
uInt addRecord(const String &fieldName, const RecordDesc &subDesc)
Add a Record field to the description.
virtual uInt removeField(Int whichField)
Remove the given field from the description.
void setComment(Int whichField, const String &comment)
Set the comment for this field.
void addFieldArray(DataType scalarOrArrayType, const IPosition &shape)
Block< String > tableDescNames_p
Table description name for table fields.
uInt mergeField(const RecordDescRep &other, Int whichFieldFromOther, int duplicateAction)
Merge a single field from other.
Bool isEqual(const RecordDescRep &other, Bool &equalDataTypes) const
Test if this description equals another one.
RecordDescRep(const RecordDescRep &other)
Create a description which is a copy of other.
uInt n_p
Number of fields in the description.
Bool operator==(const RecordDescRep &other) const
This and other compare equal if the field types and shapes are identical (recursively if there are de...
Bool isSubset(const RecordDescRep &other, Bool &equalDataTypes) const
Test if this description is a subset of another one.
Bool allExist(const RecordDescRep &, Bool &equalDataTypes) const
Test if all fields are part of the other description.
const IPosition & shape(Int whichField) const
What is the shape of the given field.
Block< String > names_p
The name of each field.
void setShape(Int whichField, const IPosition &shape)
Set the shape for this field.
std::map< String, Int > name_map_p
Mapping of field name to field number.
void addFieldName(const String &fieldName, DataType type)
Add a field name and its type.
void setShape(const IPosition &shape, Int whichField)
Set the shape (for a derived class).
Block< String > comments_p
Comments for each field.
uInt addField(const String &fieldName, DataType scalarOrArrayType)
Add scalar or array field.
const String & name(Int whichField) const
What is the name of the given field.
Int fieldNumber(const String &fieldName) const
Returns the index of the field named fieldName.
virtual void addRepField(const RecordDescRep &other, const String &newName, Int whichField)
Add a field from another Record description.
String makeName(Int whichField) const
Create a name for a field defined by index as *i (similar to glish).
Bool operator!=(const RecordDescRep &other) const
virtual void renameField(const String &newName, Int whichField)
Rename the given field.
Bool isScalar(Int whichField) const
Returns True if whichField is a scalar.
const String & tableDescName(Int whichField) const
What is the name of the table description associated with a table.
uInt merge(const RecordDescRep &other, int duplicateAction)
Add all the fields from another RecordDescRep to the current objects.
uInt nfields() const
Number of fields in the description.
Bool isTable(Int whichField) const
Returns True if whichField is a table.
Block< Int > types_p
The DataType of each field.
Bool isSubRecord(Int whichField) const
Returns True if whichField is a sub-record.
RecordDescRep & operator=(const RecordDescRep &other)
Replace this description with other.
RecordDescRep()
Create a description with no fields.
uInt addTable(const String &fieldName, const String &tableDescName)
Add a Table field to the description.
void copy_other(const RecordDescRep &other)
Bool conform(const RecordDescRep &other) const
Test if this description conforms the other.
virtual void increment_length()
Helper functions.
void addFieldAny(DataType scalarOrArrayType)
Add the field info.
Block< IPosition > shapes_p
The shape of the field [1] for scalars and sub-records.
PtrBlock< RecordDesc * > sub_records_p
The description of the subrecords.
Block< Bool > is_array_p
True if the corresponding field is an array.
Bool isStrictSubset(const RecordDescRep &other, Bool &equalDataTypes) const
Test if this description is a strict subset of another one, thus if it is a subset and not equal.
const String & comment(Int whichField) const
Get the comment for this field.
DataType type(Int whichField) const
What is the type of the given field.
const RecordDesc & subRecord(Int whichField) const
If whichField is a sub-record return its description.
Definition RecordDesc.h:444
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40