libdap  Updated for version 3.17.2
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 
55 using std::cerr;
56 using std::endl;
57 
58 namespace libdap {
59 
67 Int8::Int8(const string &n) : BaseType(n, dods_int8_c, true /*is_dap4*/), d_buf(0)
68 {}
69 
82 Int8::Int8(const string &n, const string &d) : BaseType(n, d, dods_int8_c, true /*is_dap4*/), d_buf(0)
83 {}
84 
85 Int8::Int8(const Int8 &copy_from) : BaseType(copy_from)
86 {
87  d_buf = copy_from.d_buf;
88 }
89 
90 BaseType *
92 {
93  return new Int8(*this);
94 }
95 
96 Int8 &
97 Int8::operator=(const Int8 &rhs)
98 {
99  if (this == &rhs)
100  return *this;
101 
102  static_cast<BaseType &>(*this) = rhs;
103 
104  d_buf = rhs.d_buf;
105 
106  return *this;
107 }
108 
109 unsigned int
110 Int8::width(bool) const
111 {
112  return sizeof(dods_int8);
113 }
114 
115 void
117 {
118  checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
119 }
120 
129 void
130 Int8::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
131 {
132  if (!read_p())
133  read(); // read() throws Error
134 
135  m.put_int8( d_buf ) ;
136 }
137 
138 void
140 {
141  um.get_int8( d_buf ) ;
142 }
143 
144 dods_int8
145 Int8::value() const
146 {
147  return d_buf;
148 }
149 
150 bool
151 Int8::set_value(dods_int8 i)
152 {
153  d_buf = i;
154  set_read_p(true);
155 
156  return true;
157 }
158 
159 void Int8::print_val(ostream &out, string space, bool print_decl_p)
160 {
161  if (print_decl_p) {
162  print_decl(out, space, false);
163  out << " = " << (int)d_buf << ";\n";
164  }
165  else
166  out << (int)d_buf;
167 }
168 
169 bool
170 Int8::ops(BaseType *b, int op)
171 {
172  // Get the arg's value.
173  if (!read_p() && !read())
174  throw InternalErr(__FILE__, __LINE__, "This value not read!");
175 
176  // Get the second arg's value.
177  if (!b->read_p() && !b->read())
178  throw InternalErr(__FILE__, __LINE__, "This value not read!");
179 
180  switch (b->type()) {
181  case dods_int8_c:
182  return Cmp<dods_int8, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
183  case dods_byte_c:
184  return SUCmp<dods_int8, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
185  case dods_int16_c:
186  return Cmp<dods_int8, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
187  case dods_uint16_c:
188  return SUCmp<dods_int8, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
189  case dods_int32_c:
190  return Cmp<dods_int8, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
191  case dods_uint32_c:
192  return SUCmp<dods_int8, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
193  case dods_int64_c:
194  return Cmp<dods_int8, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
195  case dods_uint64_c:
196  return SUCmp<dods_int8, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
197  case dods_float32_c:
198  return Cmp<dods_int8, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
199  case dods_float64_c:
200  return Cmp<dods_int8, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
201  default:
202  return false;
203  }
204 #if 0
205  switch (b->type()) {
206  case dods_byte_c:
207  return rops<dods_int8, dods_byte, SUCmp<dods_int8, dods_byte> >(d_buf, static_cast<Byte*>(b)->value());
208  case dods_int16_c:
209  return rops<dods_int8, dods_int16, Cmp<dods_int8, dods_int16> >(d_buf, static_cast<Int16 *>(b)->value());
210  case dods_uint16_c:
211  return rops<dods_int8, dods_uint16, SUCmp<dods_int8, dods_uint16> >(d_buf, static_cast<UInt16 *>(b)->value());
212  case dods_int32_c:
213  return rops<dods_int8, dods_int32, Cmp<dods_int8, dods_int32> >(d_buf, static_cast<Int32 *>(b)->value());
214  case dods_uint32_c:
215  return rops<dods_int8, dods_uint32, SUCmp<dods_int8, dods_uint32> >(d_buf, static_cast<UInt32 *>(b)->value());
216  case dods_int64_c:
217  return rops<dods_int8, dods_int64, Cmp<dods_int8, dods_int64> >(d_buf, static_cast<Int64 *>(b)->value());
218  case dods_uint64_c:
219  return rops<dods_int8, dods_uint64, Cmp<dods_int8, dods_uint64> >(d_buf, static_cast<UInt64 *>(b)->value());
220  case dods_float32_c:
221  return rops<dods_int8, dods_float32, Cmp<dods_int64, dods_float32> >(d_buf, static_cast<Float32 *>(b)->value());
222  case dods_float64_c:
223  return rops<dods_int8, dods_float64, Cmp<dods_int64, dods_float64> >(d_buf, static_cast<Float64 *>(b)->value());
224  default:
225  return false;
226  }
227 #endif
228 }
229 
238 void
239 Int8::dump(ostream &strm) const
240 {
241  strm << DapIndent::LMarg << "Int8::dump - ("
242  << (void *)this << ")" << endl ;
243  DapIndent::Indent() ;
244  BaseType::dump(strm) ;
245  strm << DapIndent::LMarg << "value: " << d_buf << endl ;
246  DapIndent::UnIndent() ;
247 }
248 
249 } // namespace libdap
250 
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Int8.cc:170
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:820
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:425
Int8(const string &n)
Definition: Int8.cc:67
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:924
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:125
virtual unsigned int width(bool constrained=false) const
How many bytes does this use Return the number of bytes of storage this variable uses. For scalar types, this is pretty simple (an int32 uses 4 bytes, etc.). For arrays and Constructors, it is a bit more complex. Note that a scalar String variable uses sizeof(String*) bytes, not the length of the string. In other words, the value returned is independent of the type. Also note width() of a String array returns the number of elements in the array times sizeof(String*). That is, each different array size is a different data type.
Definition: Int8.cc:110
virtual Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:310
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:116
Holds a 32-bit floating point value.
Definition: Float32.h:61
virtual BaseType * ptr_duplicate()
Definition: Int8.cc:91
A class for software fault reporting.
Definition: InternalErr.h:64
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 void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:236
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:461
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 deserialize(D4StreamUnMarshaller &um, DMR &dmr)
Definition: Int8.cc:139
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
Holds a 32-bit unsigned integer.
Definition: UInt32.h:59
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Int8.cc:239
virtual void serialize(D4StreamMarshaller &m, DMR &dmr, bool filter=false)
Serialize an Int8.
Definition: Int8.cc:130
Holds a 32-bit signed integer.
Definition: Int32.h:65