36
35
#ifndef _operators_h
37
36
#define _operators_h
40
38
#include "GNURegex.h" // GNU Regex class used for string =~ op.
41
39
#include "parser.h" // for ID_MAX
42
40
#include "ce_expr.tab.hh"
44
42
using namespace std;
50
dods_max(int i1, int i2)
52
return (unsigned)((i1 > i2) ? i1 : i2);
55
46
/** Compare two numerical types, both of which are either signed or unsigned.
56
This class is one implementation of the comparison policy used by
62
template<class T1, class T2> class Cmp
65
static bool eq(T1 v1, T2 v2)
69
static bool ne(T1 v1, T2 v2)
73
static bool gr(T1 v1, T2 v2)
77
static bool ge(T1 v1, T2 v2)
81
static bool lt(T1 v1, T2 v2)
85
static bool le(T1 v1, T2 v2)
89
static bool re(T1, T2)
91
cerr << "Illegal operation" << endl;
47
This class is one implementation of the comparison policy used by
53
template<class T1, class T2>
54
bool Cmp(int op, T1 v1, T2 v2)
63
case SCAN_GREATER_EQL:
70
throw Error("Regular expressions are supported for strings only.");
72
throw Error("Unrecognized operator.");
77
static inline unsigned long long dap_floor_zero(T i)
79
return (unsigned long long) ((i < 0) ? 0 : i);
96
82
/** Compare two numerical types, the first one unsigned and the second
97
signed. If the signed argument is negative, zero is used in the
98
comparison. This class is one implementation of the comparison policy
83
signed. If the signed argument is negative, zero is used in the
84
comparison. This class is one implementation of the comparison policy
104
template<class UT1, class T2> class USCmp
90
template<class UT1, class T2>
91
bool USCmp(int op, UT1 v1, T2 v2)
107
static bool eq(UT1 v1, T2 v2)
109
return v1 == dods_max(0, v2);
111
static bool ne(UT1 v1, T2 v2)
113
return v1 != dods_max(0, v2);
115
static bool gr(UT1 v1, T2 v2)
117
return v1 > dods_max(0, v2);
119
static bool ge(UT1 v1, T2 v2)
121
return v1 >= dods_max(0, v2);
123
static bool lt(UT1 v1, T2 v2)
125
return v1 < dods_max(0, v2);
127
static bool le(UT1 v1, T2 v2)
129
return v1 <= dods_max(0, v2);
131
static bool re(UT1, T2)
133
cerr << "Illegal operation" << endl;
95
return v1 == dap_floor_zero<T2>(v2);
97
return v1 != dap_floor_zero<T2>(v2);
99
return v1 > dap_floor_zero<T2>(v2);
100
case SCAN_GREATER_EQL:
101
return v1 >= dap_floor_zero<T2>(v2);
103
return v1 < dap_floor_zero<T2>(v2);
105
return v1 <= dap_floor_zero<T2>(v2);
107
throw Error("Regular expressions are supported for strings only.");
109
throw Error("Unrecognized operator.");
138
113
/** Compare two numerical types, the first one signed and the second
139
unsigned. If the signed argument is negative, zero is used in the
140
comparison. This class is one implementation of the comparison policy
141
used by rops. This class is here to make writing the Byte::ops, ...
142
member functions simpler. It is not necessary since the functions could
143
twiddle the order of arguments to rops and use <tt>USCmp</tt>. Having
144
this class make Byte:ops, ... simpler to read and write.
114
unsigned. If the signed argument is negative, zero is used in the
115
comparison. This class is one implementation of the comparison policy
116
used by rops. This class is here to make writing the Byte::ops, ...
117
member functions simpler. It is not necessary since the functions could
118
twidle the order of arguments to rops and use <tt>USCmp</tt>. Having
119
this class make Byte:ops, ... simpler to read and write.
150
template<class T1, class UT2> class SUCmp
125
template<class T1, class UT2>
126
bool SUCmp(int op, T1 v1, UT2 v2)
153
static bool eq(T1 v1, UT2 v2)
155
return dods_max(0, v1) == v2;
157
static bool ne(T1 v1, UT2 v2)
159
return dods_max(0, v1) != v2;
161
static bool gr(T1 v1, UT2 v2)
163
return dods_max(0, v1) > v2;
165
static bool ge(T1 v1, UT2 v2)
167
return dods_max(0, v1) >= v2;
169
static bool lt(T1 v1, UT2 v2)
171
return dods_max(0, v1) < v2;
173
static bool le(T1 v1, UT2 v2)
175
return dods_max(0, v1) <= v2;
177
static bool re(T1, UT2)
179
cerr << "Illegal operation" << endl;
130
return dap_floor_zero<T1>(v1) == v2;
132
return dap_floor_zero<T1>(v1) != v2;
134
return dap_floor_zero<T1>(v1) > v2;
135
case SCAN_GREATER_EQL:
136
return dap_floor_zero<T1>(v1) >= v2;
138
return dap_floor_zero<T1>(v1) < v2;
140
return dap_floor_zero<T1>(v1) <= v2;
142
throw Error("Regular expressions are supported for strings only.");
144
throw Error("Unrecognized operator.");
184
148
/** Compare two string types.
185
This class is one implementation of the comparison policy used by
189
template<class T1, class T2> class StrCmp
192
static bool eq(T1 v1, T2 v2)
196
static bool ne(T1 v1, T2 v2)
200
static bool gr(T1 v1, T2 v2)
204
static bool ge(T1 v1, T2 v2)
208
static bool lt(T1 v1, T2 v2)
212
static bool le(T1 v1, T2 v2)
216
static bool re(T1 v1, T2 v2)
219
return r.match(v1.c_str(), v1.length()) > 0;
223
/** This template function is used to compare two values of two instances of
224
the DAP2 simple types (Byte, ..., Str). The function does not take the
225
DAP2 objects as arguments; the caller must access the values of those
226
objects and pass them to this function. The reason for this is that all
227
the possible functions that could be generated from this template would
228
have to be explicitly listed as friend functions in each of the DAP2
229
simple type classes. In the current implementation, only the simple type
230
classes must be friends - to see why, look at Byte::ops and note that it
231
accesses the <tt>_buf</tt> member of Int16, ..., Float64 and thus must be a
232
friend of those classes.
234
NB: This would all be simpler if: 1) g++ supported template friend
235
functions (without explicit listing of all the template's arguments). 2)
236
we did not have unsigned types.
238
T1 The type of <tt>a</tt>.
240
T2 The type of <tt>b</tt>.
242
C A class which implements the policy used for comparing <tt>a</tt>
245
@param a The first argument.
246
@param b The second argument.
247
@param op The relational operator.
250
template<class T1, class T2, class C>
251
bool rops(T1 a, T2 b, int op)
149
This class is one implementation of the comparison policy used by
153
template<class T1, class T2>
154
bool StrCmp(int op, T1 v1, T2 v2)
260
case SCAN_GREATER_EQL:
269
cerr << "Unknown operator" << endl;
163
case SCAN_GREATER_EQL:
171
return r.match(v1.c_str(), v1.length()) > 0;
174
throw Error("Unrecognized operator.");