libdap++  Updated for version 3.8.2
Int16.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) 2002,2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1996-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // Implementation for Int32.
33 //
34 // jhrg 9/7/94
35 
36 
37 #include "config.h"
38 
39 static char rcsid[] not_used =
40  {"$Id: Int16.cc 21699 2009-11-05 00:06:01Z jimg $"
41  };
42 
43 #include "Byte.h"
44 #include "Int16.h"
45 #include "UInt16.h"
46 #include "Int32.h"
47 #include "UInt32.h"
48 #include "Float32.h"
49 #include "Float64.h"
50 #include "Str.h"
51 #include "Url.h"
52 #include "Array.h"
53 #include "Structure.h"
54 #include "Sequence.h"
55 #include "Grid.h"
56 
57 #include "DDS.h"
58 #include "util.h"
59 #include "parser.h"
60 #include "Operators.h"
61 #include "dods-limits.h"
62 #include "debug.h"
63 #include "InternalErr.h"
64 
65 using std::cerr;
66 using std::endl;
67 
68 namespace libdap {
69 
74 Int16::Int16(const string &n) : BaseType(n, dods_int16_c)
75 {}
76 
84 Int16::Int16(const string &n, const string &d) : BaseType(n, d, dods_int16_c)
85 {}
86 
87 Int16::Int16(const Int16 &copy_from) : BaseType(copy_from)
88 {
89  _buf = copy_from._buf;
90 }
91 
92 BaseType *
94 {
95  return new Int16(*this);
96 }
97 
98 Int16 &
100 {
101  if (this == &rhs)
102  return *this;
103 
104  dynamic_cast<BaseType &>(*this) = rhs;
105 
106  _buf = rhs._buf;
107 
108  return *this;
109 }
110 
111 unsigned int
113 {
114  return sizeof(dods_int16);
115 }
116 
117 bool
119  Marshaller &m, bool ce_eval)
120 {
121  dds.timeout_on();
122 
123  if (!read_p())
124  read(); // read() throws Error and InternalErr
125 
126 #if EVAL
127  if (ce_eval && !eval.eval_selection(dds, dataset()))
128  return true;
129 #endif
130 
131  dds.timeout_off();
132 
133  m.put_int16( _buf ) ;
134 
135  return true;
136 }
137 
138 bool
140 {
141  um.get_int16( _buf ) ;
142 
143  return false;
144 }
145 
146 unsigned int
147 Int16::val2buf(void *val, bool)
148 {
149  // Jose Garcia
150  // This method is public therefore and I believe it has being designed
151  // to be use by read which must be implemented on the surrogated library,
152  // thus if the pointer val is NULL, is an Internal Error.
153  if (!val)
154  throw InternalErr(__FILE__, __LINE__,
155  "The incoming pointer does not contain any data.");
156 
157  _buf = *(dods_int16 *)val;
158 
159  return width();
160 }
161 
162 unsigned int
163 Int16::buf2val(void **val)
164 {
165  // Jose Garcia
166  // The same comment justifying throwing an Error in val2buf applies here.
167  if (!val)
168  throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
169 
170  if (!*val)
171  *val = new dods_int16;
172 
173  *(dods_int16 *)*val = _buf;
174 
175  return width();
176 }
177 
180 {
181  return _buf;
182 }
183 
184 bool
186 {
187  _buf = i;
188  set_read_p(true);
189 
190  return true;
191 }
192 
193 #if FILE_METHODS
194 void
195 Int16::print_val(FILE *out, string space, bool print_decl_p)
196 {
197  if (print_decl_p) {
198  print_decl(out, space, false);
199  fprintf(out, " = %d;\n", _buf) ;
200  }
201  else
202  fprintf(out, "%d", _buf) ;
203 }
204 #endif
205 
206 void
207 Int16::print_val(ostream &out, string space, bool print_decl_p)
208 {
209  if (print_decl_p) {
210  print_decl(out, space, false);
211  out << " = " << _buf << ";\n" ;
212  }
213  else
214  out << _buf ;
215 }
216 
217 bool
218 Int16::ops(BaseType *b, int op)
219 {
220 
221  // Extract the Byte arg's value.
222  if (!read_p() && !read()) {
223  // Jose Garcia
224  // Since the read method is virtual and implemented outside
225  // libdap++ if we cannot read the data that is the problem
226  // of the user or of whoever wrote the surrogate library
227  // implemeting read therefore it is an internal error.
228  throw InternalErr(__FILE__, __LINE__, "This value not read!");
229  }
230 
231  // Extract the second arg's value.
232  if (!b || !(b->read_p() || b->read())) {
233  // Jose Garcia
234  // Since the read method is virtual and implemented outside
235  // libdap++ if we cannot read the data that is the problem
236  // of the user or of whoever wrote the surrogate library
237  // implemeting read therefore it is an internal error.
238  throw InternalErr(__FILE__, __LINE__, "This value not read!");
239  }
240 
241  switch (b->type()) {
242  case dods_byte_c:
243  return rops<dods_int16, dods_byte, SUCmp<dods_int16, dods_byte> >
244  (_buf, dynamic_cast<Byte *>(b)->_buf, op);
245  case dods_int16_c:
246  return rops<dods_int16, dods_int16, Cmp<dods_int16, dods_int16> >
247  (_buf, dynamic_cast<Int16 *>(b)->_buf, op);
248  case dods_uint16_c:
249  return rops<dods_int16, dods_uint16, SUCmp<dods_int16, dods_uint16> >
250  (_buf, dynamic_cast<UInt16 *>(b)->_buf, op);
251  case dods_int32_c:
252  return rops<dods_int16, dods_int32, Cmp<dods_int16, dods_int32> >
253  (_buf, dynamic_cast<Int32 *>(b)->_buf, op);
254  case dods_uint32_c:
255  return rops<dods_int16, dods_uint32, SUCmp<dods_int16, dods_uint32> >
256  (_buf, dynamic_cast<UInt32 *>(b)->_buf, op);
257  case dods_float32_c:
258  return rops<dods_int16, dods_float32, Cmp<dods_int16, dods_float32> >
259  (_buf, dynamic_cast<Float32 *>(b)->_buf, op);
260  case dods_float64_c:
261  return rops<dods_int16, dods_float64, Cmp<dods_int16, dods_float64> >
262  (_buf, dynamic_cast<Float64 *>(b)->_buf, op);
263  default:
264  return false;
265  }
266 }
267 
276 void
277 Int16::dump(ostream &strm) const
278 {
279  strm << DapIndent::LMarg << "Int16::dump - ("
280  << (void *)this << ")" << endl ;
282  BaseType::dump(strm) ;
283  strm << DapIndent::LMarg << "value: " << _buf << endl ;
285 }
286 
287 } // namespace libdap
288