Operators.h

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1999
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00031 
00032 // Templates for relational operations.
00033 //
00034 // jhrg 3/24/99
00035 
00036 #ifndef _operators_h
00037 #define _operators_h
00038 
00039 
00040 #include "GNURegex.h"  // GNU Regex class used for string =~ op.
00041 #include "parser.h"  // for ID_MAX
00042 #include "ce_expr.tab.h"
00043 
00044 using namespace std;
00045 
00046 inline unsigned
00047 dods_max(int i1, int i2)
00048 {
00049     return (unsigned)((i1 > i2) ? i1 : i2);
00050 }
00051 
00059 template<class T1, class T2> class Cmp
00060 {
00061 public:
00062     static bool eq(T1 v1, T2 v2)
00063     {
00064         return v1 == v2;
00065     }
00066     static bool ne(T1 v1, T2 v2)
00067     {
00068         return v1 != v2;
00069     }
00070     static bool gr(T1 v1, T2 v2)
00071     {
00072         return v1 > v2;
00073     }
00074     static bool ge(T1 v1, T2 v2)
00075     {
00076         return v1 >= v2;
00077     }
00078     static bool lt(T1 v1, T2 v2)
00079     {
00080         return v1 < v2;
00081     }
00082     static bool le(T1 v1, T2 v2)
00083     {
00084         return v1 <= v2;
00085     }
00086     static bool re(T1, T2)
00087     {
00088         cerr << "Illegal operation" << endl;
00089         return false;
00090     }
00091 };
00092 
00101 template<class UT1, class T2> class USCmp
00102 {
00103 public:
00104     static bool eq(UT1 v1, T2 v2)
00105     {
00106         return v1 == dods_max(0, v2);
00107     }
00108     static bool ne(UT1 v1, T2 v2)
00109     {
00110         return v1 != dods_max(0, v2);
00111     }
00112     static bool gr(UT1 v1, T2 v2)
00113     {
00114         return v1 > dods_max(0, v2);
00115     }
00116     static bool ge(UT1 v1, T2 v2)
00117     {
00118         return v1 >= dods_max(0, v2);
00119     }
00120     static bool lt(UT1 v1, T2 v2)
00121     {
00122         return v1 < dods_max(0, v2);
00123     }
00124     static bool le(UT1 v1, T2 v2)
00125     {
00126         return v1 <= dods_max(0, v2);
00127     }
00128     static bool re(UT1, T2)
00129     {
00130         cerr << "Illegal operation" << endl;
00131         return false;
00132     }
00133 };
00134 
00147 template<class T1, class UT2> class SUCmp
00148 {
00149 public:
00150     static bool eq(T1 v1, UT2 v2)
00151     {
00152         return dods_max(0, v1) == v2;
00153     }
00154     static bool ne(T1 v1, UT2 v2)
00155     {
00156         return dods_max(0, v1) != v2;
00157     }
00158     static bool gr(T1 v1, UT2 v2)
00159     {
00160         return dods_max(0, v1) > v2;
00161     }
00162     static bool ge(T1 v1, UT2 v2)
00163     {
00164         return dods_max(0, v1) >= v2;
00165     }
00166     static bool lt(T1 v1, UT2 v2)
00167     {
00168         return dods_max(0, v1) < v2;
00169     }
00170     static bool le(T1 v1, UT2 v2)
00171     {
00172         return dods_max(0, v1) <= v2;
00173     }
00174     static bool re(T1, UT2)
00175     {
00176         cerr << "Illegal operation" << endl;
00177         return false;
00178     }
00179 };
00180 
00186 template<class T1, class T2> class StrCmp
00187 {
00188 public:
00189     static bool eq(T1 v1, T2 v2)
00190     {
00191         return v1 == v2;
00192     }
00193     static bool ne(T1 v1, T2 v2)
00194     {
00195         return v1 != v2;
00196     }
00197     static bool gr(T1 v1, T2 v2)
00198     {
00199         return v1 > v2;
00200     }
00201     static bool ge(T1 v1, T2 v2)
00202     {
00203         return v1 >= v2;
00204     }
00205     static bool lt(T1 v1, T2 v2)
00206     {
00207         return v1 < v2;
00208     }
00209     static bool le(T1 v1, T2 v2)
00210     {
00211         return v1 <= v2;
00212     }
00213     static bool re(T1 v1, T2 v2)
00214     {
00215         Regex r(v2.c_str());
00216         return r.match(v1.c_str(), v1.length()) > 0;
00217     }
00218 };
00219 
00247 template<class T1, class T2, class C>
00248 bool rops(T1 a, T2 b, int op)
00249 {
00250     switch (op) {
00251     case SCAN_EQUAL:
00252         return C::eq(a, b);
00253     case SCAN_NOT_EQUAL:
00254         return C::ne(a, b);
00255     case SCAN_GREATER:
00256         return C::gr(a, b);
00257     case SCAN_GREATER_EQL:
00258         return C::ge(a, b);
00259     case SCAN_LESS:
00260         return C::lt(a, b);
00261     case SCAN_LESS_EQL:
00262         return C::le(a, b);
00263     case SCAN_REGEXP:
00264         return C::re(a, b);
00265     default:
00266         cerr << "Unknown operator" << endl;
00267         return false;
00268     }
00269 }
00270 
00271 #endif // _operators_h

Generated on Wed Jun 27 12:56:39 2007 for libdap++ by  doxygen 1.4.7