libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
Int8.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) 2012 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 <cassert>
29#include <sstream>
30
31#include "Byte.h" // synonymous with UInt8 and Char
32#include "Float32.h"
33#include "Float64.h"
34#include "Int16.h"
35#include "Int32.h"
36#include "Int64.h"
37#include "Int8.h"
38#include "Str.h"
39#include "UInt16.h"
40#include "UInt32.h"
41#include "UInt64.h"
42#include "Url.h"
43
44#include "D4StreamMarshaller.h"
46
47#include "DDS.h"
48#include "DapIndent.h"
49#include "InternalErr.h"
50#include "Operators.h"
51#include "debug.h"
52#include "dods-limits.h"
53#include "parser.h"
54#include "util.h"
55
56using std::cerr;
57using std::endl;
58
59namespace libdap {
60
68Int8::Int8(const string &n) : BaseType(n, dods_int8_c, true /*is_dap4*/), d_buf(0) {}
69
82Int8::Int8(const string &n, const string &d) : BaseType(n, d, dods_int8_c, true /*is_dap4*/), d_buf(0) {}
83
84Int8::Int8(const Int8 &copy_from) : BaseType(copy_from) { d_buf = copy_from.d_buf; }
85
86BaseType *Int8::ptr_duplicate() { return new Int8(*this); }
87
89 if (this == &rhs)
90 return *this;
92 d_buf = rhs.d_buf;
93 return *this;
94}
95
96void Int8::compute_checksum(Crc32 &checksum) { checksum.AddData(reinterpret_cast<uint8_t *>(&d_buf), sizeof(d_buf)); }
97
106void Int8::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool) {
107 if (!read_p())
108 read(); // read() throws Error
109
110 m.put_int8(d_buf);
111}
112
114
115dods_int8 Int8::value() const { return d_buf; }
116
118 d_buf = i;
119 set_read_p(true);
120
121 return true;
122}
123
124void Int8::print_val(ostream &out, string space, bool print_decl_p) {
125 if (print_decl_p) {
126 print_decl(out, space, false);
127 out << " = " << (int)d_buf << ";\n";
128 } else
129 out << (int)d_buf;
130}
131
132bool Int8::ops(BaseType *b, int op) {
133 // Get the arg's value.
134 if (!read_p() && !read())
135 throw InternalErr(__FILE__, __LINE__, "This value not read!");
136
137 // Get the second arg's value.
138 if (!b || !(b->read_p() || b->read()))
139 throw InternalErr(__FILE__, __LINE__, "This value not read!");
140
141 return d4_ops(b, op);
142}
143
147bool Int8::d4_ops(BaseType *b, int op) {
148 switch (b->type()) {
149 case dods_int8_c:
150 return Cmp<dods_int8, dods_int8>(op, d_buf, static_cast<Int8 *>(b)->value());
151 case dods_byte_c:
152 return Cmp<dods_int8, dods_byte>(op, d_buf, static_cast<Byte *>(b)->value());
153 case dods_int16_c:
154 return Cmp<dods_int8, dods_int16>(op, d_buf, static_cast<Int16 *>(b)->value());
155 case dods_uint16_c:
156 return Cmp<dods_int8, dods_uint16>(op, d_buf, static_cast<UInt16 *>(b)->value());
157 case dods_int32_c:
158 return Cmp<dods_int8, dods_int32>(op, d_buf, static_cast<Int32 *>(b)->value());
159 case dods_uint32_c:
160 return Cmp<dods_int8, dods_uint32>(op, d_buf, static_cast<UInt32 *>(b)->value());
161 case dods_int64_c:
162 return Cmp<dods_int8, dods_int64>(op, d_buf, static_cast<Int64 *>(b)->value());
163 case dods_uint64_c:
164 return Cmp<dods_int8, dods_uint64>(op, d_buf, static_cast<UInt64 *>(b)->value());
165 case dods_float32_c:
166 return Cmp<dods_int8, dods_float32>(op, d_buf, static_cast<Float32 *>(b)->value());
167 case dods_float64_c:
168 return Cmp<dods_int8, dods_float64>(op, d_buf, static_cast<Float64 *>(b)->value());
169 case dods_str_c:
170 case dods_url_c:
171 throw Error(malformed_expr, "Relational operators can only compare compatible types (number, string).");
172 default:
173 throw Error(malformed_expr, "Relational operators only work with scalar types.");
174 }
175}
176
190std::vector<BaseType *> *Int8::transform_to_dap2(AttrTable *parent_attr_table) {
191 vector<BaseType *> *vec = BaseType::transform_to_dap2(parent_attr_table);
192 if (vec->size() != 1) {
193 ostringstream oss;
194 oss << __func__ << "() - Something Bad Happened. This transform should produce only ";
195 oss << " a single BaseType yet it produced " << vec->size();
196 throw Error(internal_error, oss.str());
197 }
198 (*vec)[0]->set_type(dods_byte_c);
199 return vec;
200}
201
207bool Int8::is_dap4_projected(std::vector<std::string> &inventory) {
208 if (send_p()) {
209 attributes()->has_dap4_types(FQN(), inventory);
210 inventory.emplace_back(type_name() + " " + FQN());
211 return true;
212 }
213 return false;
214}
215
225void Int8::dump(ostream &strm) const {
226 strm << DapIndent::LMarg << "Int8::dump - (" << (void *)this << ")" << endl;
228 BaseType::dump(strm);
229 strm << DapIndent::LMarg << "value: " << d_buf << endl;
231}
232
233} // namespace libdap
#define internal_error
Internal server error (500)
Definition Error.h:63
#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
Contains the attributes for a dataset.
Definition AttrTable.h:150
BaseType & operator=(const BaseType &rhs)
Definition BaseType.cc:159
virtual string type_name() const
Returns the type of the class instance as a string.
Definition BaseType.cc:335
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
void dump(ostream &strm) const override
dumps information about this object
Definition BaseType.cc:269
virtual D4Attributes * attributes()
Definition BaseType.cc:507
virtual std::string FQN() const
Definition BaseType.cc:304
virtual bool send_p()
Should this variable be sent?
Definition BaseType.cc:478
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
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition BaseType.cc:240
Holds a single byte.
Definition Byte.h:59
bool has_dap4_types(const std::string &path, std::vector< std::string > &inventory) const
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
virtual void put_int8(dods_int8 val)
Read data from the stream made by D4StreamMarshaller.
virtual void get_int8(dods_int8 &val)
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
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_int8 val)
Definition Int8.cc:117
void dump(ostream &strm) const override
dumps information about this object
Definition Int8.cc:225
dods_int8 d_buf
Definition Int8.h:55
void serialize(D4StreamMarshaller &m, DMR &dmr, bool filter=false) override
Serialize an Int8.
Definition Int8.cc:106
void deserialize(D4StreamUnMarshaller &um, DMR &dmr) override
Definition Int8.cc:113
BaseType * ptr_duplicate() override
Definition Int8.cc:86
bool ops(BaseType *b, int op) override
Evaluate relational operators.
Definition Int8.cc:132
Int8 & operator=(const Int8 &rhs)
Definition Int8.cc:88
Int8(const string &n)
Definition Int8.cc:68
void compute_checksum(Crc32 &checksum) override
include the data for this variable in the checksum DAP4 includes a checksum with every data response....
Definition Int8.cc:96
bool is_dap4_projected(std::vector< std::string > &inventory) override
Definition Int8.cc:207
bool d4_ops(BaseType *b, int op) override
Definition Int8.cc:147
virtual dods_int8 value() const
Definition Int8.cc:115
std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table) override
DAP4 to DAP2 transform.
Definition Int8.cc:190
A class for software fault reporting.
Definition InternalErr.h:61
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
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