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

Generated on Wed Nov 28 16:23:15 2007 for libdap++ by  doxygen 1.4.7