libdap++ Updated for version 3.8.2

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