libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
Int16.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) 2002,2003 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// (c) COPYRIGHT URI/MIT 1996-1999
27// Please read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// Implementation for Int32.
33//
34// jhrg 9/7/94
35
36#include "config.h"
37
38#include <sstream>
39
40#include "Byte.h" // synonymous with UInt8 and Char
41#include "Float32.h"
42#include "Float64.h"
43#include "Int16.h"
44#include "Int32.h"
45#include "Int64.h"
46#include "Int8.h"
47#include "Str.h"
48#include "UInt16.h"
49#include "UInt32.h"
50#include "UInt64.h"
51#include "Url.h"
52
53#include "DDS.h"
54#include "Marshaller.h"
55#include "UnMarshaller.h"
56
57#include "D4StreamMarshaller.h"
59#include "DMR.h"
60
61#include "DapIndent.h"
62#include "InternalErr.h"
63#include "Operators.h"
64#include "debug.h"
65#include "dods-limits.h"
66#include "parser.h"
67#include "util.h"
68
69using std::cerr;
70using std::endl;
71
72namespace libdap {
73
78Int16::Int16(const string &n) : BaseType(n, dods_int16_c), d_buf(0) {}
79
87Int16::Int16(const string &n, const string &d) : BaseType(n, d, dods_int16_c), d_buf(0) {}
88
89Int16::Int16(const Int16 &copy_from) : BaseType(copy_from) { d_buf = copy_from.d_buf; }
90
91BaseType *Int16::ptr_duplicate() { return new Int16(*this); }
92
94 if (this == &rhs)
95 return *this;
97 d_buf = rhs.d_buf;
98 return *this;
99}
100
101bool Int16::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval) {
102#if USE_LOCAL_TIMEOUT_SCHEME
103 dds.timeout_on();
104#endif
105 if (!read_p())
106 read(); // read() throws Error and InternalErr
107
108 if (ce_eval && !eval.eval_selection(dds, dataset()))
109 return true;
110#if USE_LOCAL_TIMEOUT_SCHEME
111 dds.timeout_off();
112#endif
113 m.put_int16(d_buf);
114
115 return true;
116}
117
119 um.get_int16(d_buf);
120
121 return false;
122}
123
124void Int16::compute_checksum(Crc32 &checksum) { checksum.AddData(reinterpret_cast<uint8_t *>(&d_buf), sizeof(d_buf)); }
125
134void Int16::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool) {
135 if (!read_p())
136 read(); // read() throws Error
137
138 m.put_int16(d_buf);
139}
140
142
143unsigned int Int16::val2buf(void *val, bool) {
144 // Jose Garcia
145 // This method is public therefore and I believe it has being designed
146 // to be use by read which must be implemented on the surrogated library,
147 // thus if the pointer val is NULL, is an Internal Error.
148 if (!val)
149 throw InternalErr(__FILE__, __LINE__, "The incoming pointer does not contain any data.");
150
151 d_buf = *(dods_int16 *)val;
152
153 return width();
154}
155
156unsigned int Int16::buf2val(void **val) {
157 // Jose Garcia
158 // The same comment justifying throwing an Error in val2buf applies here.
159 if (!val)
160 throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
161
162 if (!*val)
163 *val = new dods_int16;
164
165 *(dods_int16 *)*val = d_buf;
166
167 return width();
168}
169
170dods_int16 Int16::value() const { return d_buf; }
171
173 d_buf = i;
174 set_read_p(true);
175
176 return true;
177}
178
179void Int16::print_val(FILE *out, string space, bool print_decl_p) {
180 ostringstream oss;
181 print_val(oss, space, print_decl_p);
182 fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
183}
184
185void Int16::print_val(ostream &out, string space, bool print_decl_p) {
186 if (print_decl_p) {
187 print_decl(out, space, false);
188 out << " = " << d_buf << ";\n";
189 } else
190 out << d_buf;
191}
192
193bool Int16::ops(BaseType *b, int op) {
194
195 // Extract the Byte arg's value.
196 if (!read_p() && !read()) {
197 // Jose Garcia
198 // Since the read method is virtual and implemented outside
199 // libdap++ if we cannot read the data that is the problem
200 // of the user or of whoever wrote the surrogate library
201 // implemeting read therefore it is an internal error.
202 throw InternalErr(__FILE__, __LINE__, "This value not read!");
203 }
204
205 // Extract the second arg's value.
206 if (!b || !(b->read_p() || b->read())) {
207 // Jose Garcia
208 // Since the read method is virtual and implemented outside
209 // libdap++ if we cannot read the data that is the problem
210 // of the user or of whoever wrote the surrogate library
211 // implemeting read therefore it is an internal error.
212 throw InternalErr(__FILE__, __LINE__, "This value not read!");
213 }
214
215 return d4_ops(b, op);
216}
217
221bool Int16::d4_ops(BaseType *b, int op) {
222 switch (b->type()) {
223 case dods_int8_c:
224 return Cmp<dods_int16, dods_int8>(op, d_buf, static_cast<Int8 *>(b)->value());
225 case dods_byte_c:
226 return Cmp<dods_int16, dods_byte>(op, d_buf, static_cast<Byte *>(b)->value());
227 case dods_int16_c:
228 return Cmp<dods_int16, dods_int16>(op, d_buf, static_cast<Int16 *>(b)->value());
229 case dods_uint16_c:
230 return Cmp<dods_int16, dods_uint16>(op, d_buf, static_cast<UInt16 *>(b)->value());
231 case dods_int32_c:
232 return Cmp<dods_int16, dods_int32>(op, d_buf, static_cast<Int32 *>(b)->value());
233 case dods_uint32_c:
234 return Cmp<dods_int16, dods_uint32>(op, d_buf, static_cast<UInt32 *>(b)->value());
235 case dods_int64_c:
236 return Cmp<dods_int16, dods_int64>(op, d_buf, static_cast<Int64 *>(b)->value());
237 case dods_uint64_c:
238 return Cmp<dods_int16, dods_uint64>(op, d_buf, static_cast<UInt64 *>(b)->value());
239 case dods_float32_c:
240 return Cmp<dods_int16, dods_float32>(op, d_buf, static_cast<Float32 *>(b)->value());
241 case dods_float64_c:
242 return Cmp<dods_int16, dods_float64>(op, d_buf, static_cast<Float64 *>(b)->value());
243 case dods_str_c:
244 case dods_url_c:
245 throw Error(malformed_expr, "Relational operators can only compare compatible types (number, string).");
246 default:
247 throw Error(malformed_expr, "Relational operators only work with scalar types.");
248 }
249}
250
259void Int16::dump(ostream &strm) const {
260 strm << DapIndent::LMarg << "Int16::dump - (" << (void *)this << ")" << endl;
262 BaseType::dump(strm);
263 strm << DapIndent::LMarg << "value: " << d_buf << endl;
265}
266
267} // namespace libdap
#define malformed_expr
(400)
Definition Error.h:66
Definition crc.h:43
void AddData(const uint8_t *pData, const uint32_t length)
Definition crc.h:64
BaseType & operator=(const BaseType &rhs)
Definition BaseType.cc:159
virtual bool read()
Read data into a local buffer.
Definition BaseType.cc:775
virtual void print_decl(FILE *out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
Definition BaseType.cc:868
virtual bool read_p()
Has this variable been read?
Definition BaseType.cc:410
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition BaseType.cc:442
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition BaseType.cc:326
void dump(ostream &strm) const override
dumps information about this object
Definition BaseType.cc:269
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition BaseType.cc:124
virtual Type type() const
Returns the type of the class instance.
Definition BaseType.cc:329
Holds a single byte.
Definition Byte.h:59
Evaluate a constraint expression.
bool eval_selection(DDS &dds, const std::string &dataset)
Evaluate a boolean-valued constraint expression. This is main method for the evaluator and is called ...
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
virtual void put_int16(dods_int16 val)
Read data from the stream made by D4StreamMarshaller.
virtual void get_int16(dods_int16 &val)
void timeout_off()
Definition DDS.cc:695
void timeout_on()
Definition DDS.cc:687
static ostream & LMarg(ostream &strm)
Definition DapIndent.cc:61
static void Indent()
Definition DapIndent.cc:44
static void UnIndent()
Definition DapIndent.cc:46
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
void print_val(FILE *out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
Definition Int16.cc:179
unsigned int width(bool=false) const override
How many bytes does this variable use Return the number of bytes of storage this variable uses....
Definition Int16.h:72
unsigned int buf2val(void **val) override
Reads the class data.
Definition Int16.cc:156
void dump(ostream &strm) const override
dumps information about this object
Definition Int16.cc:259
virtual dods_int16 value() const
Definition Int16.cc:170
Int16 & operator=(const Int16 &rhs)
Definition Int16.cc:93
BaseType * ptr_duplicate() override
Definition Int16.cc:91
dods_int16 d_buf
Definition Int16.h:59
Int16(const string &n)
Definition Int16.cc:78
bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false) override
Receive data from the net.
Definition Int16.cc:118
bool d4_ops(BaseType *b, int op) override
Definition Int16.cc:221
bool ops(BaseType *b, int op) override
Evaluate relational operators.
Definition Int16.cc:193
unsigned int val2buf(void *val, bool reuse=false) override
Loads class data.
Definition Int16.cc:143
virtual bool set_value(dods_int16 val)
Definition Int16.cc:172
void compute_checksum(Crc32 &checksum) override
include the data for this variable in the checksum DAP4 includes a checksum with every data response....
Definition Int16.cc:124
bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true) override
Move data to the net, then remove them from the object.
Definition Int16.cc:101
Holds a 32-bit signed integer.
Definition Int32.h:63
Holds a64-bit signed integer.
Definition Int64.h:48
Holds an 8-bit signed integer value.
Definition Int8.h:41
A class for software fault reporting.
Definition InternalErr.h:61
abstract base class used to marshal/serialize dap data objects
Definition Marshaller.h:50
virtual void put_int16(dods_int16 val)=0
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
abstract base class used to unmarshall/deserialize dap data objects
virtual void get_int16(dods_int16 &val)=0
top level DAP object to house generic methods
Definition AISConnect.cc:30
@ dods_uint32_c
Definition Type.h:100
@ dods_int16_c
Definition Type.h:97
@ dods_byte_c
Definition Type.h:96
@ dods_int32_c
Definition Type.h:99
@ dods_url_c
Definition Type.h:104
@ dods_int8_c
Definition Type.h:115
@ dods_float32_c
Definition Type.h:101
@ dods_int64_c
Definition Type.h:118
@ dods_uint64_c
Definition Type.h:119
@ dods_uint16_c
Definition Type.h:98
@ dods_float64_c
Definition Type.h:102
@ dods_str_c
Definition Type.h:103
bool Cmp(int op, T1 v1, T2 v2)
Definition Operators.h:52