libdap++ Updated for version 3.8.2

Float64.cc

Go to the documentation of this file.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <iomanip>
00037 
00038 #include "config.h"
00039 
00040 static char rcsid[] not_used =
00041     {"$Id: Float64.cc 21699 2009-11-05 00:06:01Z jimg $"
00042     };
00043 
00044 #include <iomanip>
00045 
00046 #include "Byte.h"
00047 #include "Int16.h"
00048 #include "UInt16.h"
00049 #include "Int32.h"
00050 #include "UInt32.h"
00051 #include "Float32.h"
00052 #include "Float64.h"
00053 #include "Str.h"
00054 #include "Url.h"
00055 #include "Array.h"
00056 #include "Structure.h"
00057 #include "Sequence.h"
00058 #include "Grid.h"
00059 
00060 #include "DDS.h"
00061 #include "util.h"
00062 #include "parser.h"
00063 #include "Operators.h"
00064 #include "dods-limits.h"
00065 #include "InternalErr.h"
00066 
00067 
00068 using std::cerr;
00069 using std::endl;
00070 
00071 namespace libdap {
00072 
00081 Float64::Float64(const string &n)
00082         : BaseType(n, dods_float64_c)
00083 {}
00084 
00092 Float64::Float64(const string &n, const string &d)
00093         : BaseType(n, d, dods_float64_c)
00094 {}
00095 
00096 Float64::Float64(const Float64 &copy_from) : BaseType(copy_from)
00097 {
00098     _buf = copy_from._buf;
00099 }
00100 
00101 BaseType *
00102 Float64::ptr_duplicate()
00103 {
00104     return new Float64(*this);
00105 }
00106 
00107 Float64 &
00108 Float64::operator=(const Float64 &rhs)
00109 {
00110     if (this == &rhs)
00111         return *this;
00112 
00113     dynamic_cast<BaseType &>(*this) = rhs;
00114 
00115     _buf = rhs._buf;
00116 
00117     return *this;
00118 }
00119 
00120 unsigned int
00121 Float64::width()
00122 {
00123     return sizeof(dods_float64);
00124 }
00125 
00126 bool
00127 Float64::serialize(ConstraintEvaluator &eval, DDS &dds,
00128                    Marshaller &m, bool ce_eval)
00129 {
00130     dds.timeout_on();
00131 
00132     if (!read_p())
00133         read();  // read() throws Error and InternalErr
00134 
00135 #if EVAL
00136     if (ce_eval && !eval.eval_selection(dds, dataset()))
00137         return true;
00138 #endif
00139 
00140     dds.timeout_off();
00141 
00142     m.put_float64( _buf ) ;
00143 
00144     return true;
00145 }
00146 
00147 bool
00148 Float64::deserialize(UnMarshaller &um, DDS *, bool)
00149 {
00150     um.get_float64( _buf ) ;
00151 
00152     return false;
00153 }
00154 
00155 unsigned int
00156 Float64::val2buf(void *val, bool)
00157 {
00158     // Jose Garcia
00159     // This method is public therefore and I believe it has being designed
00160     // to be use by read which must be implemented on the surrogated library,
00161     // thus if the pointer val is NULL, is an Internal Error.
00162     if (!val)
00163         throw InternalErr(__FILE__, __LINE__,
00164                           "The incoming pointer does not contain any data.");
00165 
00166     _buf = *(dods_float64 *)val;
00167 
00168     return width();
00169 }
00170 
00171 unsigned int
00172 Float64::buf2val(void **val)
00173 {
00174     // Jose Garcia
00175     // The same comment justifying throwing an Error in val2buf applies here.
00176     if (!val)
00177         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00178 
00179     if (!*val)
00180         *val = new dods_float64;
00181 
00182     *(dods_float64 *)*val = _buf;
00183 
00184     return width();
00185 }
00186 
00192 dods_float64
00193 Float64::value() const
00194 {
00195     return _buf;
00196 }
00197 
00198 bool
00199 Float64::set_value(dods_float64 val)
00200 {
00201     _buf = val;
00202     set_read_p(true);
00203 
00204     return true;
00205 }
00206 
00207 #if FILE_METHODS
00208 void
00209 Float64::print_val(FILE *out, string space, bool print_decl_p)
00210 {
00211     // FIX: need to set precision in the printing somehow.
00212     // os.precision(DODS_DBL_DIG);
00213 
00214     if (print_decl_p) {
00215         print_decl(out, space, false);
00216         fprintf(out, " = %.15g;\n", _buf) ;
00217     }
00218     else
00219         fprintf(out, "%.15g", _buf) ;
00220 }
00221 #endif
00222 
00223 void
00224 Float64::print_val(ostream &out, string space, bool print_decl_p)
00225 {
00226     // FIX: need to set precision in the printing somehow.
00227     // os.precision(DODS_DBL_DIG);
00228 
00229     if (print_decl_p) {
00230         print_decl(out, space, false);
00231         out << " = " << std::setprecision( 15 ) << _buf << ";\n" ;
00232     }
00233     else
00234         out << std::setprecision( 15 ) << _buf ;
00235 }
00236 
00237 bool
00238 Float64::ops(BaseType *b, int op)
00239 {
00240     // Extract the Byte arg's value.
00241     if (!read_p() && !read()) {
00242         // Jose Garcia
00243         // Since the read method is virtual and implemented outside
00244         // libdap++ if we cannot read the data that is the problem
00245         // of the user or of whoever wrote the surrogate library
00246         // implemeting read therefore it is an internal error.
00247         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00248     }
00249 
00250     // Extract the second arg's value.
00251     if (!b->read_p() && !b->read()) {
00252         // Jose Garcia
00253         // Since the read method is virtual and implemented outside
00254         // libdap++ if we cannot read the data that is the problem
00255         // of the user or of whoever wrote the surrogate library
00256         // implemeting read therefore it is an internal error.
00257         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00258     }
00259 
00260     switch (b->type()) {
00261     case dods_byte_c:
00262         return rops<dods_float64, dods_byte, Cmp<dods_float64, dods_byte> >
00263                (_buf, dynamic_cast<Byte *>(b)->_buf, op);
00264     case dods_int16_c:
00265         return rops<dods_float64, dods_int16, Cmp<dods_float64, dods_int16> >
00266                (_buf, dynamic_cast<Int16 *>(b)->_buf, op);
00267     case dods_uint16_c:
00268         return rops<dods_float64, dods_uint16, Cmp<dods_float64, dods_uint16> >
00269                (_buf, dynamic_cast<UInt16 *>(b)->_buf, op);
00270     case dods_int32_c:
00271         return rops<dods_float64, dods_int32, Cmp<dods_float64, dods_int32> >
00272                (_buf, dynamic_cast<Int32 *>(b)->_buf, op);
00273     case dods_uint32_c:
00274         return rops<dods_float64, dods_uint32, Cmp<dods_float64, dods_uint32> >
00275                (_buf, dynamic_cast<UInt32 *>(b)->_buf, op);
00276     case dods_float32_c:
00277         return rops<dods_float64, dods_float32, Cmp<dods_float64, dods_float32> >
00278                (_buf, dynamic_cast<Float32 *>(b)->_buf, op);
00279     case dods_float64_c:
00280         return rops<dods_float64, dods_float64, Cmp<dods_float64, dods_float64> >
00281                (_buf, dynamic_cast<Float64 *>(b)->_buf, op);
00282     default:
00283         return false;
00284     }
00285 }
00286 
00295 void
00296 Float64::dump(ostream &strm) const
00297 {
00298     strm << DapIndent::LMarg << "Float64::dump - ("
00299     << (void *)this << ")" << endl ;
00300     DapIndent::Indent() ;
00301     BaseType::dump(strm) ;
00302     strm << DapIndent::LMarg << "value: " << _buf << endl ;
00303     DapIndent::UnIndent() ;
00304 }
00305 
00306 } // namespace libdap
00307