libdap  Updated for version 3.17.0
Float64.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 1994-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 Float64.
00033 //
00034 // jhrg 9/7/94
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 
00082 Float64::Float64(const string &n) : BaseType(n, dods_float64_c), d_buf(0)
00083 {}
00084 
00092 Float64::Float64(const string &n, const string &d) : BaseType(n, d, dods_float64_c), d_buf(0)
00093 {}
00094 
00095 Float64::Float64(const Float64 &copy_from) : BaseType(copy_from)
00096 {
00097     d_buf = copy_from.d_buf;
00098 }
00099 
00100 BaseType *
00101 Float64::ptr_duplicate()
00102 {
00103     return new Float64(*this);
00104 }
00105 
00106 Float64 &
00107 Float64::operator=(const Float64 &rhs)
00108 {
00109     if (this == &rhs)
00110         return *this;
00111 
00112     dynamic_cast<BaseType &>(*this) = rhs;
00113 
00114     d_buf = rhs.d_buf;
00115 
00116     return *this;
00117 }
00118 
00119 unsigned int
00120 Float64::width(bool) const
00121 {
00122     return sizeof(dods_float64);
00123 }
00124 
00125 bool
00126 Float64::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
00127 {
00128 #if USE_LOCAL_TIMEOUT_SCHEME
00129     dds.timeout_on();
00130 #endif
00131     if (!read_p())
00132         read();  // read() throws Error and InternalErr
00133 
00134     if (ce_eval && !eval.eval_selection(dds, dataset()))
00135         return true;
00136 #if USE_LOCAL_TIMEOUT_SCHEME
00137     dds.timeout_off();
00138 #endif
00139     m.put_float64( d_buf ) ;
00140 
00141     return true;
00142 }
00143 
00144 bool
00145 Float64::deserialize(UnMarshaller &um, DDS *, bool)
00146 {
00147     um.get_float64( d_buf ) ;
00148 
00149     return false;
00150 }
00151 
00152 void
00153 Float64::compute_checksum(Crc32 &checksum)
00154 {
00155         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00156 }
00157 
00166 void
00167 Float64::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00168 {
00169     if (!read_p())
00170         read();          // read() throws Error
00171 
00172     m.put_float64( d_buf ) ;
00173 }
00174 
00175 void
00176 Float64::deserialize(D4StreamUnMarshaller &um, DMR &)
00177 {
00178     um.get_float64( d_buf ) ;
00179 }
00180 
00181 unsigned int
00182 Float64::val2buf(void *val, bool)
00183 {
00184     // Jose Garcia
00185     // This method is public therefore and I believe it has being designed
00186     // to be use by read which must be implemented on the surrogated library,
00187     // thus if the pointer val is NULL, is an Internal Error.
00188     if (!val)
00189         throw InternalErr(__FILE__, __LINE__,
00190                           "The incoming pointer does not contain any data.");
00191 
00192     d_buf = *(dods_float64 *)val;
00193 
00194     return width();
00195 }
00196 
00197 unsigned int
00198 Float64::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_float64;
00207 
00208     *(dods_float64 *)*val = d_buf;
00209 
00210     return width();
00211 }
00212 
00218 dods_float64
00219 Float64::value() const
00220 {
00221     return d_buf;
00222 }
00223 
00224 bool
00225 Float64::set_value(dods_float64 val)
00226 {
00227     d_buf = val;
00228     set_read_p(true);
00229 
00230     return true;
00231 }
00232 
00233 void
00234 Float64::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 Float64::print_val(ostream &out, string space, bool print_decl_p)
00243 {
00244     // Set the precision to 15 digits
00245     std::streamsize prec = out.precision(15);
00246 
00247     if (print_decl_p) {
00248         print_decl(out, space, false);
00249         out << " = " << d_buf << ";\n";
00250     }
00251     else
00252         out << d_buf;
00253 
00254     // reset the precision
00255     out.precision(prec);
00256 }
00257 
00258 bool
00259 Float64::ops(BaseType *b, int op)
00260 {
00261     // Extract the Byte arg's value.
00262     if (!read_p() && !read()) {
00263         // Jose Garcia
00264         // Since the read method is virtual and implemented outside
00265         // libdap++ if we cannot read the data that is the problem
00266         // of the user or of whoever wrote the surrogate library
00267         // implemeting read therefore it is an internal error.
00268         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00269     }
00270 
00271     // Extract the second arg's value.
00272     if (!b->read_p() && !b->read()) {
00273         // Jose Garcia
00274         // Since the read method is virtual and implemented outside
00275         // libdap++ if we cannot read the data that is the problem
00276         // of the user or of whoever wrote the surrogate library
00277         // implemeting read therefore it is an internal error.
00278         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00279     }
00280 
00281     switch (b->type()) {
00282         case dods_int8_c:
00283             return Cmp<dods_float64, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00284         case dods_byte_c:
00285             return SUCmp<dods_float64, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00286         case dods_int16_c:
00287             return Cmp<dods_float64, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00288         case dods_uint16_c:
00289             return SUCmp<dods_float64, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00290         case dods_int32_c:
00291             return Cmp<dods_float64, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00292         case dods_uint32_c:
00293             return SUCmp<dods_float64, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00294         case dods_int64_c:
00295             return Cmp<dods_float64, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00296         case dods_uint64_c:
00297             return SUCmp<dods_float64, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00298         case dods_float32_c:
00299             return Cmp<dods_float64, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00300         case dods_float64_c:
00301             return Cmp<dods_float64, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00302         default:
00303             return false;
00304     }
00305 }
00306 
00315 void
00316 Float64::dump(ostream &strm) const
00317 {
00318     strm << DapIndent::LMarg << "Float64::dump - ("
00319     << (void *)this << ")" << endl ;
00320     DapIndent::Indent() ;
00321     BaseType::dump(strm) ;
00322     strm << DapIndent::LMarg << "value: " << d_buf << endl ;
00323     DapIndent::UnIndent() ;
00324 }
00325 
00326 } // namespace libdap
00327