libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4RValue.cc
Go to the documentation of this file.
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2014 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#include "config.h"
27
28#include <iostream>
29
30#include "Array.h"
31#include "BaseType.h"
32#include "Byte.h"
33#include "Float32.h"
34#include "Float64.h"
35#include "Int16.h"
36#include "Int32.h"
37#include "Int64.h"
38#include "Int8.h"
39#include "Str.h"
40#include "UInt16.h"
41#include "UInt32.h"
42#include "UInt64.h"
43
44#include "D4RValue.h"
45#include "InternalErr.h"
46
47#include "dods-datatypes.h"
48#include "dods-limits.h"
49#include "parser-util.h"
50#include "util.h"
51
52using namespace std;
53
54namespace libdap {
55
56void D4RValueList::m_duplicate(const D4RValueList &src) {
57 for (std::vector<D4RValue *>::const_iterator i = src.d_rvalues.begin(), e = src.d_rvalues.end(); i != e; ++i) {
58 D4RValue *rv = *i;
59 d_rvalues.push_back(new D4RValue(*rv));
60 }
61}
62
64 for (std::vector<D4RValue *>::iterator i = d_rvalues.begin(), e = d_rvalues.end(); i != e; ++i)
65 delete *i;
66}
67
68void D4RValue::m_duplicate(const D4RValue &src) {
69 d_value_kind = src.d_value_kind;
70
71 d_variable = src.d_variable; // weak pointers
72
73 d_func = src.d_func;
74 d_args = (src.d_args != 0) ? new D4RValueList(*src.d_args) : 0; // deep copy these
75
76 d_constant = (src.d_constant != 0) ? src.d_constant->ptr_duplicate() : 0;
77}
78
79template <typename T, class DAP_TYPE> static BaseType *build_constant_array(vector<T> &values, DAP_TYPE &dt) {
80 Array *array = new Array("", &dt);
81 array->append_dim(values.size());
82
83 // TODO Make set_value_nocopy() methods so that values' pointers can be copied
84 // instead of allocating memory twice. jhrg 7/5/13
85
86 array->set_value(values, values.size());
87
88 array->set_read_p(true);
89
90 static unsigned long counter = 1;
91 array->set_name(string("g") + long_to_string(counter++));
92
93 return array;
94}
95
96D4RValue::D4RValue(unsigned long long ull)
97 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
98 UInt64 *ui = new UInt64("constant");
99 ui->set_value(ull);
100 d_constant = ui;
101}
102
103D4RValue::D4RValue(long long ll) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
104 Int64 *i = new Int64("constant");
105 i->set_value(ll);
106 d_constant = i;
107}
108
109D4RValue::D4RValue(double r) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
110 Float64 *f = new Float64("constant");
111 f->set_value(r);
112 d_constant = f;
113}
114
115D4RValue::D4RValue(std::string cpps) : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
116 Str *s = new Str("constant");
117 s->set_value(remove_quotes(cpps));
118 d_constant = s;
119}
120
121D4RValue::D4RValue(std::vector<dods_byte> &byte_args)
122 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
123 Byte b("");
124 d_constant = build_constant_array(byte_args, b);
125}
126
127D4RValue::D4RValue(std::vector<dods_int8> &byte_int8)
128 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
129 Int8 b("");
130 d_constant = build_constant_array(byte_int8, b);
131}
132
133D4RValue::D4RValue(std::vector<dods_uint16> &byte_uint16)
134 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
135 UInt16 b("");
136 d_constant = build_constant_array(byte_uint16, b);
137}
138
139D4RValue::D4RValue(std::vector<dods_int16> &byte_int16)
140 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
141 Int16 b("");
142 d_constant = build_constant_array(byte_int16, b);
143}
144
145D4RValue::D4RValue(std::vector<dods_uint32> &byte_uint32)
146 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
147 UInt32 b("");
148 d_constant = build_constant_array(byte_uint32, b);
149}
150
151D4RValue::D4RValue(std::vector<dods_int32> &byte_int32)
152 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
153 Int32 b("");
154 d_constant = build_constant_array(byte_int32, b);
155}
156
157D4RValue::D4RValue(std::vector<dods_uint64> &byte_uint64)
158 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
159 UInt64 b("");
160 d_constant = build_constant_array(byte_uint64, b);
161}
162
163D4RValue::D4RValue(std::vector<dods_int64> &byte_int64)
164 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
165 Int64 b("");
166 d_constant = build_constant_array(byte_int64, b);
167}
168
169D4RValue::D4RValue(std::vector<dods_float32> &byte_float32)
170 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
171 Float32 b("");
172 d_constant = build_constant_array(byte_float32, b);
173}
174
175D4RValue::D4RValue(std::vector<dods_float64> &byte_float64)
176 : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(constant) {
177 Float64 b("");
178 d_constant = build_constant_array(byte_float64, b);
179}
180
182 // d_variable and d_func are weak pointers; don't delete.
183 delete d_args;
184 delete d_constant;
185}
186
197D4RValue *D4RValueFactory(std::string cpps) {
198 char *ptr;
199
200 // First check if the string is a uint64, ..., then convert it.
201 // Since the check_* function use the strtoull() functions, no
202 // need to test for errors when building the actual values.
203 if (check_uint64(cpps.c_str())) {
204 return new D4RValue(strtoull(cpps.c_str(), &ptr, 0));
205 } else if (check_int64(cpps.c_str())) {
206 return new D4RValue(strtoll(cpps.c_str(), &ptr, 0));
207 } else if (check_float64(cpps.c_str())) {
208#ifdef WIN32
209 return new D4RValue(w32strtod(cpps.c_str(), &ptr));
210#else
211 return new D4RValue(strtod(cpps.c_str(), &ptr));
212#endif
213 } else {
214 return new D4RValue(cpps);
215 }
216}
217
245 switch (d_value_kind) {
246 case basetype:
247 d_variable->read();
248 d_variable->set_read_p(true);
249 return d_variable;
250
251 case function:
252 return (*d_func)(d_args, dmr);
253
254 case constant:
255 return d_constant;
256
257 default:
258 throw InternalErr(__FILE__, __LINE__, "Unknown rvalue type.");
259 }
260}
261
273 switch (d_value_kind) {
274 case basetype:
275 d_variable->read();
276 d_variable->set_read_p(true);
277 return d_variable;
278
279 case function:
280 throw Error(malformed_expr,
281 "An expression that included a function call was used in a place where that won't work.");
282
283 case constant:
284 return d_constant;
285
286 default:
287 throw InternalErr(__FILE__, __LINE__, "Unknown rvalue type.");
288 }
289}
290
291} // namespace libdap
#define malformed_expr
(400)
Definition Error.h:66
The basic data type for the DODS DAP types.
Definition BaseType.h:118
virtual BaseType * ptr_duplicate()=0
Holds a single byte.
Definition Byte.h:59
virtual ~D4RValueList()
Definition D4RValue.cc:63
virtual BaseType * value()
Get the value for a RValue object.
Definition D4RValue.cc:272
friend class D4RValueList
Definition D4RValue.h:96
virtual ~D4RValue()
Definition D4RValue.cc:181
A class for error processing.
Definition Error.h:92
Holds a 32-bit floating point value.
Definition Float32.h:59
Holds a 64-bit (double precision) floating point value.
Definition Float64.h:58
virtual bool set_value(dods_float64 val)
Definition Float64.cc:185
Holds a 16-bit signed integer value.
Definition Int16.h:57
Holds a 32-bit signed integer.
Definition Int32.h:63
Holds a64-bit signed integer.
Definition Int64.h:48
virtual bool set_value(dods_int64 i)
Definition Int64.cc:137
Holds an 8-bit signed integer value.
Definition Int8.h:41
A class for software fault reporting.
Definition InternalErr.h:61
Holds character string data.
Definition Str.h:61
virtual bool set_value(const string &value)
Definition Str.cc:219
Holds an unsigned 16-bit integer.
Definition UInt16.h:55
Holds a 32-bit unsigned integer.
Definition UInt32.h:57
Holds a 64-bit unsigned integer.
Definition UInt64.h:47
virtual bool set_value(dods_uint64 val)
Definition UInt64.cc:115
int check_float64(const char *val)
int check_uint64(const char *val)
int check_int64(const char *val)
top level DAP object to house generic methods
Definition AISConnect.cc:30
string long_to_string(long val, int base)
Definition util.cc:946
string remove_quotes(const string &s)
Definition util.cc:562
D4RValue * D4RValueFactory(std::string cpps)
Build an appropriate RValue.
Definition D4RValue.cc:197