casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SerialHelper.h
Go to the documentation of this file.
00001 //# SerialHelper: a helper class for (un)serializing a Function object
00002 //# Copyright (C) 2002
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: SerialHelper.h 19951 2007-02-28 02:59:51Z gervandiepen $
00028 
00029 #ifndef SCIMATH_SERIALHELPER_H
00030 #define SCIMATH_SERIALHELPER_H
00031 
00032 #include <scimath/Functionals/FunctionFactoryErrors.h>
00033 #include <casa/Containers/Record.h>
00034 namespace casa { //# NAMESPACE CASA - BEGIN
00035 
00036 template<class T> class Array;
00037 
00038 template <class V>
00039 void getArrayVal(V &val, int type, const Record& gr, 
00040                       const String& name, uInt index=0) 
00041     throw (InvalidSerializationError);
00042 
00043 template <class V>
00044 void getArray(Array<V> &val, int type, const Record& gr, 
00045                    const String& name) 
00046     throw (InvalidSerializationError);
00047 
00048 // <summary>
00049 //
00050 //
00051 //
00052 //
00053 //
00054 // </summary>
00055 
00056 // <use visibility=export>
00057 
00058 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00059 // </reviewed>
00060 
00061 // <prerequisite>
00062 //   <li> FunctionFactory
00063 // </prerequisite>
00064 //
00065 // <etymology>
00066 // 
00067 // 
00068 // </etymology>
00069 //
00070 // <synopsis>
00071 //
00072 //
00073 //
00074 //
00075 // </synopsis>
00076 //
00077 // <example>
00078 //
00079 //
00080 //
00081 // </example>
00082 //
00083 // <motivation>
00084 //
00085 //
00086 //
00087 // </motivation>
00088 //
00089 // <thrown>
00090 //    <li> InvalidSerializationError by getFuncType() if Record 
00091 //         does not contain a "functype" field containing a string.
00092 //    <li> InvalidSerializationError
00093 // </thrown>
00094 //
00095 // <todo asof="yyyy/mm/dd">
00096 //   <li> 
00097 //   <li> 
00098 //   <li> 
00099 // </todo>
00100 
00101 class SerialHelper {
00102 public: 
00103     static const String FUNCTYPE;
00104     static const String gtype[]; 
00105     enum shType { shtBOOL=0, shtBYTE, shtSHORT, shtINT, shtFLOAT,
00106                   shtDOUBLE, shtCOMPLEX, shtDCOMPLEX, shtSTRING};
00107 
00108     SerialHelper(const Record& record) : gr(record) { }
00109     SerialHelper(const SerialHelper& other) { gr = other.gr; }
00110     virtual ~SerialHelper() { }
00111 
00112     // load the function type name as given in the record's "functype" 
00113     // field into the given String <em>ftype</em>.  <em>gr</em> is the 
00114     //  record to extract from.  False is returned if the record 
00115     // does not contain this field.
00116     // <thrown>
00117     //   <li> InvalidSerializationError if "functype" exists but is 
00118     //          empty or the incorrect type
00119     // </thrown>
00120     Bool getFuncType(String& ftype) const
00121         throw (InvalidSerializationError);
00122 
00123     // ensure that the Function type stored in the given record, <em>gr</em>,
00124     // matches <em>ftype</em>.  If it does not, an 
00125     // InvalidSerializationError is thrown.
00126     void checkFuncType(const String& ftype) const
00127         throw (InvalidSerializationError);
00128 
00129     // return True if a field with the given <em>name</em> exists
00130     Bool exists(const String &name) const { return gr.isDefined(name); }
00131 
00132     // Get the <em>index</em>th element of the <em>name</em> field 
00133     // This should be 
00134     // particularly useful for Array objects with only one element,
00135     // i.e. a <em>scalar</em>.
00136     // Note that unlike the native  classes, indexing is zero-relative.
00137     // 
00138     // InvalidSerializationError is thrown if:
00139     // <ul>
00140     //  <li> if the given record does not contain a field called <em>name</em>
00141     //  <li> if the field is not a vector of the correct type.
00142     //  <li> if the index is out of range.
00143     // </ul>
00144     // <group>
00145     void get(Bool &val, const String& name, uInt index = 0) const
00146         throw (InvalidSerializationError);
00147 //      void get(uChar &val, const String& name, uInt index = 0) const
00148 //      throw (InvalidSerializationError);
00149     void get(Short &val, const String& name, uInt index = 0) const
00150         throw (InvalidSerializationError);
00151     void get(Int &val, const String& name, uInt index = 0) const
00152         throw (InvalidSerializationError);
00153     void get(Float &val, const String& name, uInt index = 0) const
00154         throw (InvalidSerializationError);
00155     void get(Double &val, const String& name, uInt index = 0) const
00156         throw (InvalidSerializationError);
00157     void get(Complex &val, const String& name, uInt index = 0) const
00158         throw (InvalidSerializationError);
00159     void get(DComplex &val, const String& name, uInt index = 0) const
00160         throw (InvalidSerializationError);
00161     void get(String &val, const String& name, uInt index = 0) const
00162         throw (InvalidSerializationError);
00163     void get(Record &val, const String& name) const
00164         throw (InvalidSerializationError);
00165     // </group>
00166 
00167     // Get the <em>index</em>th element of the <em>name</em> field 
00168     // This should be 
00169     // particularly useful for Array objects with only one element,
00170     // i.e. a <em>scalar</em>.
00171     // Note that unlike the native  classes, indexing is zero-relative.
00172     // 
00173     // InvalidSerializationError is thrown if:
00174     // <ul>
00175     //  <li> if the given record does not contain a field called <em>name</em>
00176     //  <li> if the field is not a vector of the correct type.
00177     //  <li> if the index is out of range.
00178     // </ul>
00179     // <group>
00180     void get(Array<Bool> &val, const String& name) const
00181         throw (InvalidSerializationError);
00182 //      void get(Array<uChar &val, const String& name) const
00183 //      throw (InvalidSerializationError);
00184     void get(Array<Short> &val, const String& name) const
00185         throw (InvalidSerializationError);
00186     void get(Array<Int> &val, const String& name) const
00187         throw (InvalidSerializationError);
00188     void get(Array<Float> &val, const String& name) const
00189         throw (InvalidSerializationError);
00190     void get(Array<Double> &val, const String& name) const
00191         throw (InvalidSerializationError);
00192     void get(Array<Complex> &val, const String& name) const
00193         throw (InvalidSerializationError);
00194     void get(Array<DComplex> &val, const String& name) const
00195         throw (InvalidSerializationError);
00196     void get(Array<String> &val, const String& name) const
00197         throw (InvalidSerializationError);
00198     // </group>
00199 
00200     SerialHelper& operator=(const SerialHelper& other) { 
00201         gr = other.gr;
00202         return *this;
00203     }
00204 
00205 protected:
00206     SerialHelper() { }
00207 
00208 private:
00209 
00210     Record gr;
00211 };
00212 
00213 
00214 } //# NAMESPACE CASA - END
00215 
00216 #endif