casacore
Loading...
Searching...
No Matches
RecordField.h
Go to the documentation of this file.
1// # RecordField.h: Access to an individual field in a record
2// # Copyright (C) 1995,1996,1997
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_RECORDFIELD_H
27#define CASA_RECORDFIELD_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Containers/Record.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// # Forward Declarations
36class TableRecord;
37class Table;
38
39// <summary>
40// Access to an individual field in a record.
41// </summary>
42
43// <use visibility=export>
44// <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tRecord">
45// </reviewed>
46
47// <prerequisite>
48// <li> <linkto class="RecordInterface">RecordInterface</linkto>.
49// </prerequisite>
50
51// <etymology>
52// RecordFieldPtr indicates that an object of this type is
53// pointing to a field in a record.
54// </etymology>
55
56// <synopsis>
57// RecordFieldPtr allows access to the fields in a record object.
58// A record object is an object of a class derived from
59// <linkto class=RecordInterface>RecordInterface</linkto>.
60// <src>RecordFieldPtr<T></src> objects can only be instantiated for types `T'
61// which are valid fields of a record object (e.g. Int, float, String,
62// Record, TableRecord). It can, however, NOT be instantiated for
63// a Table field, because Table fields are accessed indirectly via a
64// TableKeyword object. Table fields have to be accessed directly
65// through the <linkto class=TableRecord>TableRecord</linkto> interface.
66// <p>
67// Internally, a RecordFieldPtr stores a Record pointer and
68// field number. Therefore, if the order of fields in a Record is modified,
69// or if the Record is restructured, a RecordFieldPtr is invalidated and should
70// no longer be used.
71// <p>
72// The RecordFieldPtr is pointer-like in the sense that it points to an
73// object that is physically inside of another object (the enclosing
74// record object).
75// Access to the value is obtained via the dereference operator
76// (<src>operator*()</src>) to emphasize the pointer like nature of these
77// classes.
78// <br>
79// An alternative way to get access to the values is using the
80// functions define and get. Note that in
81// <srcblock>
82// RecordFieldPtr<Array<Int> > field (record, fieldNumber);
83// Array<Int> value;
84// *field = value;
85// field.define (value);
86// </srcblock>
87// the assignment (in line 3) and define (in line 4) are not equivalent.
88// The assignment uses the normal Array assignment, thus it takes the
89// Array conformance rules into account (an assign is only possible when
90// the new array value conforms the current array value or when the current
91// array value is empty).
92// On the other hand, define does not take the current array value into
93// account. Thus an array value can always be redefined.
94// <br>
95// However, note that if the field is defined with a non-fixed shape in
96// the record description, a value must always conform that shape (in
97// case of assignment as well as in case of define).
98// </synopsis>
99
100// <example>
101// See the example in the <linkto class="Record">Record</linkto> class.
102// </example>
103
104// <motivation>
105// RecordFieldPtr provides a fast way to access the data in a record.
106// </motivation>
107
108template <class T>
109class RecordFieldPtr {
110 public:
111 // This object does not point to any field, i.e.
112 // <src>this->isAttached() == False;</src>
114
115 // Attach this field pointer to the given field. If it does not exist
116 // an exception is thrown.
117 // <group>
118 RecordFieldPtr(RecordInterface& record, Int whichField);
120 // </group>
121
122 // Change our pointer to the supplied field. If it doesn't exist an
123 // exception is thrown.
124 // <group>
125 void attachToRecord(RecordInterface& record, Int whichField);
127 // </group>
128
129 // Point to no field in any Record.
130 void detach();
131
132 // Provide access to the field's value.
133 // <note>
134 // To be sure a const function is called, it is best to use get().
135 // For a non-const object, a non-const function is called, even if
136 // used as an rvalue.
137 // </note>
138 // <group>
139 T& operator*();
140 const T& operator*() const { return get(); }
141 const T& get() const { return *get_typed_ptr(parent_p, fieldNumber_p); }
142 // </group>
143
144 // Store a value in the field using redefinition.
145 // Define differs from assignment w.r.t. arrays.
146 // For define a variable shaped array is deleted first with the
147 // effect that array conformance rules are not applied for them.
148 void define(const T& value);
149
150 // Get the comment of this field.
151 const String& comment() const;
152
153 // Set the comment for this field.
155
156 // Return the fieldnumber of this field.
157 Int fieldNumber() const { return fieldNumber_p; }
158
159 // Return the name of the field.
160 String name() const { return parent_p->name(fieldNumber_p); }
161
162 // Is this field pointer attached to a valid record? Operations which
163 // might cause it to become detached are:
164 // <ol>
165 // <li> Destruction of the Record
166 // <li> Restructuring of the record.
167 // <li> Explicit call of the detach() member.
168 // </ol>
169 // # This inherited function is shown for documentation purposes.
170 Bool isAttached() const { return parent_p; }
171
172 private:
173 static const T* get_typed_ptr(RecordInterface* record, Int fieldNumber);
174
177};
178
179// <summary>
180// Read-Only access to an individual field from a Record.
181// </summary>
182
183// <use visibility=export>
184// <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tRecord">
185// </reviewed>
186
187// <prerequisite>
188// <li> <linkto class="RecordFieldPtr">RecordRecordFieldPtr</linkto>.
189// </prerequisite>
190//
191// <synopsis>
192// This class is entirely like <linkto class="RecordFieldPtr">
193// RecordFieldPtr</linkto>, except that it only allows Read-Only
194// access to fields in a Record. The documentation for that class should
195// be consulted.
196// <p>
197// Note that RecordFieldPtr is not inherited from RORecordFieldPtr,
198// because that would give problems with the function attachToRecord.
199// It would allow RecordFieldPtr to attach to a const RecordInterface object.
200// </synopsis>
201
202template <class T>
204 public:
206 RORecordFieldPtr(const RecordInterface& record, Int whichField)
207 : fieldPtr_p((RecordInterface&)record, whichField) {}
209 : fieldPtr_p((RecordInterface&)record, id) {}
213 fieldPtr_p = other.fieldPtr_p;
214 return *this;
215 }
216
218
219 void attachToRecord(const RecordInterface& record, Int whichField) {
220 fieldPtr_p.attachToRecord((RecordInterface&)record, whichField);
221 }
222 void attachToRecord(const RecordInterface& record, const RecordFieldId& id) {
223 fieldPtr_p.attachToRecord((RecordInterface&)record, id);
224 }
225
226 const T& operator*() const { return *fieldPtr_p; }
227 const T& get() const { return fieldPtr_p.get(); }
228
229 const String& comment() const { return fieldPtr_p.comment(); }
230
231 Int fieldNumber() const { return fieldPtr_p.fieldNumber(); }
232
233 void detach() { fieldPtr_p.detach(); }
234 Bool isAttached() const { return fieldPtr_p.isAttached(); }
235
236 private:
238};
239
240} // namespace casacore
241
242#ifndef CASACORE_NO_AUTO_TEMPLATES
243#include <casacore/casa/Containers/RecordField.tcc>
244#endif // # CASACORE_NO_AUTO_TEMPLATES
245#endif
const T & get() const
RORecordFieldPtr(const RecordInterface &record, const RecordFieldId &id)
const String & comment() const
const T & operator*() const
void attachToRecord(const RecordInterface &record, const RecordFieldId &id)
RORecordFieldPtr(const RORecordFieldPtr< T > &other)
RORecordFieldPtr(const RecordInterface &record, Int whichField)
RecordFieldPtr< T > fieldPtr_p
RORecordFieldPtr< T > & operator=(const RORecordFieldPtr< T > &other)
RORecordFieldPtr(const RecordFieldPtr< T > &other)
void attachToRecord(const RecordInterface &record, Int whichField)
String: the storage and methods of handling collections of characters.
Definition String.h:355
RecordFieldPtr()
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
Int fieldNumber_p
void setComment(const RecordFieldId &, const String &comment) override
Set the comment for this field.
Int fieldNumber() const
Return the fieldnumber of this field.
Int fieldNumber(const String &fieldName) const override
Get the field number from the field name.
String name() const
Return the name of the field.
void detach()
Point to no field in any Record.
const String & comment(const RecordFieldId &) const override
Get the comment for this field.
void attachToRecord(RecordInterface &record, Int whichField)
Change our pointer to the supplied field.
T & operator*()
Provide access to the field's value.
void define(const T &value)
Store a value in the field using redefinition.
RecordInterface()
The default constructor creates an empty record with a variable structure.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
static const T * get_typed_ptr(RecordInterface *record, Int fieldNumber)
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360
Bool isAttached() const
Is this field pointer attached to a valid record?
const T & get() const
const String & comment() const
Get the comment of this field.
RecordRep * parent_p
The parent Record.
Definition Record.h:417