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

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