libdap  Updated for version 3.20.6
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 
56 using std::cerr;
57 using std::endl;
58 
59 namespace libdap {
60 
68 Int8::Int8(const string &n) : BaseType(n, dods_int8_c, true /*is_dap4*/), d_buf(0)
69 {}
70 
83 Int8::Int8(const string &n, const string &d) : BaseType(n, d, dods_int8_c, true /*is_dap4*/), d_buf(0)
84 {}
85 
86 Int8::Int8(const Int8 &copy_from) : BaseType(copy_from)
87 {
88  d_buf = copy_from.d_buf;
89 }
90 
91 BaseType *
93 {
94  return new Int8(*this);
95 }
96 
97 Int8 &
98 Int8::operator=(const Int8 &rhs)
99 {
100  if (this == &rhs)
101  return *this;
102 
103  static_cast<BaseType &>(*this) = rhs;
104 
105  d_buf = rhs.d_buf;
106 
107  return *this;
108 }
109 
110 unsigned int
111 Int8::width(bool) const
112 {
113  return sizeof(dods_int8);
114 }
115 
116 void
118 {
119  checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
120 }
121 
130 void
131 Int8::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
132 {
133  if (!read_p())
134  read(); // read() throws Error
135 
136  m.put_int8( d_buf ) ;
137 }
138 
139 void
141 {
142  um.get_int8( d_buf ) ;
143 }
144 
145 dods_int8
146 Int8::value() const
147 {
148  return d_buf;
149 }
150 
151 bool
152 Int8::set_value(dods_int8 i)
153 {
154  d_buf = i;
155  set_read_p(true);
156 
157  return true;
158 }
159 
160 void Int8::print_val(ostream &out, string space, bool print_decl_p)
161 {
162  if (print_decl_p) {
163  print_decl(out, space, false);
164  out << " = " << (int)d_buf << ";\n";
165  }
166  else
167  out << (int)d_buf;
168 }
169 
170 bool
171 Int8::ops(BaseType *b, int op)
172 {
173  // Get the arg's value.
174  if (!read_p() && !read())
175  throw InternalErr(__FILE__, __LINE__, "This value not read!");
176 
177  // Get the second arg's value.
178  if (!b || !(b->read_p() || b->read()))
179  throw InternalErr(__FILE__, __LINE__, "This value not read!");
180 
181  return d4_ops(b, op);
182 }
183 
187 bool Int8::d4_ops(BaseType *b, int op)
188 {
189  switch (b->type()) {
190  case dods_int8_c:
191  return Cmp<dods_int8, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
192  case dods_byte_c:
193  return Cmp<dods_int8, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
194  case dods_int16_c:
195  return Cmp<dods_int8, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
196  case dods_uint16_c:
197  return Cmp<dods_int8, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
198  case dods_int32_c:
199  return Cmp<dods_int8, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
200  case dods_uint32_c:
201  return Cmp<dods_int8, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
202  case dods_int64_c:
203  return Cmp<dods_int8, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
204  case dods_uint64_c:
205  return Cmp<dods_int8, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
206  case dods_float32_c:
207  return Cmp<dods_int8, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
208  case dods_float64_c:
209  return Cmp<dods_int8, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
210  case dods_str_c:
211  case dods_url_c:
212  throw Error(malformed_expr, "Relational operators can only compare compatible types (number, string).");
213  default:
214  throw Error(malformed_expr, "Relational operators only work with scalar types.");
215  }
216 }
217 
231 std::vector<BaseType *> *
233 {
234  vector<BaseType *> *vec = BaseType::transform_to_dap2(parent_attr_table);
235  if(vec->size()!=1){
236  ostringstream oss;
237  oss << __func__ << "() - Something Bad Happened. This transform should produce only ";
238  oss << " a single BaseType yet it produced " << vec->size();
239  throw Error(internal_error,oss.str());
240  }
241  (*vec)[0]->set_type(dods_byte_c);
242  return vec;
243 }
253 void
254 Int8::dump(ostream &strm) const
255 {
256  strm << DapIndent::LMarg << "Int8::dump - ("
257  << (void *)this << ")" << endl ;
258  DapIndent::Indent() ;
259  BaseType::dump(strm) ;
260  strm << DapIndent::LMarg << "value: " << d_buf << endl ;
261  DapIndent::UnIndent() ;
262 }
263 
264 } // namespace libdap
265 
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Int8.cc:171
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:899
Holds an 8-bit signed integer value.
Definition: Int8.h:42
Holds a64-bit signed integer.
Definition: Int64.h:49
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:480
Int8(const string &n)
Definition: Int8.cc:68
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:1003
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: Int8.cc:232
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:291
Contains the attributes for a dataset.
Definition: AttrTable.h:142
Read data from the stream made by D4StreamMarshaller.
Holds an unsigned 16-bit integer.
Definition: UInt16.h:57
Definition: crc.h:76
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
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:117
Holds a 32-bit floating point value.
Definition: Float32.h:61
top level DAP object to house generic methods
Definition: AISConnect.cc:30
virtual BaseType * ptr_duplicate()
Definition: Int8.cc:92
A class for software fault reporting.
Definition: InternalErr.h:64
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:111
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4&#39;s receiv...
Holds a 16-bit signed integer value.
Definition: Int16.h:59
virtual Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:365
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:516
Holds a 64-bit unsigned integer.
Definition: UInt64.h:49
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Int8.cc:254
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: BaseType.cc:259
virtual void deserialize(D4StreamUnMarshaller &um, DMR &dmr)
Definition: Int8.cc:140
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
Holds a 64-bit (double precision) floating point value.
Definition: Float64.h:60
Holds a single byte.
Definition: Byte.h:60
virtual bool d4_ops(BaseType *b, int op)
Definition: Int8.cc:187
A class for error processing.
Definition: Error.h:92
Holds a 32-bit unsigned integer.
Definition: UInt32.h:59
virtual void serialize(D4StreamMarshaller &m, DMR &dmr, bool filter=false)
Serialize an Int8.
Definition: Int8.cc:131
Holds a 32-bit signed integer.
Definition: Int32.h:65