libdap  Updated for version 3.17.0
UInt16.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 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 Int32.
00033 //
00034 // jhrg 9/7/94
00035 
00036 
00037 #include "config.h"
00038 
00039 #include <sstream>
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 "debug.h"
00067 #include "InternalErr.h"
00068 
00069 using std::cerr;
00070 using std::endl;
00071 
00072 namespace libdap {
00073 
00078 UInt16::UInt16(const string &n) : BaseType(n, dods_uint16_c), d_buf(0)
00079 {}
00080 
00088 UInt16::UInt16(const string &n, const string &d) : BaseType(n, d, dods_uint16_c), d_buf(0)
00089 {}
00090 
00091 UInt16::UInt16(const UInt16 &copy_from) : BaseType(copy_from)
00092 {
00093     d_buf = copy_from.d_buf;
00094 }
00095 
00096 BaseType *
00097 UInt16::ptr_duplicate()
00098 {
00099     return new UInt16(*this);
00100 }
00101 
00102 UInt16 &
00103 UInt16::operator=(const UInt16 &rhs)
00104 {
00105     if (this == &rhs)
00106         return *this;
00107 
00108     dynamic_cast<BaseType &>(*this) = rhs;
00109 
00110     d_buf = rhs.d_buf;
00111 
00112     return *this;
00113 }
00114 
00115 unsigned int
00116 UInt16::width(bool) const
00117 {
00118     return sizeof(dods_uint16);
00119 }
00120 
00121 bool
00122 UInt16::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
00123 {
00124 #if USE_LOCAL_TIMEOUT_SCHEME
00125     dds.timeout_on();
00126 #endif
00127     if (!read_p())
00128         read();  // read() throws Error and InternalErr
00129 
00130     if (ce_eval && !eval.eval_selection(dds, dataset()))
00131         return true;
00132 #if USE_LOCAL_TIMEOUT_SCHEME
00133     dds.timeout_off();
00134 #endif
00135     m.put_uint16( d_buf ) ;
00136 
00137     return true;
00138 }
00139 
00140 bool
00141 UInt16::deserialize(UnMarshaller &um, DDS *, bool)
00142 {
00143     um.get_uint16( d_buf ) ;
00144 
00145     return false;
00146 }
00147 
00148 void
00149 UInt16::compute_checksum(Crc32 &checksum)
00150 {
00151         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00152 }
00153 
00162 void
00163 UInt16::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00164 {
00165     if (!read_p())
00166         read();          // read() throws Error
00167 
00168     m.put_uint16( d_buf ) ;
00169 }
00170 
00171 void
00172 UInt16::deserialize(D4StreamUnMarshaller &um, DMR &)
00173 {
00174     um.get_uint16( d_buf ) ;
00175 }
00176 
00177 unsigned int
00178 UInt16::val2buf(void *val, bool)
00179 {
00180     // Jose Garcia
00181     // This method is public therefore and I believe it has being designed
00182     // to be use by read which must be implemented on the surrogated library,
00183     // thus if the pointer val is NULL, is an Internal Error.
00184     if (!val)
00185         throw InternalErr(__FILE__, __LINE__,
00186                           "The incoming pointer does not contain any data.");
00187 
00188     d_buf = *(dods_uint16 *)val;
00189 
00190     return width();
00191 }
00192 
00193 unsigned int
00194 UInt16::buf2val(void **val)
00195 {
00196     // Jose Garcia
00197     // The same comment justifying throwing an Error in val2buf applies here.
00198     if (!val)
00199         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00200 
00201     if (!*val)
00202         *val = new dods_uint16;
00203 
00204     *(dods_uint16 *)*val = d_buf;
00205 
00206     return width();
00207 }
00208 
00209 dods_uint16
00210 UInt16::value() const
00211 {
00212     return d_buf;
00213 }
00214 
00215 bool
00216 UInt16::set_value(dods_uint16 i)
00217 {
00218     d_buf = i;
00219     set_read_p(true);
00220 
00221     return true;
00222 }
00223 
00224 void
00225 UInt16::print_val(FILE *out, string space, bool print_decl_p)
00226 {
00227     ostringstream oss;
00228     print_val(oss, space, print_decl_p);
00229     fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
00230 }
00231 
00232 void
00233 UInt16::print_val(ostream &out, string space, bool print_decl_p)
00234 {
00235     if (print_decl_p) {
00236         print_decl(out, space, false);
00237         out << " = " << (unsigned int)d_buf << ";\n" ;
00238     }
00239     else
00240         out << (unsigned int)d_buf ;
00241 }
00242 
00243 bool
00244 UInt16::ops(BaseType *b, int op)
00245 {
00246     // Extract the Byte arg's value.
00247     if (!read_p() && !read()) {
00248         // Jose Garcia
00249         // Since the read method is virtual and implemented outside
00250         // libdap++ if we cannot read the data that is the problem
00251         // of the user or of whoever wrote the surrogate library
00252         // implemeting read therefore it is an internal error.
00253         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00254     }
00255 
00256     // Extract the second arg's value.
00257     if (!b || !(b->read_p() || b->read())) {
00258         // Jose Garcia
00259         // Since the read method is virtual and implemented outside
00260         // libdap++ if we cannot read the data that is the problem
00261         // of the user or of whoever wrote the surrogate library
00262         // implemeting read therefore it is an internal error.
00263         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00264     }
00265 
00266     switch (b->type()) {
00267         case dods_int8_c:
00268             return USCmp<dods_uint16, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00269         case dods_byte_c:
00270             return Cmp<dods_uint16, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00271         case dods_int16_c:
00272             return USCmp<dods_uint16, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00273         case dods_uint16_c:
00274             return Cmp<dods_uint16, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00275         case dods_int32_c:
00276             return USCmp<dods_uint16, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00277         case dods_uint32_c:
00278             return Cmp<dods_uint16, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00279         case dods_int64_c:
00280             return USCmp<dods_uint16, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00281         case dods_uint64_c:
00282             return Cmp<dods_uint16, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00283         case dods_float32_c:
00284             return USCmp<dods_uint16, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00285         case dods_float64_c:
00286             return USCmp<dods_uint16, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00287         default:
00288             return false;
00289     }
00290 }
00291 
00300 void
00301 UInt16::dump(ostream &strm) const
00302 {
00303     strm << DapIndent::LMarg << "UInt16::dump - ("
00304     << (void *)this << ")" << endl ;
00305     DapIndent::Indent() ;
00306     BaseType::dump(strm) ;
00307     strm << DapIndent::LMarg << "value: " << d_buf << endl ;
00308     DapIndent::UnIndent() ;
00309 }
00310 
00311 } // namespace libdap
00312