File: D4RValue.cc

package info (click to toggle)
libdap 3.20.7-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 24,448 kB
  • sloc: cpp: 52,490; sh: 40,745; xml: 23,511; ansic: 20,030; yacc: 2,520; exp: 1,544; makefile: 1,057; lex: 309; perl: 52; fortran: 8
file content (320 lines) | stat: -rw-r--r-- 9,563 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2014 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

#include "config.h"

#include <iostream>

#include "BaseType.h"
#include "Array.h"
#include "Byte.h"
#include "Int8.h"
#include "UInt16.h"
#include "Int16.h"
#include "UInt32.h"
#include "Int32.h"
#include "UInt64.h"
#include "Int64.h"
#include "Float32.h"
#include "Float64.h"
#include "Str.h"

#include "D4RValue.h"
#include "InternalErr.h"

#include "dods-datatypes.h"
#include "dods-limits.h"
#include "parser-util.h"
#include "util.h"

using namespace std;

namespace libdap {

void
D4RValueList::m_duplicate(const D4RValueList &src)
{
    for (std::vector<D4RValue *>::const_iterator i = src.d_rvalues.begin(), e = src.d_rvalues.end(); i != e; ++i) {
        D4RValue *rv = *i;
        d_rvalues.push_back(new D4RValue(*rv));
    }
}

D4RValueList::~D4RValueList()
{
    for (std::vector<D4RValue *>::iterator i = d_rvalues.begin(), e = d_rvalues.end(); i != e; ++i)
        delete *i;
}

void
D4RValue::m_duplicate(const D4RValue &src)
{
    d_value_kind = src.d_value_kind;

    d_variable = src.d_variable;    // weak pointers

    d_func = src.d_func;
    d_args = (src.d_args != 0) ? new D4RValueList(*src.d_args) : 0; // deep copy these

    d_constant = (src.d_constant != 0) ? src.d_constant->ptr_duplicate() : 0;
}

template<typename T, class DAP_TYPE>
static BaseType *
build_constant_array(vector<T> &values, DAP_TYPE &dt)
{
    Array *array = new Array("", &dt);
    array->append_dim(values.size());

    // TODO Make set_value_nocopy() methods so that values' pointers can be copied
    // instead of allocating memory twice. jhrg 7/5/13

    array->set_value(values, values.size());

    array->set_read_p(true);

    static unsigned long counter = 1;
    array->set_name(string("g") + long_to_string(counter++));

    return array;
}

D4RValue::D4RValue(unsigned long long ull) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant)
{
	UInt64 *ui = new UInt64("constant");
	ui->set_value(ull);
	d_constant = ui;
}

D4RValue::D4RValue(long long ll) : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Int64 *i = new Int64("constant");
	i->set_value(ll);
	d_constant = i;
}

D4RValue::D4RValue(double r) : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Float64 *f = new Float64("constant");
	f->set_value(r);
	d_constant = f;
}

D4RValue::D4RValue(std::string cpps) : d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Str *s = new Str("constant");
	s->set_value(remove_quotes(cpps));
	d_constant = s;
}

D4RValue::D4RValue(std::vector<dods_byte> &byte_args)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Byte b("");
	d_constant = build_constant_array(byte_args, b);
}

D4RValue::D4RValue(std::vector<dods_int8> &byte_int8)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Int8 b("");
	d_constant = build_constant_array(byte_int8, b);
}

D4RValue::D4RValue(std::vector<dods_uint16> &byte_uint16)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	UInt16 b("");
	d_constant = build_constant_array(byte_uint16, b);
}

D4RValue::D4RValue(std::vector<dods_int16> &byte_int16)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Int16 b("");
	d_constant = build_constant_array(byte_int16, b);
}

D4RValue::D4RValue(std::vector<dods_uint32> &byte_uint32)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	UInt32 b("");
	d_constant = build_constant_array(byte_uint32, b);
}

D4RValue::D4RValue(std::vector<dods_int32> &byte_int32)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Int32 b("");
	d_constant = build_constant_array(byte_int32, b);
}

D4RValue::D4RValue(std::vector<dods_uint64> &byte_uint64)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	UInt64 b("");
	d_constant = build_constant_array(byte_uint64, b);
}

D4RValue::D4RValue(std::vector<dods_int64> &byte_int64)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Int64 b("");
	d_constant = build_constant_array(byte_int64, b);
}

D4RValue::D4RValue(std::vector<dods_float32> &byte_float32)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Float32 b("");
	d_constant = build_constant_array(byte_float32, b);
}

D4RValue::D4RValue(std::vector<dods_float64> &byte_float64)
	: d_variable(0), d_func(0), d_args(0),  d_constant(0), d_value_kind(constant)
{
	Float64 b("");
	d_constant = build_constant_array(byte_float64, b);
}

D4RValue::~D4RValue() {
	// d_variable and d_func are weak pointers; don't delete.
	delete d_args;
	delete d_constant;
}

/**
 * @brief Build an appropriate RValue
 *
 * Look at the value in the string parameter and build an appropriate
 * BaseType, use that as a constant and build an RValue. This can be used
 * by the DAP4 parser directly to build the constants in filter clauses.
 *
 * @param cpps The string argument read by the parser.
 * @return A D4RValue pointer.
 */
D4RValue *D4RValueFactory(std::string cpps)
{
    char *ptr;

    // First check if the string is a uint64, ..., then convert it.
    // Since the check_* function use the strtoull() functions, no
    // need to test for errors when building the actual values.
    if (check_uint64(cpps.c_str())) {
        return new D4RValue(strtoull(cpps.c_str(), &ptr, 0));
    }
    else if (check_int64(cpps.c_str())) {
        return new D4RValue(strtoll(cpps.c_str(), &ptr, 0));
    }
    else if (check_float64(cpps.c_str())) {
#ifdef WIN32
        return new D4RValue(w32strtod(cpps.c_str(), &ptr));
#else
        return new D4RValue(strtod(cpps.c_str(), &ptr));
#endif
    }
    else {
        return new D4RValue(cpps);
    }
}

/**
 * @brief Get the value for a RValue object
 * Return the BaseType * for a given RValue. For a dataset variable, read the
 * variable's value and, for a function, evaluate that function. Since read()
 * is called for a dataset variable each time this method is called, if the
 * variable is part of a Sequence, the next value in the sequence will be returned.
 * However, since this code also sets the read_p property after calling read(),
 * if the variable does not have a new value, read() will not be called (using the
 * read_p property, the read() method is called only when the variable has a new
 * value to be read.
 *
 * @note Unlike the DAP2 functions, we have an easier-to-follow memory model for
 * function values. The values (BaseType*) returned by this method will be packaged
 * up in a RValueList and deleted when that list is deleted. Constant values and
 * function result values will be deleted at that time; variables will not. Thus
 * Server Functions should always allocate storage for their return values.
 *
 * @todo Could move the operation that wraps a constant in a BaseType to this method
 * while providing other ways to access the value(s) (methods to determine if the
 * rvalue is a constant and what DAP type it is, e.g.). This would provide an optimization
 * for the filter evaluator which may access the values many times. We might also
 * modify the server side functions so they could access constant values more efficiently.
 *
 * @param dmr The DMR to pass to a function.
 * @return A BaseType* that holds the value.
 */
BaseType *
D4RValue::value(DMR &dmr)
{
	switch (d_value_kind) {
	case basetype:
		d_variable->read();
		d_variable->set_read_p(true);
		return d_variable;

	case function:
		return (*d_func)(d_args, dmr);

	case constant:
		return d_constant;

	default:
		throw InternalErr(__FILE__, __LINE__, "Unknown rvalue type.");
	}
}

/**
 * @brief Get the value for a RValue object
 *
 * This version of value() will not work for function RValues, but has the advantage that
 * it can be used more easily for the D4RValue objects built for, and stored in, D4Filter-
 * Clause instances.
 *
 * @see D4RValue::value(DMR&)
 * @return The value wrapped in a BaseType*
 */
BaseType *
D4RValue::value()
{
    switch (d_value_kind) {
    case basetype:
        d_variable->read();
        d_variable->set_read_p(true);
        return d_variable;

    case function:
        throw Error(malformed_expr, "An expression that included a function call was used in a place where that won't work.");

    case constant:
        return d_constant;

    default:
        throw InternalErr(__FILE__, __LINE__, "Unknown rvalue type.");
    }
}

} // namespace libdap