libdap  Updated for version 3.17.0
Float32.cc
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1999
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00031 
00032 // Implementation for Float32.
00033 //
00034 // 3/22/99 jhrg
00035 
00036 #include "config.h"
00037 
00038 #include <sstream>
00039 #include <iomanip>
00040 
00041 #include "Byte.h"           // synonymous with UInt8 and Char
00042 #include "Int8.h"
00043 #include "Int16.h"
00044 #include "UInt16.h"
00045 #include "Int32.h"
00046 #include "UInt32.h"
00047 #include "Int64.h"
00048 #include "UInt64.h"
00049 #include "Float32.h"
00050 #include "Float64.h"
00051 #include "Str.h"
00052 #include "Url.h"
00053 
00054 #include "DDS.h"
00055 #include "Marshaller.h"
00056 #include "UnMarshaller.h"
00057 
00058 #include "DMR.h"
00059 #include "D4StreamMarshaller.h"
00060 #include "D4StreamUnMarshaller.h"
00061 
00062 #include "util.h"
00063 #include "parser.h"
00064 #include "Operators.h"
00065 #include "dods-limits.h"
00066 #include "InternalErr.h"
00067 
00068 
00069 using std::cerr;
00070 using std::endl;
00071 
00072 namespace libdap {
00073 
00081 Float32::Float32(const string &n) : BaseType(n, dods_float32_c), d_buf(0)
00082 {}
00083 
00091 Float32::Float32(const string &n, const string &d) : BaseType(n, d, dods_float32_c), d_buf(0)
00092 {}
00093 
00094 Float32::Float32(const Float32 &copy_from) : BaseType(copy_from)
00095 {
00096     d_buf = copy_from.d_buf;
00097 }
00098 
00099 BaseType *
00100 Float32::ptr_duplicate()
00101 {
00102     return new Float32(*this);
00103 }
00104 
00105 Float32 &
00106 Float32::operator=(const Float32 &rhs)
00107 {
00108     if (this == &rhs)
00109         return *this;
00110 
00111     dynamic_cast<BaseType &>(*this) = rhs;
00112 
00113     d_buf = rhs.d_buf;
00114 
00115     return *this;
00116 }
00117 
00118 unsigned int
00119 Float32::width(bool) const
00120 {
00121     return sizeof(dods_float32);
00122 }
00123 
00124 bool
00125 Float32::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
00126 {
00127 #if USE_LOCAL_TIMEOUT_SCHEME
00128     dds.timeout_on();
00129 #endif
00130     if (!read_p())
00131         read();  // read() throws Error and InternalErr
00132 
00133     if (ce_eval && !eval.eval_selection(dds, dataset()))
00134         return true;
00135 #if USE_LOCAL_TIMEOUT_SCHEME
00136     dds.timeout_off();
00137 #endif
00138     m.put_float32( d_buf ) ;
00139 
00140     return true;
00141 }
00142 
00143 bool
00144 Float32::deserialize(UnMarshaller &um, DDS *, bool)
00145 {
00146     um.get_float32( d_buf ) ;
00147 
00148     return false;
00149 }
00150 
00151 void
00152 Float32::compute_checksum(Crc32 &checksum)
00153 {
00154         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00155 }
00156 
00165 void
00166 Float32::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00167 {
00168     if (!read_p())
00169         read();          // read() throws Error
00170 
00171     m.put_float32( d_buf ) ;
00172 }
00173 
00174 void
00175 Float32::deserialize(D4StreamUnMarshaller &um, DMR &)
00176 {
00177     um.get_float32( d_buf ) ;
00178 }
00179 
00180 unsigned int
00181 Float32::val2buf(void *val, bool)
00182 {
00183     // Jose Garcia This method is public I believe it has been
00184     // designed to be used by read which must be implemented in a
00185     // subclass, thus if the pointer val is NULL, is an Internal
00186     // Error.
00187     // Changed to InternalErr. jhrg
00188     if (!val)
00189         throw InternalErr(__FILE__, __LINE__,
00190                           "The incoming pointer does not contain any data.");
00191 
00192     d_buf = *(dods_float32 *)val;
00193 
00194     return width();
00195 }
00196 
00197 unsigned int
00198 Float32::buf2val(void **val)
00199 {
00200     // Jose Garcia
00201     // The same comment justifying throwing an Error in val2buf applies here.
00202     if (!val)
00203         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00204 
00205     if (!*val)
00206         *val = new dods_float32;
00207 
00208     *(dods_float32 *)*val = d_buf;
00209 
00210     return width();
00211 }
00212 
00213 bool
00214 Float32::set_value(dods_float32 f)
00215 {
00216     d_buf = f;
00217     set_read_p(true);
00218 
00219     return true;
00220 }
00221 
00227 dods_float32
00228 Float32::value() const
00229 {
00230     return d_buf;
00231 }
00232 
00233 void
00234 Float32::print_val(FILE *out, string space, bool print_decl_p)
00235 {
00236     ostringstream oss;
00237     print_val(oss, space, print_decl_p);
00238     fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
00239 }
00240 
00241 void
00242 Float32::print_val(ostream &out, string space, bool print_decl_p)
00243 {
00244     // FIX: need to set precision in the printing somehow.
00245     // os.precision(DODS_FLT_DIG);
00246 
00247     if (print_decl_p) {
00248         print_decl(out, space, false);
00249         out << " = " << std::setprecision( 6 ) << d_buf << ";\n" ;
00250     }
00251     else
00252         out << std::setprecision( 6 ) << d_buf ;
00253 }
00254 
00255 bool
00256 Float32::ops(BaseType *b, int op)
00257 {
00258     // Get this instance's value
00259     if (!read_p() && !read()) {
00260         // Jose Garcia Since the read method is virtual and
00261         // implemented outside libdap++ if we cannot read the data
00262         // that is the problem of whomever wrote the implementation of
00263         // read and therefore it is an internal error.
00264         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00265     }
00266 
00267     // Extract the second arg's value.
00268     if (!b || !(b->read_p() || b->read())) {
00269         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00270     }
00271 
00272     switch (b->type()) {
00273         case dods_int8_c:
00274             return Cmp<dods_float32, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00275         case dods_byte_c:
00276             return SUCmp<dods_float32, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00277         case dods_int16_c:
00278             return Cmp<dods_float32, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00279         case dods_uint16_c:
00280             return SUCmp<dods_float32, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00281         case dods_int32_c:
00282             return Cmp<dods_float32, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00283         case dods_uint32_c:
00284             return SUCmp<dods_float32, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00285         case dods_int64_c:
00286             return Cmp<dods_float32, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00287         case dods_uint64_c:
00288             return SUCmp<dods_float32, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00289         case dods_float32_c:
00290             return Cmp<dods_float32, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00291         case dods_float64_c:
00292             return Cmp<dods_float32, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00293         default:
00294             return false;
00295     }
00296 }
00297 
00306 void
00307 Float32::dump(ostream &strm) const
00308 {
00309     strm << DapIndent::LMarg << "Float32::dump - (" << (void *)this << ")"
00310          << endl ;
00311     DapIndent::Indent() ;
00312     BaseType::dump(strm) ;
00313     strm << DapIndent::LMarg << "value: " << d_buf << endl ;
00314     DapIndent::UnIndent() ;
00315 }
00316 
00317 } // namespace libdap
00318