libdap++ Updated for version 3.8.2

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.hh"
00043 
00044 using namespace std;
00045 
00046 namespace libdap
00047 {
00048 
00049 inline unsigned
00050 dods_max(int i1, int i2)
00051 {
00052     return (unsigned)((i1 > i2) ? i1 : i2);
00053 }
00054 
00062 template<class T1, class T2> class Cmp
00063 {
00064 public:
00065     static bool eq(T1 v1, T2 v2)
00066     {
00067         return v1 == v2;
00068     }
00069     static bool ne(T1 v1, T2 v2)
00070     {
00071         return v1 != v2;
00072     }
00073     static bool gr(T1 v1, T2 v2)
00074     {
00075         return v1 > v2;
00076     }
00077     static bool ge(T1 v1, T2 v2)
00078     {
00079         return v1 >= v2;
00080     }
00081     static bool lt(T1 v1, T2 v2)
00082     {
00083         return v1 < v2;
00084     }
00085     static bool le(T1 v1, T2 v2)
00086     {
00087         return v1 <= v2;
00088     }
00089     static bool re(T1, T2)
00090     {
00091         cerr << "Illegal operation" << endl;
00092         return false;
00093     }
00094 };
00095 
00104 template<class UT1, class T2> class USCmp
00105 {
00106 public:
00107     static bool eq(UT1 v1, T2 v2)
00108     {
00109         return v1 == dods_max(0, v2);
00110     }
00111     static bool ne(UT1 v1, T2 v2)
00112     {
00113         return v1 != dods_max(0, v2);
00114     }
00115     static bool gr(UT1 v1, T2 v2)
00116     {
00117         return v1 > dods_max(0, v2);
00118     }
00119     static bool ge(UT1 v1, T2 v2)
00120     {
00121         return v1 >= dods_max(0, v2);
00122     }
00123     static bool lt(UT1 v1, T2 v2)
00124     {
00125         return v1 < dods_max(0, v2);
00126     }
00127     static bool le(UT1 v1, T2 v2)
00128     {
00129         return v1 <= dods_max(0, v2);
00130     }
00131     static bool re(UT1, T2)
00132     {
00133         cerr << "Illegal operation" << endl;
00134         return false;
00135     }
00136 };
00137 
00150 template<class T1, class UT2> class SUCmp
00151 {
00152 public:
00153     static bool eq(T1 v1, UT2 v2)
00154     {
00155         return dods_max(0, v1) == v2;
00156     }
00157     static bool ne(T1 v1, UT2 v2)
00158     {
00159         return dods_max(0, v1) != v2;
00160     }
00161     static bool gr(T1 v1, UT2 v2)
00162     {
00163         return dods_max(0, v1) > v2;
00164     }
00165     static bool ge(T1 v1, UT2 v2)
00166     {
00167         return dods_max(0, v1) >= v2;
00168     }
00169     static bool lt(T1 v1, UT2 v2)
00170     {
00171         return dods_max(0, v1) < v2;
00172     }
00173     static bool le(T1 v1, UT2 v2)
00174     {
00175         return dods_max(0, v1) <= v2;
00176     }
00177     static bool re(T1, UT2)
00178     {
00179         cerr << "Illegal operation" << endl;
00180         return false;
00181     }
00182 };
00183 
00189 template<class T1, class T2> class StrCmp
00190 {
00191 public:
00192     static bool eq(T1 v1, T2 v2)
00193     {
00194         return v1 == v2;
00195     }
00196     static bool ne(T1 v1, T2 v2)
00197     {
00198         return v1 != v2;
00199     }
00200     static bool gr(T1 v1, T2 v2)
00201     {
00202         return v1 > v2;
00203     }
00204     static bool ge(T1 v1, T2 v2)
00205     {
00206         return v1 >= v2;
00207     }
00208     static bool lt(T1 v1, T2 v2)
00209     {
00210         return v1 < v2;
00211     }
00212     static bool le(T1 v1, T2 v2)
00213     {
00214         return v1 <= v2;
00215     }
00216     static bool re(T1 v1, T2 v2)
00217     {
00218         Regex r(v2.c_str());
00219         return r.match(v1.c_str(), v1.length()) > 0;
00220     }
00221 };
00222 
00250 template<class T1, class T2, class C>
00251 bool rops(T1 a, T2 b, int op)
00252 {
00253     switch (op) {
00254     case SCAN_EQUAL:
00255         return C::eq(a, b);
00256     case SCAN_NOT_EQUAL:
00257         return C::ne(a, b);
00258     case SCAN_GREATER:
00259         return C::gr(a, b);
00260     case SCAN_GREATER_EQL:
00261         return C::ge(a, b);
00262     case SCAN_LESS:
00263         return C::lt(a, b);
00264     case SCAN_LESS_EQL:
00265         return C::le(a, b);
00266     case SCAN_REGEXP:
00267         return C::re(a, b);
00268     default:
00269         cerr << "Unknown operator" << endl;
00270         return false;
00271     }
00272 }
00273 
00274 } // namespace libdap
00275 
00276 #endif // _operators_h