~ubuntu-branches/ubuntu/vivid/libdap/vivid

« back to all changes in this revision

Viewing changes to Operators.h

  • Committer: Package Import Robot
  • Author(s): Alastair McKinstry
  • Date: 2013-10-17 22:12:11 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20131017221211-re1r37k4d6wrtmeq
Tags: 3.12.0-1
* New upstream release.
  - No longer need curl-types-remove.patch 
* Add debian/watch file.
* Fix typo in hardening flags; Change  to =all,-pie ; 
  Use DEB_LDFLAGS_MAINT_APPEND. Closes: #697387.
* Enable parallel build. Closes: #723938.
* Depend on libcurl4-gnutls-dev | libcurl-dev. Closes: #722701.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
// -*- mode: c++; c-basic-offset:4 -*-
3
2
 
4
3
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
19
18
//
20
19
// You should have received a copy of the GNU Lesser General Public
21
20
// License along with this library; if not, write to the Free Software
22
 
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
22
//
24
23
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
24
 
36
35
#ifndef _operators_h
37
36
#define _operators_h
38
37
 
39
 
 
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"
43
41
 
44
42
using namespace std;
45
43
 
46
 
namespace libdap
47
 
{
48
 
 
49
 
inline unsigned
50
 
dods_max(int i1, int i2)
51
 
{
52
 
    return (unsigned)((i1 > i2) ? i1 : i2);
53
 
}
 
44
namespace libdap {
54
45
 
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
57
 
    rops.
58
 
 
59
 
    @see rops
60
 
    @see USCmp
61
 
    @see SUCmp */
62
 
template<class T1, class T2> class Cmp
63
 
{
64
 
public:
65
 
    static bool eq(T1 v1, T2 v2)
66
 
    {
67
 
        return v1 == v2;
68
 
    }
69
 
    static bool ne(T1 v1, T2 v2)
70
 
    {
71
 
        return v1 != v2;
72
 
    }
73
 
    static bool gr(T1 v1, T2 v2)
74
 
    {
75
 
        return v1 > v2;
76
 
    }
77
 
    static bool ge(T1 v1, T2 v2)
78
 
    {
79
 
        return v1 >= v2;
80
 
    }
81
 
    static bool lt(T1 v1, T2 v2)
82
 
    {
83
 
        return v1 < v2;
84
 
    }
85
 
    static bool le(T1 v1, T2 v2)
86
 
    {
87
 
        return v1 <= v2;
88
 
    }
89
 
    static bool re(T1, T2)
90
 
    {
91
 
        cerr << "Illegal operation" << endl;
92
 
        return false;
93
 
    }
94
 
};
 
47
 This class is one implementation of the comparison policy used by
 
48
 rops.
 
49
 
 
50
 @see rops
 
51
 @see USCmp
 
52
 @see SUCmp */
 
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
}
95
81
 
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
99
 
    used by rops.
 
83
 signed. If the signed argument is negative, zero is used in the
 
84
 comparison. This class is one implementation of the comparison policy
 
85
 used by rops.
100
86
 
101
 
    @see rops
102
 
    @see SUCmp
103
 
    @see Cmp */
104
 
template<class UT1, class T2> class USCmp
 
87
 @see rops
 
88
 @see SUCmp
 
89
 @see Cmp */
 
90
template<class UT1, class T2>
 
91
bool USCmp(int op, UT1 v1, T2 v2)
105
92
{
106
 
public:
107
 
    static bool eq(UT1 v1, T2 v2)
108
 
    {
109
 
        return v1 == dods_max(0, v2);
110
 
    }
111
 
    static bool ne(UT1 v1, T2 v2)
112
 
    {
113
 
        return v1 != dods_max(0, v2);
114
 
    }
115
 
    static bool gr(UT1 v1, T2 v2)
116
 
    {
117
 
        return v1 > dods_max(0, v2);
118
 
    }
119
 
    static bool ge(UT1 v1, T2 v2)
120
 
    {
121
 
        return v1 >= dods_max(0, v2);
122
 
    }
123
 
    static bool lt(UT1 v1, T2 v2)
124
 
    {
125
 
        return v1 < dods_max(0, v2);
126
 
    }
127
 
    static bool le(UT1 v1, T2 v2)
128
 
    {
129
 
        return v1 <= dods_max(0, v2);
130
 
    }
131
 
    static bool re(UT1, T2)
132
 
    {
133
 
        cerr << "Illegal operation" << endl;
134
 
        return false;
135
 
    }
136
 
};
 
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
}
137
112
 
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.
145
120
 
146
 
    @see Byte::ops
147
 
    @see USCmp
148
 
    @see Cmp
149
 
    @see ops */
150
 
template<class T1, class UT2> class SUCmp
 
121
 @see Byte::ops
 
122
 @see USCmp
 
123
 @see Cmp
 
124
 @see ops */
 
125
template<class T1, class UT2>
 
126
bool SUCmp(int op, T1 v1, UT2 v2)
151
127
{
152
 
public:
153
 
    static bool eq(T1 v1, UT2 v2)
154
 
    {
155
 
        return dods_max(0, v1) == v2;
156
 
    }
157
 
    static bool ne(T1 v1, UT2 v2)
158
 
    {
159
 
        return dods_max(0, v1) != v2;
160
 
    }
161
 
    static bool gr(T1 v1, UT2 v2)
162
 
    {
163
 
        return dods_max(0, v1) > v2;
164
 
    }
165
 
    static bool ge(T1 v1, UT2 v2)
166
 
    {
167
 
        return dods_max(0, v1) >= v2;
168
 
    }
169
 
    static bool lt(T1 v1, UT2 v2)
170
 
    {
171
 
        return dods_max(0, v1) < v2;
172
 
    }
173
 
    static bool le(T1 v1, UT2 v2)
174
 
    {
175
 
        return dods_max(0, v1) <= v2;
176
 
    }
177
 
    static bool re(T1, UT2)
178
 
    {
179
 
        cerr << "Illegal operation" << endl;
180
 
        return false;
181
 
    }
182
 
};
 
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
}
183
147
 
184
148
/** Compare two string types.
185
 
    This class is one implementation of the comparison policy used by
186
 
    rops.
187
 
 
188
 
    @see rops */
189
 
template<class T1, class T2> class StrCmp
190
 
{
191
 
public:
192
 
    static bool eq(T1 v1, T2 v2)
193
 
    {
194
 
        return v1 == v2;
195
 
    }
196
 
    static bool ne(T1 v1, T2 v2)
197
 
    {
198
 
        return v1 != v2;
199
 
    }
200
 
    static bool gr(T1 v1, T2 v2)
201
 
    {
202
 
        return v1 > v2;
203
 
    }
204
 
    static bool ge(T1 v1, T2 v2)
205
 
    {
206
 
        return v1 >= v2;
207
 
    }
208
 
    static bool lt(T1 v1, T2 v2)
209
 
    {
210
 
        return v1 < v2;
211
 
    }
212
 
    static bool le(T1 v1, T2 v2)
213
 
    {
214
 
        return v1 <= v2;
215
 
    }
216
 
    static bool re(T1 v1, T2 v2)
217
 
    {
218
 
        Regex r(v2.c_str());
219
 
        return r.match(v1.c_str(), v1.length()) > 0;
220
 
    }
221
 
};
222
 
 
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.
233
 
 
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.
237
 
 
238
 
    T1 The type of <tt>a</tt>.
239
 
 
240
 
    T2 The type of <tt>b</tt>.
241
 
 
242
 
    C A class which implements the policy used for comparing <tt>a</tt>
243
 
    and <tt>b</tt>.
244
 
 
245
 
    @param a The first argument.
246
 
    @param b The second argument.
247
 
    @param op The relational operator.
248
 
    @see Byte::ops */
249
 
 
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
 
150
 rops.
 
151
 
 
152
 @see rops */
 
153
template<class T1, class T2>
 
154
bool StrCmp(int op, T1 v1, T2 v2)
252
155
{
253
156
    switch (op) {
254
 
    case SCAN_EQUAL:
255
 
        return C::eq(a, b);
256
 
    case SCAN_NOT_EQUAL:
257
 
        return C::ne(a, b);
258
 
    case SCAN_GREATER:
259
 
        return C::gr(a, b);
260
 
    case SCAN_GREATER_EQL:
261
 
        return C::ge(a, b);
262
 
    case SCAN_LESS:
263
 
        return C::lt(a, b);
264
 
    case SCAN_LESS_EQL:
265
 
        return C::le(a, b);
266
 
    case SCAN_REGEXP:
267
 
        return C::re(a, b);
268
 
    default:
269
 
        cerr << "Unknown operator" << endl;
270
 
        return false;
 
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.");
271
175
    }
272
176
}
273
177