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

Generated on Tue Jun 10 18:00:31 2008 for libdap++ by  doxygen 1.5.4