casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RecordField.h
Go to the documentation of this file.
00001 //# RecordField.h: Access to an individual field in a record
00002 //# Copyright (C) 1995,1996,1997
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: RecordField.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00028 
00029 
00030 #ifndef CASA_RECORDFIELD_H
00031 #define CASA_RECORDFIELD_H
00032 
00033 //# Includes
00034 #include <casa/aips.h>
00035 #include <casa/Containers/Record.h>
00036 #include <casa/Utilities/Notice.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward Declarations
00041 class TableRecord;
00042 class Table;
00043 
00044 
00045 // <summary>
00046 // Access to an individual field in a record.
00047 // </summary>
00048 
00049 // <use visibility=export>
00050 // <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tRecord">
00051 // </reviewed>
00052 
00053 // <prerequisite>
00054 //   <li> <linkto class="RecordInterface">RecordInterface</linkto>.
00055 // </prerequisite>
00056 
00057 // <etymology>
00058 // RecordFieldPtr indicates that an object of this type is
00059 // pointing to a field in a record.
00060 // </etymology>
00061 
00062 // <synopsis>
00063 // RecordFieldPtr allows access to the fields in a record object.
00064 // A record object is an object of a class derived from
00065 // <linkto class=RecordInterface>RecordInterface</linkto>.
00066 // <src>RecordFieldPtr<T></src> objects can only be instantiated for types `T'
00067 // which are valid fields of a record object (e.g. Int, float, String,
00068 // Record, TableRecord). It can, however, NOT be instantiated for
00069 // a Table field, because Table fields are accessed indirectly via a
00070 // TableKeyword object. Table fields have to be accessed directly
00071 // through the <linkto class=TableRecord>TableRecord</linkto> interface.
00072 // <p>
00073 // The RecordFieldPtr is pointer-like in the sense that it points to an
00074 // object that is physically inside of another object (the enclosing
00075 // record object).
00076 // Access to the value is obtained via the dereference operator
00077 // (<src>operator*()</src>) to emphasize the pointer like nature of these
00078 // classes.
00079 // <br>
00080 // An alternative way to get access to the values is using the
00081 // functions define and get. Note that in
00082 // <srcblock>
00083 //    RecordFieldPtr<Array<Int> > field (record, fieldNumber);
00084 //    Array<Int> value;
00085 //    *field = value;
00086 //    field.define (value);
00087 // </srcblock>
00088 // the assignment (in line 3) and define (in line 4) are not equivalent.
00089 // The assignment uses the normal Array assignment, thus it takes the
00090 // Array conformance rules into account (an assign is only possible when
00091 // the new array value conforms the current array value or when the current
00092 // array value is empty).
00093 // On the other hand, define does not take the current array value into
00094 // account. Thus an array value can always be redefined.
00095 // <br>
00096 // However, note that if the field is defined with a non-fixed shape in
00097 // the record description, a value must always conform that shape (in
00098 // case of assignment as well as in case of define).
00099 // <p>
00100 // RecordFieldPtr is derived from NoticeTarget to get messages from
00101 // the mother record class when it changes. For example, when the
00102 // record is destructed, all RecordFieldPtr's pointing to that record
00103 // will automatically be detached.
00104 // </synopsis>
00105 
00106 // <example>
00107 // See the example in the <linkto class="Record">Record</linkto> class.
00108 // </example>
00109 
00110 // <motivation>
00111 // RecordFieldPtr provides a fast way to access the data in a record.
00112 // </motivation>
00113 
00114 //# <todo asof="1995/06/15">
00115 //# </todo>
00116 
00117 
00118 template<class T> class RecordFieldPtr : public NoticeTarget
00119 {
00120 public:
00121     // This object does not point to any field, i.e. 
00122     // <src>this->isAttached() == False;</src>
00123     RecordFieldPtr();
00124 
00125     // Attach this field pointer to the given field. If it does not exist
00126     // an exception is thrown.
00127     // <group>
00128     RecordFieldPtr (RecordInterface& record, Int whichField);
00129     RecordFieldPtr (RecordInterface& record, const RecordFieldId&);
00130     // </group>
00131 
00132     // After calling, this and other point to the same field, i.e. it
00133     // uses reference semantics.
00134     // <group>
00135     RecordFieldPtr (const RecordFieldPtr<T>& other);
00136     RecordFieldPtr<T>& operator= (const RecordFieldPtr<T>& other);
00137     // </group>
00138 
00139     ~RecordFieldPtr();
00140 
00141     // Change our pointer to the supplied field. If it doesn't exist an
00142     // exception is thrown.
00143     // <group>
00144     void attachToRecord (RecordInterface& record, Int whichField);
00145     void attachToRecord (RecordInterface& record, const RecordFieldId&);
00146     // </group>
00147 
00148     // Point to no field in any Record.
00149     void detach();
00150 
00151     // Provide access to the field's value.
00152     // <note>
00153     // To be sure a const function is called, it is best to use get().
00154     // For a non-const object, a non-const function is called, even if
00155     // used as an rvalue.
00156     // </note>
00157     // <group>
00158     T& operator*();
00159     const T& operator*() const {return *fieldPtr_p;}
00160     const T& get() const {return *fieldPtr_p;}
00161     // </group>
00162 
00163     // Store a value in the field using redefinition.
00164     // Define differs from assignment w.r.t. arrays.
00165     // For define a variable shaped array is deleted first with the
00166     // effect that array conformance rules are not applied for them.
00167     void define (const T& value);
00168 
00169     // Get the comment of this field.
00170     const String& comment() const;
00171 
00172     // Set the comment for this field.
00173     void setComment (const String& comment);
00174 
00175     // Return the fieldnumber of this field.
00176     Int fieldNumber() const
00177         {return fieldNumber_p;}
00178 
00179     // Return the name of the field.
00180     String name() const
00181         {return parent_p->name (fieldNumber_p);}
00182 
00183     // Is this field pointer attached to a valid record? Operations which
00184     // might cause it to become detached are:
00185     // <ol>
00186     //     <li> Destruction of the Record
00187     //     <li> Restructuring of the record.
00188     //     <li> Explicit call of the detach() member.
00189     // </ol>
00190     //# This inherited function is shown for documentation purposes.
00191     Bool isAttached() const
00192         {return NoticeTarget::isAttached();}
00193 
00194 private:
00195     T*               fieldPtr_p;
00196     RecordInterface* parent_p;
00197     Int              fieldNumber_p;
00198 
00199     // Not important for users - the mechanism by which field pointers are
00200     // notified when there is a change in the record.
00201     virtual void notify (const Notice& message);
00202 };
00203 
00204 
00205 // <summary>
00206 //  Read-Only access to an individual field from a Record.
00207 // </summary>
00208 
00209 // <use visibility=export>
00210 // <reviewed reviewer="Mark Wieringa" date="1996/04/15" tests="tRecord">
00211 // </reviewed>
00212 
00213 // <prerequisite>
00214 //   <li> <linkto class="RecordFieldPtr">RecordRecordFieldPtr</linkto>.
00215 // </prerequisite>
00216 //
00217 // <synopsis>
00218 // This class is entirely like <linkto class="RecordFieldPtr">
00219 // RecordFieldPtr</linkto>, except that it only allows Read-Only
00220 // access to fields in a Record. The documentation for that class should
00221 // be consulted.
00222 // <p>
00223 // Note that RecordFieldPtr is not inherited from RORecordFieldPtr,
00224 // because that would give problems with the function attachToRecord.
00225 // It would allow RecordFieldPtr to attach to a const RecordInterface object.
00226 // </synopsis>
00227 
00228 template<class T> class RORecordFieldPtr
00229 {
00230 public:
00231     RORecordFieldPtr() {}
00232     RORecordFieldPtr (const RecordInterface& record, Int whichField)
00233         : fieldPtr_p((RecordInterface&)record, whichField) {}
00234     RORecordFieldPtr (const RecordInterface& record, const RecordFieldId& id)
00235         : fieldPtr_p((RecordInterface&)record, id) {}
00236     RORecordFieldPtr (const RecordFieldPtr<T>& other)
00237         : fieldPtr_p(other) {}
00238     RORecordFieldPtr (const RORecordFieldPtr<T>& other)
00239         : fieldPtr_p(other.fieldPtr_p) {}
00240     RORecordFieldPtr<T>& operator= (const RORecordFieldPtr<T>& other)
00241         { fieldPtr_p = other.fieldPtr_p; return *this;}
00242 
00243     ~RORecordFieldPtr() {}
00244 
00245     void attachToRecord (const RecordInterface& record, Int whichField)
00246         { fieldPtr_p.attachToRecord ((RecordInterface&)record, whichField); }
00247     void attachToRecord (const RecordInterface& record, const RecordFieldId& id)
00248         { fieldPtr_p.attachToRecord ((RecordInterface&)record, id); }
00249     
00250     const T& operator*() const  {return *fieldPtr_p;}
00251     const T& get() const  {return fieldPtr_p.get();}
00252 
00253     const String& comment() const  {return fieldPtr_p.comment();}
00254 
00255     Int fieldNumber() const
00256         {return fieldPtr_p.fieldNumber();}
00257 
00258     void detach() {fieldPtr_p.detach(); }
00259     Bool isAttached() const {return fieldPtr_p.isAttached(); }
00260 
00261 private:
00262     RecordFieldPtr<T> fieldPtr_p;
00263 };
00264 
00265 
00266 
00267 //# Define some global functions to specialize some FieldRecordPtr functions.
00268 //# Some compilers have problems with normal specializations.
00269 inline void defineRecordFieldPtr (RecordInterface* parent, Int fieldNumber,
00270                                   DataType type, const void* value)
00271 {
00272     parent->defineDataField (fieldNumber, type, value);
00273 }
00274 inline void defineRecordFieldPtr (RecordInterface* parent, Int fieldNumber,
00275                                   DataType, const TableRecord* value)
00276 {
00277     parent->defineDataField (fieldNumber, TpRecord, value);
00278 }
00279 
00280 // This function attaches a RecordFieldPtr object.
00281 // It is checked if the field type is correct.
00282 inline void* attachRecordFieldPtr (RecordInterface* parent, Int fieldNumber,
00283                                    DataType type, const void*)
00284 {
00285     return parent->get_pointer (fieldNumber, type);
00286 }
00287 // Specialization for a Table field (which cannot be used).
00288 inline void* attachRecordFieldPtr (RecordInterface* parent, Int fieldNumber,
00289                                    DataType, const Table*)
00290 {
00291     return parent->get_pointer (fieldNumber, TpOther);
00292 }
00293 // Specialization for a Record field.
00294 inline void* attachRecordFieldPtr (RecordInterface* parent, Int fieldNumber,
00295                                    DataType, const Record*)
00296 {
00297     return parent->get_pointer (fieldNumber, TpRecord, "Record");
00298 }
00299 // Specialization for a TableRecord field.
00300 inline void* attachRecordFieldPtr (RecordInterface* parent, Int fieldNumber,
00301                                    DataType, const TableRecord*)
00302 {
00303     return parent->get_pointer (fieldNumber, TpRecord, "TableRecord");
00304 }
00305 
00306 
00307 
00308 
00309 } //# NAMESPACE CASA - END
00310 
00311 #ifndef CASACORE_NO_AUTO_TEMPLATES
00312 #include <casa/Containers/RecordField.tcc>
00313 #endif //# CASACORE_NO_AUTO_TEMPLATES
00314 #endif