libdap++ Updated for version 3.8.2

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