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

Generated on Fri Feb 8 05:08:57 2008 for libdap++ by  doxygen 1.5.4