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