casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
AipsrcValue.h
Go to the documentation of this file.
00001 //# AipsrcValue.h: Class to read values from the  Aipsrc general resource files
00002 //# Copyright (C) 1995,1996,1997,1999,2002,2003
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: AipsrcValue.h 21051 2011-04-20 11:46:29Z gervandiepen $
00027 
00028 #ifndef CASA_AIPSRCVALUE_H
00029 #define CASA_AIPSRCVALUE_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/BasicSL/String.h>
00033 #include <casa/Containers/Block.h>
00034 #include <casa/System/Aipsrc.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Forward declarations
00039 class Unit;
00040 
00041 // <summary> Class to read values from the Aipsrc general resource files
00042 // </summary>
00043 
00044 // <use visibility=export>
00045 
00046 // <reviewed reviewer="mhaller" date="1997/10/08" tests="tAipsrcValue" demos="">
00047 // </reviewed>
00048 
00049 // <prerequisite>
00050 //  <li> <linkto class=Aipsrc>Aipsrc</linkto>
00051 // </prerequisite>
00052 //
00053 // <etymology>
00054 // A class for getting values from the Aipsrc files
00055 // </etymology>
00056 //
00057 // <synopsis>
00058 // The static AipsrcValue class can get typed values from the Aipsrc
00059 // resource files.<br>
00060 // The basic interaction with the class is with the static keyword match
00061 // functions:
00062 // <srcblock>
00063 // Bool AipsrcValue<Type>::find(Type &result, const String &keyword)
00064 // Bool AipsrcValue<Type>::find(Type &result, const String &keyword,
00065 //                              const Type &deflt)
00066 // </srcblock>
00067 // comparable to the standard (String) <linkto class=Aipsrc>Aipsrc</linkto>
00068 // find.<br>
00069 // If the resource file contains a multi-valued keyword, use the
00070 // <linkto class=AipsrcVector>AipsrcVector</linkto> class instead.
00071 //
00072 // The class is templated. For ease of use typedefs are provided for:
00073 // <srcblock>
00074 // AipsrcDouble, AipsrcInt, AipsrcBool, AipsrcString
00075 // AipsrcVDouble, AipsrcVInt, AipsrcVBool, AipsrcVString
00076 // </srcblock>
00077 // In addition to the above finds, special finds:
00078 // <srcblock>
00079 // Bool AipsrcValue<Type>::find(Type &result, const String &keyword,
00080 //                              const Unit &defun, const Unit &resun)
00081 // Bool AipsrcValue<Type>::find(Type &result, const String &keyword,
00082 //                              const Unit &defun, const Unit &resun,
00083 //                              const Type &deflt)
00084 // </srcblock>
00085 // are provided. These finds will read the keyword value as a Quantity.
00086 // If no units are given, the defun are assumed. The result is converted
00087 // to the resun, before the value is returned. E.g.
00088 // <srcblock>
00089 //      Double x;
00090 //      find(x, "time.offset", "h", "d");
00091 // </srcblock>
00092 // will return:
00093 // <ul>
00094 //  <li> 2.5/24 for a value specified as 2.5 in resource file
00095 //  <li> 2.5/24 for 2:30:00
00096 //  <li> 0.5/24 for 30min
00097 //  <li> 0.5 for 0.5d
00098 // </ul>
00099 //
00100 // The class has <src>registerRC, get, set</src> functions as described in
00101 // <linkto class=Aipsrc>Aipsrc</linkto>. Note that registration is on a
00102 // per Type basis, and hence registration of the same keyword in different
00103 // types (and possible sets) act on different values, but with the same
00104 // result if no set has been done.
00105 //
00106 // Specialisation exists for <src>Bool</src>, where <src>True</src> is
00107 // any value string starting with one of 'yYtT123456789', and False in
00108 // all other cases, and no finds with Units are provided. Strings are
00109 // supposed to be handled by standard <linkto class=Aipsrc>Aipsrc</linkto>
00110 // class for single values, and a specialisation exists for the
00111 // <linkto class=AipsrcVector>AipsrcVector</linkto> case.
00112 //
00113 // </synopsis>
00114 //
00115 // <example>
00116 // <srcblock>
00117 //  String tzoff;               // result of keyword find
00118 //  if (!AipsrcValue<Double>::find(tzoff, "time.zone.offset")) {        // look for key
00119 //    tzoff = -5;
00120 //  };
00121 // </srcblock>
00122 // A more convenient way of accomplishing the same result is:
00123 // <srcblock>
00124 //    AipsrcDouble::find(tzoff, "time.zone.offset", -5);
00125 // </srcblock>
00126 // or even:
00127 // <srcblock>
00128 //    AipsrcDouble::find(tzoff, "time.zone.offset",
00129 //                       "h", "h", -5);
00130 // </srcblock>
00131 // Here the final argument is the default to use if the keyword is not found
00132 // at all.
00133 // </example>
00134 //
00135 // 
00136 // <templating>
00137 //  <li> All types with a <src>>></src> defined. 
00138 // <note role=warning>
00139 // Since interpretation of the keyword value string is done with the standard
00140 // input right-shift operator, specialisations are necessary for non-standard
00141 // cases like Bool. They are provided. String is supposed to be handled by
00142 // standard Aipsrc.
00143 // </note>
00144 // </templating>
00145 //
00146 // <motivation>
00147 // Programs need a way to interact with the AipsrcValue files.
00148 // </motivation>
00149 //
00150 // <thrown>
00151 //    <li>AipsError if the environment variables HOME and/or AIPSPATH not set.
00152 // </thrown>
00153 //
00154 // <todo asof="1997/08/07">
00155 // </todo>
00156 
00157 template <class T> class AipsrcValue : public Aipsrc {
00158 
00159 public:
00160   //# Constructors
00161   // Default constructor
00162   // <note role=tip>
00163   // A constructor (and destructor) have been provided to be able to generate
00164   // a (routine-level) static register list. This had to be done since
00165   // static data members are not yet implemented in the gcc compiler for
00166   // templated classes. Once they are available the <tt>tlist</tt> and
00167   // <tt>ntlst</tt> data can become static, constructor and desctructor and
00168   // all references to the init() method can disappear.
00169   // </note>
00170   AipsrcValue();
00171   //# Destructor
00172   // See note with constructor
00173   ~AipsrcValue();
00174 
00175   //# Member functions
00176   // The <src>find()</src> functions will, given a keyword, return the value
00177   // of a matched keyword found in the files. If no match found the
00178   // function will be False, and the default returned if specified.
00179   // <group>
00180   static Bool find(T &value, const String &keyword);
00181   static Bool find(T &value, const String &keyword, const T &deflt);
00182   // </group>
00183   // These <src>find()</src> functions will, given a keyword, read the value
00184   // of a matched keyword as a Quantity. If no unit has been given in the
00185   // keyword value, the defun Unit will be assumed. The value returned
00186   // will be converted to the resun Unit. If no match found, the default
00187   // value is returned (see example above).
00188   // <group>
00189   static Bool find(T &value, const String &keyword,
00190                    const Unit &defun, const Unit &resun);
00191   static Bool find(T &value, const String &keyword,
00192                    const Unit &defun, const Unit &resun,
00193                    const T &deflt);
00194   // </group>
00195   // Functions to register keywords for later use in get() and set(). The
00196   // returned value is the index for get() and set().
00197   // <group>
00198   static uInt registerRC(const String &keyword,
00199                          const T &deflt);
00200   static uInt registerRC(const String &keyword,
00201                          const Unit &defun, const Unit &resun,
00202                          const T &deflt);
00203   // </group>
00204 
00205   // Gets are like find, but using registered integers rather than names. The
00206   // aipsrc file is read only once, and values can be set as well.
00207   // <group>
00208   static const T &get(uInt keyword);
00209   // </group>
00210 
00211   // Sets allow registered values to be set
00212   // <group>
00213   static void set(uInt keyword, const T &deflt);
00214   // </group>
00215 
00216   // Save registered value to <src>$HOME/.aipsrc</src>
00217   static void save(uInt keyword);
00218 
00219 private:
00220   //# Data
00221   // The global AipsrcValue object
00222   static AipsrcValue myp_p;
00223   static Mutex theirMutex;
00224   // Register list
00225   // <group>
00226   Block<T> tlst;
00227   Block<String> ntlst;
00228   // </group>
00229 
00230   //# Constructors
00231   // Copy constructor (not implemented)
00232   AipsrcValue<T> &operator=(const AipsrcValue<T> &other);
00233 
00234   //# Copy assignment (not implemented)
00235   AipsrcValue(const AipsrcValue<T> &other);
00236 
00237   //# General member functions
00238 };
00239 
00240 template <> 
00241 Bool AipsrcValue<String>::find(String &value,
00242                                const String &keyword,
00243                                const Unit &defun, const Unit &resun);
00244 
00245 #define AipsrcValue_Bool AipsrcValue
00246 
00247 // <summary> Specialization of AipsrcValue for Bool </summary>
00248 
00249 // <synopsis>
00250 // <note role=warning>
00251 // The name <src>AipsrcValue_Bool</src> is only for cxx2html
00252 // documentation problems. Use <src>AipsrcValue</src> in your code.</note>
00253 // </synopsis>
00254 
00255 template <> class AipsrcValue_Bool<Bool> : public Aipsrc {
00256 public:
00257   AipsrcValue_Bool();
00258   ~AipsrcValue_Bool();
00259   static Bool find(Bool &value, const String &keyword);
00260   static Bool find(Bool &value, const String &keyword, const Bool &deflt);
00261   static uInt registerRC(const String &keyword, const Bool &deflt);
00262   static const Bool &get(uInt keyword);
00263   static void set(uInt keyword, const Bool &deflt);
00264   static void save(uInt keyword);
00265 private:
00266   static AipsrcValue_Bool myp_p;
00267   static Mutex theirMutex;
00268   Block<Bool> tlst;
00269   Block<String> ntlst;
00270   AipsrcValue_Bool<Bool> &operator=(const AipsrcValue_Bool<Bool> &other);
00271   AipsrcValue_Bool(const AipsrcValue_Bool<Bool> &other);
00272 };
00273 
00274 #undef AipsrcValue_Bool
00275 
00276 
00277 } //# NAMESPACE CASA - END
00278 
00279 #ifndef CASACORE_NO_AUTO_TEMPLATES
00280 #include <casa/System/AipsrcValue.tcc>
00281 #endif //# CASACORE_NO_AUTO_TEMPLATES
00282 #endif