libdap  Updated for version 3.17.0
Int32.cc
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1994-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 #include <sstream>
00040 
00041 #include "Byte.h"           // synonymous with UInt8 and Char
00042 #include "Int8.h"
00043 #include "Int16.h"
00044 #include "UInt16.h"
00045 #include "Int32.h"
00046 #include "UInt32.h"
00047 #include "Int64.h"
00048 #include "UInt64.h"
00049 #include "Float32.h"
00050 #include "Float64.h"
00051 #include "Str.h"
00052 #include "Url.h"
00053 
00054 #include "DDS.h"
00055 #include "Marshaller.h"
00056 #include "UnMarshaller.h"
00057 
00058 #include "DMR.h"
00059 #include "D4StreamMarshaller.h"
00060 #include "D4StreamUnMarshaller.h"
00061 
00062 #include "util.h"
00063 #include "parser.h"
00064 #include "Operators.h"
00065 #include "dods-limits.h"
00066 #include "debug.h"
00067 #include "InternalErr.h"
00068 
00069 
00070 using std::cerr;
00071 using std::endl;
00072 
00073 namespace libdap {
00074 
00082 Int32::Int32(const string &n) : BaseType(n, dods_int32_c), d_buf(0)
00083 {}
00084 
00092 Int32::Int32(const string &n, const string &d) : BaseType(n, d, dods_int32_c), d_buf(0)
00093 {}
00094 
00095 Int32::Int32(const Int32 &copy_from) : BaseType(copy_from)
00096 {
00097     d_buf = copy_from.d_buf;
00098 }
00099 
00100 BaseType *
00101 Int32::ptr_duplicate()
00102 {
00103     return new Int32(*this);
00104 }
00105 
00106 Int32::~Int32()
00107 {
00108     DBG(cerr << "~Int32" << endl);
00109 }
00110 
00111 Int32 &
00112 Int32::operator=(const Int32 &rhs)
00113 {
00114     if (this == &rhs)
00115         return *this;
00116 
00117     dynamic_cast<BaseType &>(*this) = rhs;
00118 
00119     d_buf = rhs.d_buf;
00120 
00121     return *this;
00122 }
00123 
00124 unsigned int
00125 Int32::width(bool) const
00126 {
00127     return sizeof(dods_int32);
00128 }
00129 
00130 bool
00131 Int32::serialize(ConstraintEvaluator &eval, DDS &dds,
00132                  Marshaller &m, bool ce_eval)
00133 {
00134 #if USE_LOCAL_TIMEOUT_SCHEME
00135     dds.timeout_on();
00136 #endif
00137     if (!read_p())
00138         read();  // read() throws Error and InternalErr
00139 
00140     if (ce_eval && !eval.eval_selection(dds, dataset()))
00141         return true;
00142 #if USE_LOCAL_TIMEOUT_SCHEME
00143     dds.timeout_off();
00144 #endif
00145     m.put_int32( d_buf ) ;
00146 
00147     return true;
00148 }
00149 
00150 bool
00151 Int32::deserialize(UnMarshaller &um, DDS *, bool)
00152 {
00153     um.get_int32( d_buf ) ;
00154 
00155     return false;
00156 }
00157 
00158 void
00159 Int32::compute_checksum(Crc32 &checksum)
00160 {
00161         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00162 }
00163 
00172 void
00173 Int32::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00174 {
00175     if (!read_p())
00176         read();          // read() throws Error
00177 
00178     m.put_int32( d_buf ) ;
00179 }
00180 
00181 void
00182 Int32::deserialize(D4StreamUnMarshaller &um, DMR &)
00183 {
00184     um.get_int32( d_buf ) ;
00185 }
00186 
00187 unsigned int
00188 Int32::val2buf(void *val, bool)
00189 {
00190     // Jose Garcia
00191     // This method is public therefore and I believe it has being designed
00192     // to be use by read which must be implemented on the surrogated library,
00193     // thus if the pointer val is NULL, is an Internal Error.
00194     if (!val)
00195         throw InternalErr(__FILE__, __LINE__,
00196                           "The incoming pointer does not contain any data.");
00197 
00198     d_buf = *(dods_int32 *)val;
00199 
00200     return width();
00201 }
00202 
00203 unsigned int
00204 Int32::buf2val(void **val)
00205 {
00206     // Jose Garcia
00207     // The same comment justifying throwing an Error in val2buf applies here.
00208     if (!val)
00209         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00210 
00211     if (!*val)
00212         *val = new dods_int32;
00213 
00214     *(dods_int32 *)*val = d_buf;
00215 
00216     return width();
00217 }
00218 
00219 dods_int32
00220 Int32::value() const
00221 {
00222     return d_buf;
00223 }
00224 
00225 bool
00226 Int32::set_value(dods_int32 i)
00227 {
00228     d_buf = i;
00229     set_read_p(true);
00230 
00231     return true;
00232 }
00233 
00234 void
00235 Int32::print_val(FILE *out, string space, bool print_decl_p)
00236 {
00237     ostringstream oss;
00238     print_val(oss, space, print_decl_p);
00239     fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
00240 }
00241 
00242 void
00243 Int32::print_val(ostream &out, string space, bool print_decl_p)
00244 {
00245     if (print_decl_p) {
00246         print_decl(out, space, false);
00247         out << " = " << (int)d_buf << ";\n" ;
00248     }
00249     else
00250         out << (int)d_buf ;
00251 }
00252 
00253 bool
00254 Int32::ops(BaseType *b, int op)
00255 {
00256     if (!read_p() && !read()) {
00257         // Jose Garcia
00258         // Since the read method is virtual and implemented outside
00259         // libdap++ if we cannot read the data that is the problem
00260         // of the user or of whoever wrote the surrogate library
00261         // implemeting read therefore it is an internal error.
00262         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00263     }
00264 
00265     // Extract the second arg's value.
00266     if (!b->read_p() && !b->read()) {
00267         // Jose Garcia
00268         // Since the read method is virtual and implemented outside
00269         // libdap++ if we cannot read the data that is the problem
00270         // of the user or of whoever wrote the surrogate library
00271         // implemeting read therefore it is an internal error.
00272         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00273     }
00274 
00275     switch (b->type()) {
00276         case dods_int8_c:
00277             return Cmp<dods_int32, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00278         case dods_byte_c:
00279             return SUCmp<dods_int32, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00280         case dods_int16_c:
00281             return Cmp<dods_int32, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00282         case dods_uint16_c:
00283             return SUCmp<dods_int32, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00284         case dods_int32_c:
00285             return Cmp<dods_int32, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00286         case dods_uint32_c:
00287             return SUCmp<dods_int32, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00288         case dods_int64_c:
00289             return Cmp<dods_int32, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00290         case dods_uint64_c:
00291             return SUCmp<dods_int32, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00292         case dods_float32_c:
00293             return Cmp<dods_int32, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00294         case dods_float64_c:
00295             return Cmp<dods_int32, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00296         default:
00297             return false;
00298     }
00299 }
00300 
00309 void
00310 Int32::dump(ostream &strm) const
00311 {
00312     strm << DapIndent::LMarg << "Int32::dump - ("
00313     << (void *)this << ")" << endl ;
00314     DapIndent::Indent() ;
00315     BaseType::dump(strm) ;
00316     strm << DapIndent::LMarg << "value: " << d_buf << endl ;
00317     DapIndent::UnIndent() ;
00318 }
00319 
00320 } // namespace libdap
00321