casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DisplayOptions.h
Go to the documentation of this file.
00001 //# DisplayOptions.h: option parsing for display classes
00002 //# Copyright (C) 1999,2000,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 //# $Id$
00027 
00028 #ifndef DISPLAY_DISPLAYOPTIONS_H
00029 #define DISPLAY_DISPLAYOPTIONS_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/Containers/Record.h>
00033 #include <casa/Arrays/Array.h>
00034 #include <casa/Arrays/ArrayLogical.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // <summary>
00039 // Class to provide option parsing routines for display classes.
00040 // </summary>
00041 
00042 // <use visibility=local>
00043 
00044 // <reviewed reviewer="Peter Barnes" date="1999/10/27" tests="tDisplayOptions" demos="">
00045 // </reviewed>
00046 
00047 // <prerequisite>
00048 // <li> <linkto class=Record>Record</linkto>
00049 // </prerequisite>
00050 
00051 // <etymology>
00052 // DisplayOptions contains methods for parsing "Options" for
00053 // various "Display" classes.
00054 // </etymology>
00055 
00056 // <synopsis>
00057 // DisplayOptions is a simple class which provides methods for parsing
00058 // Records containing fields which conform to "options" as used in the
00059 // Display Library classes.  Consider a record having the following
00060 // structure:
00061 // <srcblock>
00062 // rec.minimum.value = 4.0
00063 // rec.minimum.otherfield = "some text"
00064 // rec.maximum = 8.0
00065 // </srcblock>
00066 // The DisplayOptions methods can be used to extract the values of the
00067 // <src>minimum</src> and <src>maximum</src> fields of the Record
00068 // <src>rec</src>, regardless of whether the value itself is stored in
00069 // a <src>value</src> sub-field, or at the next higher level.  The
00070 // DisplayOptions methods also offer the capability to detect "unset"
00071 // Record values.
00072 //
00073 // This class can be used as-is, or inherited from by higher-level
00074 // classes.
00075 // </synopsis>
00076 
00077 // <example>
00078 // The following example shows the use of DisplayOptions as a 
00079 // stand-alone class.
00080 // <srcblock>
00081 // /* assume some Record "rec" is to be parsed */
00082 // DisplayOptions dopt;
00083 // Bool error;
00084 //
00085 // Float min = 0.0;
00086 // if (dopt.readOptionRecord(min, error, rec, "minimum") && !error) {
00087 //   cerr << "minimum found and changed, new value is " << min << endl;
00088 // }
00089 // 
00090 // String color;
00091 // Bool colorIsUnset = False;
00092 // if (dopt.readOptionRecord(color, colorIsUnset, error,
00093 //                           rec, "color") && !error) {
00094 //   if (colorIsUnset) {
00095 //     cerr << "color found and unset" << endl;
00096 //   } else {
00097 //     cerr << "color found and changed, new value is " << color << endl;
00098 //   }
00099 // }
00100 // </srcblock>
00101 // </example>
00102 
00103 // <motivation>
00104 // Options are used prolifically throughout the display classes,
00105 // so some unified processing of options is desirable.
00106 // </motivation>
00107 
00108 // <thrown>
00109 // None.
00110 // </thrown>
00111 
00112 // <todo asof="1999/08/26">
00113 // <li> add unset support for Bool, Float and Int types
00114 // </todo>
00115 
00116 class DisplayOptions {
00117 
00118  public:
00119 
00120   // Constructor.
00121   DisplayOptions();
00122   
00123   // Destructor.
00124   virtual ~DisplayOptions() { };
00125 
00126   // Find the field <src>fieldname</src> in Record <src>rec</src>,
00127   // containing the requested type (Bool, String, Float, Int) in
00128   // itself or its "value" sub-field, and return the value in
00129   // <src>target</src>.  If the field doesn't exist, or does not
00130   // contain the requested type, or a "value" sub-field containing the
00131   // requested type, then <src>error</src> is set True.  When
00132   // <src>error</src> is False, the return value indicates whether
00133   // <src>target</src> was modified.  The Float version will also read
00134   // records containing Ints or Doubles, and cast their value to
00135   // Float.  
00136   // <group>
00137 
00138   template <class T> Bool readOptionRecord(Vector<T> &target, Bool &error, 
00139                                            const Record &rec,
00140                                            const String &fieldname) const;
00141 
00142   template <class T> Bool readOptionRecord(T &target, Bool &error, const Record &rec,
00143                                            const String &fieldname) const;
00144   // </group>
00145 
00146   // Find the field <src>fieldname</src> in Record <src>rec</src>,
00147   // containing the requested type (String) in itself or its "value"
00148   // sub-field, and return the value in <src>target</src>.  If the
00149   // field (or "value" sub-field) instead contains an "unset" Record,
00150   // then <src>unsetTarget</src> is set True.  If the field doesn't
00151   // exist, or does not contain the requested type (String) or an
00152   // "unset" Record, or a "value" sub-field containing either of
00153   // these, then <src>error</src> is set True.  When <src>error</src>
00154   // is False, the return value indicates whether <src>target</src>
00155   // (or <src>unsetTarget</src>) was modified.
00156   virtual Bool readOptionRecord(String &target, Bool &unsetTarget,
00157                                 Bool &error, const Record &rec,
00158                                 const String &fieldname) const;
00159 
00160   // Return a Record which is an "unset" Record, ie. has a field with
00161   // name <src>String("i_am_unset")</src> whose value is
00162   // <src>String("i_am_unset")</src>.
00163   Record unset() const
00164     { return itsUnsetRecord; }
00165 
00166   // Return True or False indicating if the provided Record is equal
00167   // to an "unset" Record.
00168    Bool isUnset(const Record &rec) const;
00169 
00170  protected:
00171 
00172   // (Required) copy constructor.
00173   DisplayOptions(const DisplayOptions &other);
00174 
00175   // (Required) copy assignment.
00176   void operator=(const DisplayOptions &other);
00177 
00178 private:
00179   // A utility to check if two types are compatible. (I.e. double is 
00180   // "compatible" with int. Used by readOptionRecord.
00181   Bool compatible(const DataType &compareme, const DataType &tome) const;
00182 
00183   // An unset record to return with protected <src>unset()</src>
00184   // function.
00185   Record itsUnsetRecord;
00186 
00187 };
00188 
00189 
00190 } //# NAMESPACE CASA - END
00191 #ifndef AIPS_NO_TEMPLATE_SRC
00192 #include <display/Utilities/DisplayOptionsTemplate.tcc>
00193 #endif
00194 #endif