libdap Updated for version 3.20.11
libdap4 is an implementation of OPeNDAP's DAP protocol.
Int8.cc
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 "Int8.h"
33#include "Int16.h"
34#include "UInt16.h"
35#include "Int32.h"
36#include "UInt32.h"
37#include "Int64.h"
38#include "UInt64.h"
39#include "Float32.h"
40#include "Float64.h"
41#include "Str.h"
42#include "Url.h"
43
44#include "D4StreamMarshaller.h"
45#include "D4StreamUnMarshaller.h"
46
47#include "DDS.h"
48#include "util.h"
49#include "parser.h"
50#include "Operators.h"
51#include "dods-limits.h"
52#include "debug.h"
53#include "InternalErr.h"
54#include "DapIndent.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{}
70
83Int8::Int8(const string &n, const string &d) : BaseType(n, d, dods_int8_c, true /*is_dap4*/), d_buf(0)
84{}
85
86Int8::Int8(const Int8 &copy_from) : BaseType(copy_from)
87{
88 d_buf = copy_from.d_buf;
89}
90
91BaseType *
93{
94 return new Int8(*this);
95}
96
97Int8 &
98Int8::operator=(const Int8 &rhs)
99{
100 if (this == &rhs)
101 return *this;
102 BaseType::operator=(rhs);
103 d_buf = rhs.d_buf;
104 return *this;
105}
106
107unsigned int
108Int8::width(bool) const
109{
110 return sizeof(dods_int8);
111}
112
113void
115{
116 checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
117}
118
127void
128Int8::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
129{
130 if (!read_p())
131 read(); // read() throws Error
132
133 m.put_int8( d_buf ) ;
134}
135
136void
138{
139 um.get_int8( d_buf ) ;
140}
141
142dods_int8
143Int8::value() const
144{
145 return d_buf;
146}
147
148bool
149Int8::set_value(dods_int8 i)
150{
151 d_buf = i;
152 set_read_p(true);
153
154 return true;
155}
156
157void Int8::print_val(ostream &out, string space, bool print_decl_p)
158{
159 if (print_decl_p) {
160 print_decl(out, space, false);
161 out << " = " << (int)d_buf << ";\n";
162 }
163 else
164 out << (int)d_buf;
165}
166
167bool
169{
170 // Get the arg's value.
171 if (!read_p() && !read())
172 throw InternalErr(__FILE__, __LINE__, "This value not read!");
173
174 // Get the second arg's value.
175 if (!b || !(b->read_p() || b->read()))
176 throw InternalErr(__FILE__, __LINE__, "This value not read!");
177
178 return d4_ops(b, op);
179}
180
184bool Int8::d4_ops(BaseType *b, int op)
185{
186 switch (b->type()) {
187 case dods_int8_c:
188 return Cmp<dods_int8, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
189 case dods_byte_c:
190 return Cmp<dods_int8, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
191 case dods_int16_c:
192 return Cmp<dods_int8, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
193 case dods_uint16_c:
194 return Cmp<dods_int8, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
195 case dods_int32_c:
196 return Cmp<dods_int8, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
197 case dods_uint32_c:
198 return Cmp<dods_int8, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
199 case dods_int64_c:
200 return Cmp<dods_int8, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
201 case dods_uint64_c:
202 return Cmp<dods_int8, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
203 case dods_float32_c:
204 return Cmp<dods_int8, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
205 case dods_float64_c:
206 return Cmp<dods_int8, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
207 case dods_str_c:
208 case dods_url_c:
209 throw Error(malformed_expr, "Relational operators can only compare compatible types (number, string).");
210 default:
211 throw Error(malformed_expr, "Relational operators only work with scalar types.");
212 }
213}
214
228std::vector<BaseType *> *
230{
231 vector<BaseType *> *vec = BaseType::transform_to_dap2(parent_attr_table);
232 if(vec->size()!=1){
233 ostringstream oss;
234 oss << __func__ << "() - Something Bad Happened. This transform should produce only ";
235 oss << " a single BaseType yet it produced " << vec->size();
236 throw Error(internal_error,oss.str());
237 }
238 (*vec)[0]->set_type(dods_byte_c);
239 return vec;
240}
250void
251Int8::dump(ostream &strm) const
252{
253 strm << DapIndent::LMarg << "Int8::dump - ("
254 << (void *)this << ")" << endl ;
255 DapIndent::Indent() ;
256 BaseType::dump(strm) ;
257 strm << DapIndent::LMarg << "value: " << d_buf << endl ;
258 DapIndent::UnIndent() ;
259}
260
261} // namespace libdap
262
Definition: crc.h:77
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98
Contains the attributes for a dataset.
Definition: AttrTable.h:143
The basic data type for the DODS DAP types.
Definition: BaseType.h:118
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:895
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:999
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:476
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:512
void dump(ostream &strm) const override
dumps information about this object
Definition: BaseType.cc:287
virtual Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:361
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: BaseType.cc:255
Holds a single byte.
Definition: Byte.h:61
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
Read data from the stream made by D4StreamMarshaller.
A class for error processing.
Definition: Error.h:94
Holds a 32-bit floating point value.
Definition: Float32.h:62
Holds a 64-bit (double precision) floating point value.
Definition: Float64.h:61
Holds a 16-bit signed integer value.
Definition: Int16.h:60
Holds a 32-bit signed integer.
Definition: Int32.h:66
Holds a64-bit signed integer.
Definition: Int64.h:50
Holds an 8-bit signed integer value.
Definition: Int8.h:43
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Int8.cc:251
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Int8.cc:168
virtual unsigned int width(bool constrained=false) const
How many bytes does this variable use Return the number of bytes of storage this variable uses....
Definition: Int8.cc:108
virtual void compute_checksum(Crc32 &checksum)
include the data for this variable in the checksum DAP4 includes a checksum with every data response....
Definition: Int8.cc:114
virtual void deserialize(D4StreamUnMarshaller &um, DMR &dmr)
Definition: Int8.cc:137
virtual BaseType * ptr_duplicate()
Definition: Int8.cc:92
Int8(const string &n)
Definition: Int8.cc:68
virtual void serialize(D4StreamMarshaller &m, DMR &dmr, bool filter=false)
Serialize an Int8.
Definition: Int8.cc:128
virtual bool d4_ops(BaseType *b, int op)
Definition: Int8.cc:184
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: Int8.cc:229
A class for software fault reporting.
Definition: InternalErr.h:65
Holds an unsigned 16-bit integer.
Definition: UInt16.h:58
Holds a 32-bit unsigned integer.
Definition: UInt32.h:60
Holds a 64-bit unsigned integer.
Definition: UInt64.h:50
top level DAP object to house generic methods
Definition: AlarmHandler.h:36