casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
AttValBase.h
Go to the documentation of this file.
00001 //# AttValBase.h: abstract base class and support class for AttributeValues
00002 //# Copyright (C) 1996,1997,1999,2000,2001,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 TRIALDISPLAY_ATTVALBASE_H
00029 #define TRIALDISPLAY_ATTVALBASE_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/Utilities/DataType.h>
00033 #include <casa/Arrays/Vector.h>
00034 #include <casa/Quanta/Quantum.h>
00035 #include <casa/iostream.h>
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 // <summary>
00040 // Provision of type identification services for Attribute classes.
00041 // </summary>
00042 //
00043 // <use visibility=local>
00044 //
00045 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tAttribute" demos="">
00046 // </reviewed>
00047 //
00048 // <etymology>
00049 // "AttValue" is a contraction of "Attribute Value"
00050 // </etymology>
00051 //
00052 // <synopsis>
00053 // AttValue provides type identification services for use in the
00054 // Attribute classes.
00055 // </synopsis>
00056 //
00057 // <example>
00058 // AttValue can be used to provide an enumeration which identifies 
00059 // the type of a variable, for example: 
00060 // <srcblock>
00061 // Int i;
00062 // if (AttValue::whatType(&i) != AttValue::AtInt) {
00063 //   throw(AipsError(String("Incorrect type identification in AttValue")));
00064 // }
00065 // </srcblock>
00066 // </example>
00067 //
00068 // <motivation>
00069 // This is a support class to unify type identification for the 
00070 // various Attribute classes.
00071 // </motivation>
00072 //
00073 // <todo asof="2000/01/17">
00074 // <li> Consider using global whatType function instead of this class.
00075 // </todo>
00076 
00077 class AttValue {
00078 
00079  public:
00080 
00081   // The possible value types.  
00082   enum ValueType {
00083     AtuInt,
00084     AtInt,
00085     AtFloat,
00086     AtDouble,
00087     AtBool,
00088     AtString,
00089     AtQuantity,
00090     AtInvalid
00091   };
00092   
00093   // Determine the type of a scalar or Array variable.
00094   // <group>
00095   static AttValue::ValueType whatType(uInt *) 
00096     { return AttValue::AtuInt; };
00097   static AttValue::ValueType whatType(Vector<uInt> *) 
00098     { return AttValue::AtuInt; };
00099   static AttValue::ValueType whatType(Int *) 
00100     { return AttValue::AtInt; };
00101   static AttValue::ValueType whatType(Vector<Int> *) 
00102     { return AttValue::AtInt; };
00103   static AttValue::ValueType whatType(Float *) 
00104     { return AttValue::AtFloat; };
00105   static AttValue::ValueType whatType(Vector<Float> *) 
00106     { return AttValue::AtFloat; };
00107   static AttValue::ValueType whatType(Double *) 
00108     { return AttValue::AtDouble; };
00109   static AttValue::ValueType whatType(Vector<Double> *) 
00110     { return AttValue::AtDouble; };
00111   static AttValue::ValueType whatType(Bool *) 
00112     { return AttValue::AtBool; };
00113   static AttValue::ValueType whatType(Vector<Bool> *) 
00114     { return AttValue::AtBool; };
00115   static AttValue::ValueType whatType(String *) 
00116     { return AttValue::AtString; };
00117   static AttValue::ValueType whatType(Vector<String> *) 
00118     { return AttValue::AtString; };
00119   static AttValue::ValueType whatType(Quantity *)
00120     { return AttValue::AtQuantity; };
00121   static AttValue::ValueType whatType(Vector<Quantity> *)
00122     { return AttValue::AtQuantity; };
00123   static AttValue::ValueType whatType(void *) 
00124     { return AttValue::AtInvalid; };
00125   // </group>
00126 
00127 };
00128 
00129 // <summary> 
00130 // Base class for values of Attributes used in the display classes.
00131 // </summary>
00132 //
00133 // <use visibility=export>
00134 // 
00135 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tAttribute" demos="">
00136 // </reviewed>
00137 // 
00138 // <etymology> 
00139 // "AttributeValueBase" is a concatenation, representing a "Base"
00140 // class for "Attribute Values."
00141 // </etymology>
00142 //
00143 // <synopsis> 
00144 // This class is the base for storing Attribute values.  <linkto
00145 // class=Attribute> Attributes </linkto> are simple name-value pairs,
00146 // where the name is a String, and the value can be a scalar or Array.
00147 //
00148 // This class defines the type-independent interface for Attribute
00149 // values, and the type-dependent interface and implementation is
00150 // provided in <linkto class=AttributeValue> AttributeValue </linkto>.
00151 // This type independent interface allows comparison of Attributes
00152 // of different types.
00153 // </synopsis>
00154 //
00155 // <motivation> 
00156 // To provide the non-templated (ie. type-independent) interface of
00157 // AttributeValues in a single place, thus enabling the hiding of 
00158 // the templated aspect of Attributes from the end-user.  In particular
00159 // it allows implementation of the comparison operators in the base
00160 // class, regardless of type.
00161 // </motivation>
00162 //
00163 // <todo asof="1999/12/09">
00164 // Nothing known.
00165 // </todo>
00166 
00167 class AttributeValueBase  {
00168 
00169  public:
00170   
00171   // Constructor.
00172   AttributeValueBase(AttValue::ValueType type, Bool strict);
00173   
00174   // Copy constructor.
00175   AttributeValueBase(const AttributeValueBase &other);
00176 
00177   // Destructor.
00178   virtual ~AttributeValueBase();
00179 
00180   // Copy assignment.
00181   const AttributeValueBase& operator=(const AttributeValueBase &other);
00182   
00183   // Get the type of the value stored.
00184   AttValue::ValueType getType() const; 
00185 
00186   // Check for equality (and inequality) of two objects derived from
00187   // AttributeValueBase.  It is implemented in terms of the pure
00188   // virtual method <src>matches</src>, which must be implemented in
00189   // derived classes.  The <src>operator==</src> only returns
00190   // <src>True</src> if <src>this->matches(other)</src> and
00191   // <src>other.matches(*this)</src> are both <src>True</src>.  This
00192   // guarantees that if <src> a == b </src> it follows that <src> b ==
00193   // a </src> (this is enforced this way because <src>a</src> and
00194   // <src>b</src> can be classes derived differently from
00195   // AttributeValueBase which can therefore have a different
00196   // implementation of <src>match()</src>).  
00197   // <group>
00198   Bool operator==(const AttributeValueBase &other) const;
00199   Bool operator!=(const AttributeValueBase &other) const;
00200   // </group>
00201   
00202   // Return a new copy of the AttributeValueBase 
00203   virtual AttributeValueBase* clone() const = 0;
00204   
00205   // Set/get the strictness state of this AttributeValue.
00206   // <group>
00207   virtual void setStrictness(const Bool &newStrict);
00208   virtual Bool getStrictness() const;
00209   // </group>
00210   
00211   // Add <src>other</src> to <src>*this</src>.
00212   virtual void operator+=(const AttributeValueBase& other) = 0;
00213 
00214   // Return class name
00215   virtual String className() const {return String("AttributeValueBase");};
00216 
00217   virtual void print(ostream& os) = 0;
00218   
00219  protected:
00220 
00221   // The type of what is stored.
00222   AttValue::ValueType itsValueType;
00223 
00224   // Whether the match is strict or not.
00225   Bool itsStrictness;
00226 
00227   // Calculate whether <src>*this</src> matches <src>other</src>.
00228   // Since the meaning of "match" can be different for different
00229   // types, it is left to the derived class to define this method.
00230   virtual Bool matches(const AttributeValueBase &other) const = 0;
00231 
00232   // Set the type of the value stored.
00233   virtual void setType(const AttValue::ValueType &newType);
00234 
00235   // Check that private data match
00236   Bool myMatch(const AttributeValueBase &other) const;
00237 };
00238 
00239 ostream &operator<<(ostream &os, AttributeValueBase &av);
00240 
00241 
00242 } //# NAMESPACE CASA - END
00243 
00244 #endif