libdap++  Updated for version 3.11.7
Float32.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 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 Float32.
33 //
34 // 3/22/99 jhrg
35 
36 
37 #include <iomanip>
38 
39 #include "config.h"
40 
41 static char rcsid[] not_used =
42  {"$Id: Float32.cc 21699 2009-11-05 00:06:01Z jimg $"
43  };
44 
45 #include "Byte.h"
46 #include "Int16.h"
47 #include "UInt16.h"
48 #include "Int32.h"
49 #include "UInt32.h"
50 #include "Float32.h"
51 #include "Float64.h"
52 #include "Str.h"
53 #include "Url.h"
54 #include "Array.h"
55 #include "Structure.h"
56 #include "Sequence.h"
57 #include "Grid.h"
58 
59 #include "DDS.h"
60 #include "util.h"
61 #include "parser.h"
62 #include "Operators.h"
63 #include "dods-limits.h"
64 #include "InternalErr.h"
65 
66 
67 using std::cerr;
68 using std::endl;
69 
70 namespace libdap {
71 
79 Float32::Float32(const string &n)
81 {}
82 
90 Float32::Float32(const string &n, const string &d)
91  : BaseType(n, d, dods_float32_c)
92 {}
93 
94 Float32::Float32(const Float32 &copy_from) : BaseType(copy_from)
95 {
96  _buf = copy_from._buf;
97 }
98 
99 BaseType *
101 {
102  return new Float32(*this);
103 }
104 
105 Float32 &
107 {
108  if (this == &rhs)
109  return *this;
110 
111  dynamic_cast<BaseType &>(*this) = rhs;
112 
113  _buf = rhs._buf;
114 
115  return *this;
116 }
117 
118 unsigned int
120 {
121  return sizeof(dods_float32);
122 }
123 
124 bool
126  Marshaller &m, bool ce_eval)
127 {
128  dds.timeout_on();
129 
130  if (!read_p())
131  read(); // read() throws Error and InternalErr
132 
133 #if EVAL
134  if (ce_eval && !eval.eval_selection(dds, dataset()))
135  return true;
136 #endif
137 
138  dds.timeout_off();
139 
140  m.put_float32( _buf ) ;
141 
142  return true;
143 }
144 
145 bool
147 {
148  um.get_float32( _buf ) ;
149 
150  return false;
151 }
152 
153 unsigned int
154 Float32::val2buf(void *val, bool)
155 {
156  // Jose Garcia This method is public I believe it has been
157  // designed to be used by read which must be implemented in a
158  // subclass, thus if the pointer val is NULL, is an Internal
159  // Error.
160  // Changed to InternalErr. jhrg
161  if (!val)
162  throw InternalErr(__FILE__, __LINE__,
163  "The incoming pointer does not contain any data.");
164 
165  _buf = *(dods_float32 *)val;
166 
167  return width();
168 }
169 
170 unsigned int
171 Float32::buf2val(void **val)
172 {
173  // Jose Garcia
174  // The same comment justifying throwing an Error in val2buf applies here.
175  if (!val)
176  throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
177 
178  if (!*val)
179  *val = new dods_float32;
180 
181  *(dods_float32 *)*val = _buf;
182 
183  return width();
184 }
185 
186 bool
188 {
189  _buf = f;
190  set_read_p(true);
191 
192  return true;
193 }
194 
202 {
203  return _buf;
204 }
205 
206 #if FILE_METHODS
207 void
208 Float32::print_val(FILE *out, string space, bool print_decl_p)
209 {
210  // FIX: need to set precision in the printing somehow.
211  // os.precision(DODS_FLT_DIG);
212 
213  if (print_decl_p) {
214  print_decl(out, space, false);
215  fprintf(out, " = %.6g;\n", _buf) ;
216  }
217  else
218  fprintf(out, "%.6g", _buf) ;
219 }
220 #endif
221 
222 void
223 Float32::print_val(ostream &out, string space, bool print_decl_p)
224 {
225  // FIX: need to set precision in the printing somehow.
226  // os.precision(DODS_FLT_DIG);
227 
228  if (print_decl_p) {
229  print_decl(out, space, false);
230  out << " = " << std::setprecision( 6 ) << _buf << ";\n" ;
231  }
232  else
233  out << std::setprecision( 6 ) << _buf ;
234 }
235 
236 bool
238 {
239  // Get this instance's value
240  if (!read_p() && !read()) {
241  // Jose Garcia Since the read method is virtual and
242  // implemented outside libdap++ if we cannot read the data
243  // that is the problem of whomever wrote the implementation of
244  // read and therefore it is an internal error.
245  throw InternalErr(__FILE__, __LINE__, "This value not read!");
246  }
247 
248  // Extract the second arg's value.
249  if (!b || !(b->read_p() || b->read())) {
250  throw InternalErr(__FILE__, __LINE__, "This value not read!");
251  }
252 
253  switch (b->type()) {
254  case dods_byte_c:
255  return rops<dods_float32, dods_byte, Cmp<dods_float32, dods_byte> >
256  (_buf, dynamic_cast<Byte *>(b)->_buf, op);
257  case dods_int16_c:
258  return rops<dods_float32, dods_int16, Cmp<dods_float32, dods_int16> >
259  (_buf, dynamic_cast<Int16 *>(b)->_buf, op);
260  case dods_uint16_c:
261  return rops<dods_float32, dods_uint16, Cmp<dods_float32, dods_uint16> >
262  (_buf, dynamic_cast<UInt16 *>(b)->_buf, op);
263  case dods_int32_c:
264  return rops<dods_float32, dods_int32, Cmp<dods_float32, dods_int32> >
265  (_buf, dynamic_cast<Int32 *>(b)->_buf, op);
266  case dods_uint32_c:
267  return rops<dods_float32, dods_uint32, Cmp<dods_float32, dods_uint32> >
268  (_buf, dynamic_cast<UInt32 *>(b)->_buf, op);
269  case dods_float32_c:
270  return rops<dods_float32, dods_float32, Cmp<dods_float32, dods_float32> >
271  (_buf, dynamic_cast<Float32 *>(b)->_buf, op);
272  case dods_float64_c:
273  return rops<dods_float32, dods_float64, Cmp<dods_float32, dods_float64> >
274  (_buf, dynamic_cast<Float64 *>(b)->_buf, op);
275  default:
276  return false;
277  }
278 }
279 
288 void
289 Float32::dump(ostream &strm) const
290 {
291  strm << DapIndent::LMarg << "Float32::dump - (" << (void *)this << ")"
292  << endl ;
294  BaseType::dump(strm) ;
295  strm << DapIndent::LMarg << "value: " << _buf << endl ;
297 }
298 
299 } // namespace libdap
300 
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:788
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:444
static void UnIndent()
Definition: DapIndent.cc:49
abstract base class used to unmarshall/deserialize dap data objects
Definition: UnMarshaller.h:54
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:851
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: Float32.cc:208
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
Receive data from the net.
Definition: Float32.cc:146
#define not_used
Definition: config.h:853
virtual void put_float32(dods_float32 val)=0
virtual void get_float32(dods_float32 &val)=0
Holds an unsigned 16-bit integer.
Definition: UInt16.h:59
Float32 & operator=(const Float32 &rhs)
Definition: Float32.cc:106
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
Move data to the net.
Definition: Float32.cc:125
void timeout_off()
Definition: DDS.cc:758
virtual unsigned int val2buf(void *val, bool reuse=false)
Loads class data.
Definition: Float32.cc:154
Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:238
Holds a 32-bit floating point value.
Definition: Float32.h:61
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Float32.cc:289
A class for software fault reporting.
Definition: InternalErr.h:64
string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:231
bool eval_selection(DDS &dds, const string &dataset)
Evaluate a boolean-valued constraint expression. This is main method for the evaluator ans is called ...
Float32(const string &n)
Definition: Float32.cc:79
Holds a 16-bit signed integer value.
Definition: Int16.h:59
static void Indent()
Definition: DapIndent.cc:43
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:186
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:483
virtual bool set_value(dods_float32 f)
Definition: Float32.cc:187
virtual unsigned int width()
Returns the size of the class instance data.
Definition: Float32.cc:119
void timeout_on()
Definition: DDS.cc:750
Evaluate a constraint expression.
virtual BaseType * ptr_duplicate()
Definition: Float32.cc:100
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:78
dods_float32 _buf
Definition: Float32.h:74
virtual unsigned int buf2val(void **val)
Reads the class data.
Definition: Float32.cc:171
The basic data type for the DODS DAP types.
Definition: BaseType.h:194
abstract base class used to marshal/serialize dap data objects
Definition: Marshaller.h:53
Holds a 64-bit (double precision) floating point value.
Definition: Float64.h:62
Holds a single byte.
Definition: Byte.h:63
virtual dods_float32 value() const
Definition: Float32.cc:201
Holds a 32-bit unsigned integer.
Definition: UInt32.h:61
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Float32.cc:237
Holds a 32-bit signed integer.
Definition: Int32.h:62