libdap++  Updated for version 3.12.0
Operators.h
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2002,2003 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 // (c) COPYRIGHT URI/MIT 1999
26 // Please read the full copyright statement in the file COPYRIGHT_URI.
27 //
28 // Authors:
29 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
30 
31 // Templates for relational operations.
32 //
33 // jhrg 3/24/99
34 
35 #ifndef _operators_h
36 #define _operators_h
37 
38 #include "GNURegex.h" // GNU Regex class used for string =~ op.
39 #include "parser.h" // for ID_MAX
40 #include "ce_expr.tab.hh"
41 
42 using namespace std;
43 
44 namespace libdap {
45 
53 template<class T1, class T2>
54 bool Cmp(int op, T1 v1, T2 v2)
55 {
56  switch (op) {
57  case SCAN_EQUAL:
58  return v1 == v2;
59  case SCAN_NOT_EQUAL:
60  return v1 != v2;
61  case SCAN_GREATER:
62  return v1 > v2;
63  case SCAN_GREATER_EQL:
64  return v1 >= v2;
65  case SCAN_LESS:
66  return v1 < v2;
67  case SCAN_LESS_EQL:
68  return v1 <= v2;
69  case SCAN_REGEXP:
70  throw Error("Regular expressions are supported for strings only.");
71  default:
72  throw Error("Unrecognized operator.");
73  }
74 }
75 
76 template<class T>
77 static inline unsigned long long dap_floor_zero(T i)
78 {
79  return (unsigned long long) ((i < 0) ? 0 : i);
80 }
81 
90 template<class UT1, class T2>
91 bool USCmp(int op, UT1 v1, T2 v2)
92 {
93  switch (op) {
94  case SCAN_EQUAL:
95  return v1 == dap_floor_zero<T2>(v2);
96  case SCAN_NOT_EQUAL:
97  return v1 != dap_floor_zero<T2>(v2);
98  case SCAN_GREATER:
99  return v1 > dap_floor_zero<T2>(v2);
100  case SCAN_GREATER_EQL:
101  return v1 >= dap_floor_zero<T2>(v2);
102  case SCAN_LESS:
103  return v1 < dap_floor_zero<T2>(v2);
104  case SCAN_LESS_EQL:
105  return v1 <= dap_floor_zero<T2>(v2);
106  case SCAN_REGEXP:
107  throw Error("Regular expressions are supported for strings only.");
108  default:
109  throw Error("Unrecognized operator.");
110  }
111 }
112 
125 template<class T1, class UT2>
126 bool SUCmp(int op, T1 v1, UT2 v2)
127 {
128  switch (op) {
129  case SCAN_EQUAL:
130  return dap_floor_zero<T1>(v1) == v2;
131  case SCAN_NOT_EQUAL:
132  return dap_floor_zero<T1>(v1) != v2;
133  case SCAN_GREATER:
134  return dap_floor_zero<T1>(v1) > v2;
135  case SCAN_GREATER_EQL:
136  return dap_floor_zero<T1>(v1) >= v2;
137  case SCAN_LESS:
138  return dap_floor_zero<T1>(v1) < v2;
139  case SCAN_LESS_EQL:
140  return dap_floor_zero<T1>(v1) <= v2;
141  case SCAN_REGEXP:
142  throw Error("Regular expressions are supported for strings only.");
143  default:
144  throw Error("Unrecognized operator.");
145  }
146 }
147 
153 template<class T1, class T2>
154 bool StrCmp(int op, T1 v1, T2 v2)
155 {
156  switch (op) {
157  case SCAN_EQUAL:
158  return v1 == v2;
159  case SCAN_NOT_EQUAL:
160  return v1 != v2;
161  case SCAN_GREATER:
162  return v1 > v2;
163  case SCAN_GREATER_EQL:
164  return v1 >= v2;
165  case SCAN_LESS:
166  return v1 < v2;
167  case SCAN_LESS_EQL:
168  return v1 <= v2;
169  case SCAN_REGEXP: {
170  Regex r(v2.c_str());
171  return r.match(v1.c_str(), v1.length()) > 0;
172  }
173  default:
174  throw Error("Unrecognized operator.");
175  }
176 }
177 
178 } // namespace libdap
179 
180 #endif // _operators_h
bool Cmp(int op, T1 v1, T2 v2)
Definition: Operators.h:54
bool USCmp(int op, UT1 v1, T2 v2)
Definition: Operators.h:91
bool StrCmp(int op, T1 v1, T2 v2)
Definition: Operators.h:154
bool SUCmp(int op, T1 v1, UT2 v2)
Definition: Operators.h:126
A class for error processing.
Definition: Error.h:90