casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
FunctionHolder.h
Go to the documentation of this file.
00001 //# FunctionHolder.h: A holder for Functions to enable record conversions
00002 //# Copyright (C) 2002,2003,2004
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 //# $Id: FunctionHolder.h 21024 2011-03-01 11:46:18Z gervandiepen $
00027 
00028 #ifndef SCIMATH_FUNCTIONHOLDER_H
00029 #define SCIMATH_FUNCTIONHOLDER_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Utilities/PtrHolder.h>
00034 #include <casa/Utilities/RecordTransformable.h>
00035 #include <scimath/Functionals/Function.h>
00036 #include <casa/Arrays/Vector.h>
00037 #include <casa/BasicSL/String.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 //# Forward Declarations
00042 
00043 // <summary> A holder for Functions to enable record conversions </summary>
00044 
00045 // <use visibility=export>
00046 
00047 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tFunctionHolder" demos="">
00048 // </reviewed>
00049 
00050 // <prerequisite>
00051 //   <li> <linkto class=RecordInterface>RecordInterface</linkto> class
00052 //   <li> <linkto class=Function>Function</linkto> class
00053 // </prerequisite>
00054 //
00055 // <etymology>
00056 // A Holder of general Measures
00057 // </etymology>
00058 //
00059 // <synopsis>
00060 // This class can be used to handle heterogeneous collections of Functions,
00061 // e.g. as a <src>Vector<FunctionHolder></src>. With the aid of the
00062 // toRecord() and fromRecord() functions it can be used
00063 // to convert a Function object into or from a record.
00064 // A FunctionHolder is created from a Function, or can be empty.
00065 //
00066 // </synopsis>
00067 //
00068 // <example>
00069 // <srcblock>
00070 //      TableRecord rec;
00071 //      MDirection dir(MVDirection(Quantity(12.5, 'deg'), Quantity(-2, 'deg')),
00072 //                     MDirection::J2000);
00073 //      String error;           // error message
00074 //      if (!FunctionHolder(dir).toRecord(error, rec)) {
00075 //              cout << error << endl;
00076 //      }
00077 //      Record grec;            // a Record
00078 //      if (!FunctionHolder(dir).toRecord(error, grec)) {  // make record
00079 //              cout << error << endl;
00080 //      }
00081 // // Note that for GlishRecords use can be made of the
00082 // // GlishRecord::to/fromrecord() methods.
00083 // </srcblock>
00084 // </example>
00085 //
00086 // <motivation>
00087 // To make general conversions between Functions and records, without knowing
00088 // the actual Function being converted.
00089 // </motivation>
00090 
00091 template <class T> class FunctionHolder : public RecordTransformable {
00092  public:
00093   //# Enumerations
00094   // Types of functions
00095   enum Types {
00096     GAUSSIAN1D,
00097     GAUSSIAN2D,
00098     GAUSSIAN3D,
00099     GAUSSIANND,
00100     HYPERPLANE,
00101     POLYNOMIAL,
00102     EVENPOLYNOMIAL,
00103     ODDPOLYNOMIAL,
00104     SINUSOID1D,
00105     CHEBYSHEV,
00106     BUTTERWORTH,
00107     COMBINE,
00108     COMPOUND,
00109     COMPILED,
00110     N_Types
00111   };
00112   //# Structures
00113   // Structure to hold functional status
00114   struct FuncStat {
00115     // Name
00116     String nam;
00117     // type
00118     Types tp;
00119     // Order (True if needed)
00120     Bool order;
00121   };
00122 
00123   //# Constructors
00124   // Creates an empty holder
00125   FunctionHolder();
00126   // Create from a Function (copy made)
00127   FunctionHolder(const Function<T> &in);
00128   // Copy a holder (copy semantics)
00129   FunctionHolder(const FunctionHolder<T> &other);
00130   //# Destructor
00131   ~FunctionHolder();
00132 
00133   //# Operators
00134   // Assignment (copy semantics)
00135   FunctionHolder &operator=(const FunctionHolder<T> &other);
00136 
00137   //# Member Functions
00138   // Check the the FunctionHolder holds the specified type. Return
00139   // True if if does and False otherwise.
00140   // <group>
00141   Bool isEmpty() const;
00142   // </group>
00143   // Get the known names
00144   const Vector<String> &names() const;
00145   // Get a specific Function from the holder (with lifetime as long 
00146   // as holder exists).
00147   // <thrown>
00148   // <li> AipsError if holder empty
00149   // <li> AipsError if holder contains wrong Function
00150   // </thrown>
00151   // <group>
00152   const Function<T> &asFunction() const;
00153   // </group>
00154   // Add a function
00155   Bool addFunction(const Function<T> &fnc); 
00156   // Get the type of currently filled holder
00157   Types type() const;
00158   // Create a Function from a record. An error message is generated, and False
00159   // returned if an invalid record is given. A valid record will return True.
00160   // A valid record contains at least the following fields (any additional fields are
00161   // ignored):
00162   // <ul>
00163   // <li> tp = TpString: type of Function (gaussian1d, etc; case
00164   //     insensitive) -- OR an enumeration code
00165   // <li> order = TpInt: the order needed to create a Function (-1 if not
00166   //    necessary or default)
00167   // <li> ndim, npar, params are optional
00168   // <li> nfunc, funcs are required for COMBI or COMPOUND
00169   // </ul>
00170   // A Function can be created from a string. In that case the string
00171   // will only indicate the type of function (like polynomial), and will
00172   // create a default polynomial of that given type. 
00173   // Error messages are postfixed to error.
00174   // <group>
00175   virtual Bool fromRecord(String &error, const RecordInterface &in);
00176   virtual Bool fromString(String &error, const String &in);
00177   template <class U>
00178     Bool getRecord(String &error, Function<U> *&fn,
00179                    const RecordInterface &in);
00180   // </group>
00181   // Create a record from a Function. The return will be False and an error
00182   // message generated only if the FunctionHolder does not contain a Function.
00183   // Error messages are postfixed to error.
00184   virtual Bool toRecord(String &error, RecordInterface &out) const;
00185   // Get identification of record
00186   virtual const String &ident() const;
00187 
00188 private:
00189   //# Data Members
00190   // Pointer to a Function
00191   PtrHolder<Function<T> > hold_p;
00192   // Aids (only filled after a succesful to/fromRecord
00193   // <group>
00194   mutable Types nf_p;
00195   mutable Int order_p;
00196   mutable String text_p;
00197   mutable PtrHolder<RecordInterface> mode_p;
00198   // </group>
00199   // List of known names
00200   mutable Vector<String> nam_p;
00201   // Filled list?
00202   mutable Bool isFilled;
00203 
00204   //# Member functions
00205   // Initialise and check the name list
00206   void init() const;
00207   // Aid for to/from Record, String
00208   // <group>
00209   Bool putType(String &error, RecordInterface &out) const;
00210   template <class U>
00211     Bool getType(String &error, Function<U> *&fn, const RecordInterface &in);  
00212   template <class U>
00213     Bool getType(String &error, Function<U> *&fn);
00214   void setParameters(Function<T> *&fn, const Vector<T> &params);
00215   void setParameters(Function<AutoDiff<T> > *&fn, const Vector<T> &params);
00216   // </group>
00217 };
00218 
00219 
00220 } //# NAMESPACE CASA - END
00221 
00222 #ifndef CASACORE_NO_AUTO_TEMPLATES
00223 #include <scimath/Functionals/FunctionHolder.tcc>
00224 #endif //# CASACORE_NO_AUTO_TEMPLATES
00225 #endif