casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Compare.h
Go to the documentation of this file.
00001 //# Compare.h: compare two objects of the same type
00002 //# Copyright (C) 1994,1995,1999
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: Compare.h 20997 2010-11-17 07:05:29Z gervandiepen $
00027 
00028 #ifndef CASA_COMPARE_H
00029 #define CASA_COMPARE_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Utilities/DataType.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 // <summary> signature of comparison functions </summary>
00038 // <use visibility=export>
00039 // <reviewed reviewer="Friso Olnon" date="1995/02/24" tests="" demos="">
00040 
00041 // <synopsis>
00042 // This typedef defines the signature of the comparison functions used
00043 // in, for instance, the <linkto class="Sort">Sort</linkto> class: functions
00044 // with two <src>const void*</src> arguments returning an
00045 // <src>int</src> value. One such function is defined in the
00046 // <linkto class="ObjCompare">ObjCompare</linkto> class.
00047 // </synopsis>
00048 
00049 // <group name=ObjCompareFunc>
00050 typedef int ObjCompareFunc (const void*, const void*);
00051 // </group>
00052 
00053 
00054 // <summary> abstract base class for comparing two objects </summary>
00055 // <use visibility=export>
00056 // <reviewed reviewer="Friso Olnon" date="1995/02/24" tests="" demos="">
00057 //
00058 // <synopsis> 
00059 // The abstract class <src>BaseCompare<T></src> is used for comparisons
00060 // in sorting or iterating. One can derive a concrete comparison class
00061 // from it.
00062 // </synopsis>
00063 
00064 class BaseCompare
00065 {
00066 public:
00067     virtual ~BaseCompare() {}
00068 
00069     // Compare two objects, and return
00070     // <ul>
00071     //  <li> -1  if obj1 < obj2;
00072     //  <li>  0  if obj1 == obj2;
00073     //  <li>  1  otherwise.
00074     // </ul>
00075     virtual int comp (const void* obj1, const void* obj2) const = 0;
00076 
00077     // Get the data type of the comparison.
00078     // By default it returns TpOther.
00079     virtual DataType dataType() const
00080       { return TpOther; }
00081 };
00082 
00083 // <summary> compare two objects </summary>
00084 // <use visibility=export>
00085 // <reviewed reviewer="Friso Olnon" date="1995/02/24" tests="" demos="">
00086 
00087 // <synopsis> 
00088 // The templated class <src>ObjCompare<T></src> really is only a place
00089 // holder for the static function <src>compare</src> which compares two
00090 // objects of type T.
00091 // </synopsis>
00092 
00093 // <templating arg=T>
00094 // <li> operator==
00095 // <li> operator<
00096 // </templating>
00097 
00098 template<class T> class ObjCompare: public BaseCompare
00099 {
00100 public:
00101     virtual ~ObjCompare();
00102 
00103     // Compare two objects, and return
00104     // <ul>
00105     //  <li> -1  if obj1 < obj2;
00106     //  <li>  0  if obj1 == obj2;
00107     //  <li>  1  otherwise.
00108     // </ul>
00109     // The static function is not inlined allowing one to take the address of
00110     // it. Furthermore, the function's signature agrees with
00111     // <linkto group="Compare.h#ObjCompareFunc">ObjCompareFunc</linkto> so
00112     // that it can be used in the <linkto class="Sort">Sort</linkto> class.
00113     static int compare (const void* obj1, const void* obj2);
00114     virtual int comp (const void* obj1, const void* obj2) const;
00115 
00116     // Get the data type of the comparison.
00117     virtual DataType dataType() const;
00118 };
00119 
00120 
00121 } //# NAMESPACE CASA - END
00122 
00123 #ifndef CASACORE_NO_AUTO_TEMPLATES
00124 #include <casa/Utilities/Compare.tcc>
00125 #endif //# CASACORE_NO_AUTO_TEMPLATES
00126 #endif