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
61 /** The Int8 constructor accepts the name of the variable to be created.
62
63 @note This type is available in DAP4 only.
64 See http://docs.opendap.org/index.php/DAP4:_Specification_Volume_1#Atomic_Types
65
66 @param n A string containing the name of the variable to be created.
67 */
Int8(const string & n)68 Int8::Int8(const string &n) : BaseType(n, dods_int8_c, true /*is_dap4*/), d_buf(0)
69 {}
70
71 /** The Int8 server-side constructor accepts the name of the variable and
72 the dataset name from which this instance is created. This is a signed
73 8-bit integer and was added for DAP4; Byte and UInt8 are unsigned 8-bit
74 integers.
75
76 @note This integer type cannot be used with DAP2; it is only present in DAP4.
77 See http://docs.opendap.org/index.php/DAP4:_Specification_Volume_1#Atomic_Types.
78
79 @param n A string containing the name of the variable to be created.
80 @param d A string containing the name of the dataset from which this
81 variable is created
82 */
Int8(const string & n,const string & d)83 Int8::Int8(const string &n, const string &d) : BaseType(n, d, dods_int8_c, true /*is_dap4*/), d_buf(0)
84 {}
85
Int8(const Int8 & copy_from)86 Int8::Int8(const Int8 ©_from) : BaseType(copy_from)
87 {
88 d_buf = copy_from.d_buf;
89 }
90
91 BaseType *
ptr_duplicate()92 Int8::ptr_duplicate()
93 {
94 return new Int8(*this);
95 }
96
97 Int8 &
operator =(const Int8 & rhs)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
width(bool) const111 Int8::width(bool) const
112 {
113 return sizeof(dods_int8);
114 }
115
116 void
compute_checksum(Crc32 & checksum)117 Int8::compute_checksum(Crc32 &checksum)
118 {
119 checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
120 }
121
122 /**
123 * @brief Serialize an Int8
124 * @param m
125 * @param dmr Unused
126 * @param eval Unused
127 * @param filter Unused
128 * @exception Error is thrown if the value needs to be read and that operation fails.
129 */
130 void
serialize(D4StreamMarshaller & m,DMR &,bool)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
deserialize(D4StreamUnMarshaller & um,DMR &)140 Int8::deserialize(D4StreamUnMarshaller &um, DMR &)
141 {
142 um.get_int8( d_buf ) ;
143 }
144
145 dods_int8
value() const146 Int8::value() const
147 {
148 return d_buf;
149 }
150
151 bool
set_value(dods_int8 i)152 Int8::set_value(dods_int8 i)
153 {
154 d_buf = i;
155 set_read_p(true);
156
157 return true;
158 }
159
print_val(ostream & out,string space,bool print_decl_p)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
ops(BaseType * b,int op)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
184 /**
185 * @see BaseType::d4_ops(BaseType *, int)
186 */
d4_ops(BaseType * b,int op)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
218 /** @brief DAP4 to DAP2 transform
219 *
220 * Return a DAP2 'copy' of the variable. In this
221 * case, since Int8 doesn't have a natural representation
222 * in DAP2 we are going to just call it a Byte .
223 * Why? Because SIZE.
224 *
225 * @param root The root group that should hold this new variable. Add Group-level
226 * stuff here (e.g., D4Dimensions).
227 * @param container Add the new variable to this container.
228 *
229 * @return A pointer to the transformed variable
230 */
231 std::vector<BaseType *> *
transform_to_dap2(AttrTable * parent_attr_table)232 Int8::transform_to_dap2(AttrTable *parent_attr_table)
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 }
244 /**
245 * @brief dumps information about this object
246 *
247 * Displays the pointer value of this instance and information about this
248 * instance.
249 *
250 * @param strm C++ i/o stream to dump the information to
251 * @return void
252 */
253 void
dump(ostream & strm) const254 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
266