libdap  Updated for version 3.17.0
Operators.h
00001 // -*- mode: c++; c-basic-offset:4 -*-
00002 
00003 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00004 // Access Protocol.
00005 
00006 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00007 // Author: James Gallagher <jgallagher@opendap.org>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 //
00023 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00024 
00025 // (c) COPYRIGHT URI/MIT 1999
00026 // Please read the full copyright statement in the file COPYRIGHT_URI.
00027 //
00028 // Authors:
00029 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00030 
00031 // Templates for relational operations.
00032 //
00033 // jhrg 3/24/99
00034 
00035 #ifndef _operators_h
00036 #define _operators_h
00037 
00038 #include "GNURegex.h"  // GNU Regex class used for string =~ op.
00039 #include "parser.h"  // for ID_MAX
00040 #include "ce_expr.tab.hh"
00041 
00042 //using namespace std;
00043 
00044 namespace libdap {
00045 
00053 template<class T1, class T2>
00054 bool Cmp(int op, T1 v1, T2 v2)
00055 {
00056     switch (op) {
00057         case SCAN_EQUAL:
00058             return v1 == v2;
00059         case SCAN_NOT_EQUAL:
00060             return v1 != v2;
00061         case SCAN_GREATER:
00062             return v1 > v2;
00063         case SCAN_GREATER_EQL:
00064             return v1 >= v2;
00065         case SCAN_LESS:
00066             return v1 < v2;
00067         case SCAN_LESS_EQL:
00068             return v1 <= v2;
00069         case SCAN_REGEXP:
00070             throw Error(malformed_expr, "Regular expressions are supported for strings only.");
00071         default:
00072             throw Error(malformed_expr, "Unrecognized operator.");
00073     }
00074 }
00075 
00076 template<class T>
00077 static inline unsigned long long dap_floor_zero(T i)
00078 {
00079     return (unsigned long long) ((i < 0) ? 0 : i);
00080 }
00081 
00090 template<class UT1, class T2>
00091 bool USCmp(int op, UT1 v1, T2 v2)
00092 {
00093     switch (op) {
00094         case SCAN_EQUAL:
00095             return v1 == dap_floor_zero<T2>(v2);
00096         case SCAN_NOT_EQUAL:
00097             return v1 != dap_floor_zero<T2>(v2);
00098         case SCAN_GREATER:
00099             return v1 > dap_floor_zero<T2>(v2);
00100         case SCAN_GREATER_EQL:
00101             return v1 >= dap_floor_zero<T2>(v2);
00102         case SCAN_LESS:
00103             return v1 < dap_floor_zero<T2>(v2);
00104         case SCAN_LESS_EQL:
00105             return v1 <= dap_floor_zero<T2>(v2);
00106         case SCAN_REGEXP:
00107             throw Error(malformed_expr, "Regular expressions are supported for strings only.");
00108         default:
00109             throw Error(malformed_expr, "Unrecognized operator.");
00110     }
00111 }
00112 
00125 template<class T1, class UT2>
00126 bool SUCmp(int op, T1 v1, UT2 v2)
00127 {
00128     switch (op) {
00129         case SCAN_EQUAL:
00130             return dap_floor_zero<T1>(v1) == v2;
00131         case SCAN_NOT_EQUAL:
00132             return dap_floor_zero<T1>(v1) != v2;
00133         case SCAN_GREATER:
00134             return dap_floor_zero<T1>(v1) > v2;
00135         case SCAN_GREATER_EQL:
00136             return dap_floor_zero<T1>(v1) >= v2;
00137         case SCAN_LESS:
00138             return dap_floor_zero<T1>(v1) < v2;
00139         case SCAN_LESS_EQL:
00140             return dap_floor_zero<T1>(v1) <= v2;
00141         case SCAN_REGEXP:
00142             throw Error(malformed_expr, "Regular expressions are supported for strings only.");
00143         default:
00144             throw Error(malformed_expr, "Unrecognized operator.");
00145     }
00146 }
00147 
00153 template<class T1, class T2>
00154 bool StrCmp(int op, T1 v1, T2 v2)
00155 {
00156     switch (op) {
00157         case SCAN_EQUAL:
00158             return v1 == v2;
00159         case SCAN_NOT_EQUAL:
00160             return v1 != v2;
00161         case SCAN_GREATER:
00162             return v1 > v2;
00163         case SCAN_GREATER_EQL:
00164             return v1 >= v2;
00165         case SCAN_LESS:
00166             return v1 < v2;
00167         case SCAN_LESS_EQL:
00168             return v1 <= v2;
00169         case SCAN_REGEXP: {
00170             Regex r(v2.c_str());
00171             return r.match(v1.c_str(), v1.length()) > 0;
00172         }
00173         default:
00174             throw Error(malformed_expr, "Unrecognized operator.");
00175     }
00176 }
00177 
00178 } // namespace libdap
00179 
00180 #endif // _operators_h