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 
00037 #include "config.h"
00038 
00039 static char rcsid[] not_used =
00040     {"$Id: Float64.cc 16088 2007-03-28 21:42:19Z jimg $"
00041     };
00042 
00043 #include <stdlib.h>
00044 
00045 #include <iomanip>
00046 
00047 #include "Float64.h"
00048 #include "DDS.h"
00049 #include "util.h"
00050 #include "parser.h"
00051 //#include "ce_expr.tab.h"
00052 #include "Operators.h"
00053 #include "dods-limits.h"
00054 #include "InternalErr.h"
00055 
00056 
00057 using std::cerr;
00058 using std::endl;
00059 
00068 Float64::Float64(const string &n)
00069         : BaseType(n, dods_float64_c, (xdrproc_t)XDR_FLOAT64)
00070 {}
00071 
00072 Float64::Float64(const Float64 &copy_from) : BaseType(copy_from)
00073 {
00074     _buf = copy_from._buf;
00075 }
00076 
00077 BaseType *
00078 Float64::ptr_duplicate()
00079 {
00080     return new Float64(*this);
00081 }
00082 
00083 Float64 &
00084 Float64::operator=(const Float64 &rhs)
00085 {
00086     if (this == &rhs)
00087         return *this;
00088 
00089     dynamic_cast<BaseType &>(*this) = rhs;
00090 
00091     _buf = rhs._buf;
00092 
00093     return *this;
00094 }
00095 
00096 unsigned int
00097 Float64::width()
00098 {
00099     return sizeof(dods_float64);
00100 }
00101 
00102 bool
00103 Float64::serialize(const string &dataset, ConstraintEvaluator &eval, DDS &dds,
00104                    XDR *sink, bool ce_eval)
00105 {
00106     dds.timeout_on();
00107 
00108     if (!read_p())
00109         read(dataset);  // read() throws Error and InternalErr
00110 
00111 #if EVAL
00112     if (ce_eval && !eval.eval_selection(dds, dataset))
00113         return true;
00114 #endif
00115 
00116     dds.timeout_off();
00117 
00118     if (!xdr_double(sink, &_buf))
00119         throw Error("Network I/O Error. Could not send float 64 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00120 
00121     return true;
00122 }
00123 
00124 bool
00125 Float64::deserialize(XDR *source, DDS *, bool)
00126 {
00127     if (!xdr_double(source, &_buf))
00128         throw Error("Network I/O Error. Could not read float 64 data. This may be due to a\nbug in libdap or a problem with the network connection.");
00129 
00130     return false;
00131 }
00132 
00133 unsigned int
00134 Float64::val2buf(void *val, bool)
00135 {
00136     // Jose Garcia
00137     // This method is public therefore and I believe it has being designed
00138     // to be use by read which must be implemented on the surrogated library,
00139     // thus if the pointer val is NULL, is an Internal Error.
00140     if (!val)
00141         throw InternalErr(__FILE__, __LINE__,
00142                           "The incoming pointer does not contain any data.");
00143 
00144     _buf = *(dods_float64 *)val;
00145 
00146     return width();
00147 }
00148 
00149 unsigned int
00150 Float64::buf2val(void **val)
00151 {
00152     // Jose Garcia
00153     // The same comment justifying throwing an Error in val2buf applies here.
00154     if (!val)
00155         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00156 
00157     if (!*val)
00158         *val = new dods_float64;
00159 
00160     *(dods_float64 *)*val = _buf;
00161 
00162     return width();
00163 }
00164 
00170 dods_float64
00171 Float64::value() const
00172 {
00173     return _buf;
00174 }
00175 
00176 bool
00177 Float64::set_value(dods_float64 val)
00178 {
00179     _buf = val;
00180     set_read_p(true);
00181 
00182     return true;
00183 }
00184 
00185 void
00186 Float64::print_val(FILE *out, string space, bool print_decl_p)
00187 {
00188     // FIX: need to set precision in the printing somehow.
00189     // os.precision(DODS_DBL_DIG);
00190 
00191     if (print_decl_p) {
00192         print_decl(out, space, false);
00193         fprintf(out, " = %.15g;\n", _buf) ;
00194     }
00195     else
00196         fprintf(out, "%.15g", _buf) ;
00197 }
00198 
00199 bool
00200 Float64::ops(BaseType *b, int op, const string &dataset)
00201 {
00202     // Extract the Byte arg's value.
00203     if (!read_p() && !read(dataset)) {
00204         // Jose Garcia
00205         // Since the read method is virtual and implemented outside
00206         // libdap++ if we can not read the data that is the problem
00207         // of the user or of whoever wrote the surrogate library
00208         // implemeting read therefore it is an internal error.
00209         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00210     }
00211 
00212     // Extract the second arg's value.
00213     if (!b->read_p() && !b->read(dataset)) {
00214         // Jose Garcia
00215         // Since the read method is virtual and implemented outside
00216         // libdap++ if we can not read the data that is the problem
00217         // of the user or of whoever wrote the surrogate library
00218         // implemeting read therefore it is an internal error.
00219         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00220     }
00221 
00222     switch (b->type()) {
00223     case dods_byte_c:
00224         return rops<dods_float64, dods_byte, Cmp<dods_float64, dods_byte> >
00225                (_buf, dynamic_cast<Byte *>(b)->_buf, op);
00226     case dods_int16_c:
00227         return rops<dods_float64, dods_int16, Cmp<dods_float64, dods_int16> >
00228                (_buf, dynamic_cast<Int16 *>(b)->_buf, op);
00229     case dods_uint16_c:
00230         return rops<dods_float64, dods_uint16, Cmp<dods_float64, dods_uint16> >
00231                (_buf, dynamic_cast<UInt16 *>(b)->_buf, op);
00232     case dods_int32_c:
00233         return rops<dods_float64, dods_int32, Cmp<dods_float64, dods_int32> >
00234                (_buf, dynamic_cast<Int32 *>(b)->_buf, op);
00235     case dods_uint32_c:
00236         return rops<dods_float64, dods_uint32, Cmp<dods_float64, dods_uint32> >
00237                (_buf, dynamic_cast<UInt32 *>(b)->_buf, op);
00238     case dods_float32_c:
00239         return rops<dods_float64, dods_float32, Cmp<dods_float64, dods_float32> >
00240                (_buf, dynamic_cast<Float32 *>(b)->_buf, op);
00241     case dods_float64_c:
00242         return rops<dods_float64, dods_float64, Cmp<dods_float64, dods_float64> >
00243                (_buf, dynamic_cast<Float64 *>(b)->_buf, op);
00244     default:
00245         return false;
00246     }
00247 }
00248 
00257 void
00258 Float64::dump(ostream &strm) const
00259 {
00260     strm << DapIndent::LMarg << "Float64::dump - ("
00261     << (void *)this << ")" << endl ;
00262     DapIndent::Indent() ;
00263     BaseType::dump(strm) ;
00264     strm << DapIndent::LMarg << "value: " << _buf << endl ;
00265     DapIndent::UnIndent() ;
00266 }
00267 

Generated on Wed Jun 27 12:56:39 2007 for libdap++ by  doxygen 1.4.7