libdap++ Updated for version 3.8.2
|
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/99 jhrg 00035 00036 00037 #include <iomanip> 00038 00039 #include "config.h" 00040 00041 static char rcsid[] not_used = 00042 {"$Id: Float32.cc 20518 2009-03-05 23:39:46Z jimg $" 00043 }; 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 00057 namespace libdap { 00058 00066 Float32::Float32(const string &n) 00067 : BaseType(n, dods_float32_c) 00068 {} 00069 00077 Float32::Float32(const string &n, const string &d) 00078 : BaseType(n, d, dods_float32_c) 00079 {} 00080 00081 Float32::Float32(const Float32 ©_from) : BaseType(copy_from) 00082 { 00083 _buf = copy_from._buf; 00084 } 00085 00086 BaseType * 00087 Float32::ptr_duplicate() 00088 { 00089 return new Float32(*this); 00090 } 00091 00092 Float32 & 00093 Float32::operator=(const Float32 &rhs) 00094 { 00095 if (this == &rhs) 00096 return *this; 00097 00098 dynamic_cast<BaseType &>(*this) = rhs; 00099 00100 _buf = rhs._buf; 00101 00102 return *this; 00103 } 00104 00105 unsigned int 00106 Float32::width() 00107 { 00108 return sizeof(dods_float32); 00109 } 00110 00111 bool 00112 Float32::serialize(ConstraintEvaluator &eval, DDS &dds, 00113 Marshaller &m, bool ce_eval) 00114 { 00115 dds.timeout_on(); 00116 00117 if (!read_p()) 00118 read(); // read() throws Error and InternalErr 00119 00120 #if EVAL 00121 if (ce_eval && !eval.eval_selection(dds, dataset())) 00122 return true; 00123 #endif 00124 00125 dds.timeout_off(); 00126 00127 m.put_float32( _buf ) ; 00128 00129 return true; 00130 } 00131 00132 bool 00133 Float32::deserialize(UnMarshaller &um, DDS *, bool) 00134 { 00135 um.get_float32( _buf ) ; 00136 00137 return false; 00138 } 00139 00140 unsigned int 00141 Float32::val2buf(void *val, bool) 00142 { 00143 // Jose Garcia This method is public I believe it has been 00144 // designed to be used by read which must be implemented in a 00145 // subclass, thus if the pointer val is NULL, is an Internal 00146 // Error. 00147 // Changed to InternalErr. jhrg 00148 if (!val) 00149 throw InternalErr(__FILE__, __LINE__, 00150 "The incoming pointer does not contain any data."); 00151 00152 _buf = *(dods_float32 *)val; 00153 00154 return width(); 00155 } 00156 00157 unsigned int 00158 Float32::buf2val(void **val) 00159 { 00160 // Jose Garcia 00161 // The same comment justifying throwing an Error in val2buf applies here. 00162 if (!val) 00163 throw InternalErr(__FILE__, __LINE__, "NULL pointer."); 00164 00165 if (!*val) 00166 *val = new dods_float32; 00167 00168 *(dods_float32 *)*val = _buf; 00169 00170 return width(); 00171 } 00172 00173 bool 00174 Float32::set_value(dods_float32 f) 00175 { 00176 _buf = f; 00177 set_read_p(true); 00178 00179 return true; 00180 } 00181 00187 dods_float32 00188 Float32::value() const 00189 { 00190 return _buf; 00191 } 00192 //#if FILE_METHODS 00193 void 00194 Float32::print_val(FILE *out, string space, bool print_decl_p) 00195 { 00196 // FIX: need to set precision in the printing somehow. 00197 // os.precision(DODS_FLT_DIG); 00198 00199 if (print_decl_p) { 00200 print_decl(out, space, false); 00201 fprintf(out, " = %.6g;\n", _buf) ; 00202 } 00203 else 00204 fprintf(out, "%.6g", _buf) ; 00205 } 00206 //#endif 00207 void 00208 Float32::print_val(ostream &out, string space, bool print_decl_p) 00209 { 00210 // FIX: need to set precision in the printing somehow. 00211 // os.precision(DODS_FLT_DIG); 00212 00213 if (print_decl_p) { 00214 print_decl(out, space, false); 00215 out << " = " << std::setprecision( 6 ) << _buf << ";\n" ; 00216 } 00217 else 00218 out << std::setprecision( 6 ) << _buf ; 00219 } 00220 00221 bool 00222 Float32::ops(BaseType *b, int op) 00223 { 00224 // Get this instance's value 00225 if (!read_p() && !read()) { 00226 // Jose Garcia Since the read method is virtual and 00227 // implemented outside libdap++ if we cannot read the data 00228 // that is the problem of whomever wrote the implementation of 00229 // read and therefore it is an internal error. 00230 throw InternalErr(__FILE__, __LINE__, "This value not read!"); 00231 } 00232 00233 // Extract the second arg's value. 00234 if (!b || !b->read_p() && !b->read()) { 00235 throw InternalErr(__FILE__, __LINE__, "This value not read!"); 00236 } 00237 00238 switch (b->type()) { 00239 case dods_byte_c: 00240 return rops<dods_float32, dods_byte, Cmp<dods_float32, dods_byte> > 00241 (_buf, dynamic_cast<Byte *>(b)->_buf, op); 00242 case dods_int16_c: 00243 return rops<dods_float32, dods_int16, Cmp<dods_float32, dods_int16> > 00244 (_buf, dynamic_cast<Int16 *>(b)->_buf, op); 00245 case dods_uint16_c: 00246 return rops<dods_float32, dods_uint16, Cmp<dods_float32, dods_uint16> > 00247 (_buf, dynamic_cast<UInt16 *>(b)->_buf, op); 00248 case dods_int32_c: 00249 return rops<dods_float32, dods_int32, Cmp<dods_float32, dods_int32> > 00250 (_buf, dynamic_cast<Int32 *>(b)->_buf, op); 00251 case dods_uint32_c: 00252 return rops<dods_float32, dods_uint32, Cmp<dods_float32, dods_uint32> > 00253 (_buf, dynamic_cast<UInt32 *>(b)->_buf, op); 00254 case dods_float32_c: 00255 return rops<dods_float32, dods_float32, Cmp<dods_float32, dods_float32> > 00256 (_buf, dynamic_cast<Float32 *>(b)->_buf, op); 00257 case dods_float64_c: 00258 return rops<dods_float32, dods_float64, Cmp<dods_float32, dods_float64> > 00259 (_buf, dynamic_cast<Float64 *>(b)->_buf, op); 00260 default: 00261 return false; 00262 } 00263 } 00264 00273 void 00274 Float32::dump(ostream &strm) const 00275 { 00276 strm << DapIndent::LMarg << "Float32::dump - (" << (void *)this << ")" 00277 << endl ; 00278 DapIndent::Indent() ; 00279 BaseType::dump(strm) ; 00280 strm << DapIndent::LMarg << "value: " << _buf << endl ; 00281 DapIndent::UnIndent() ; 00282 } 00283 00284 } // namespace libdap 00285