libdap  Updated for version 3.17.0
UInt32.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 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 using std::cerr;
00070 using std::endl;
00071 
00072 namespace libdap {
00073 
00079 UInt32::UInt32(const string &n) : BaseType(n, dods_uint32_c), d_buf(0)
00080 {}
00081 
00089 UInt32::UInt32(const string &n, const string &d) : BaseType(n, d, dods_uint32_c), d_buf(0)
00090 {}
00091 
00092 UInt32::UInt32(const UInt32 &copy_from) : BaseType(copy_from)
00093 {
00094     d_buf = copy_from.d_buf;
00095 }
00096 
00097 BaseType *
00098 UInt32::ptr_duplicate()
00099 {
00100     return new UInt32(*this);
00101 }
00102 
00103 UInt32 &
00104 UInt32::operator=(const UInt32 &rhs)
00105 {
00106     if (this == &rhs)
00107         return *this;
00108 
00109     dynamic_cast<BaseType &>(*this) = rhs;
00110 
00111     d_buf = rhs.d_buf;
00112 
00113     return *this;
00114 }
00115 
00116 unsigned int
00117 UInt32::width(bool) const
00118 {
00119     return sizeof(dods_uint32);
00120 }
00121 
00122 bool
00123 UInt32::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
00124 {
00125 #if USE_LOCAL_TIMEOUT_SCHEME
00126     dds.timeout_on();
00127 #endif
00128     if (!read_p())
00129         read();  // read() throws Error and InternalErr
00130 
00131     if (ce_eval && !eval.eval_selection(dds, dataset()))
00132         return true;
00133 #if USE_LOCAL_TIMEOUT_SCHEME
00134     dds.timeout_off();
00135 #endif
00136     m.put_uint32( d_buf ) ;
00137 
00138     return true;
00139 }
00140 
00141 bool
00142 UInt32::deserialize(UnMarshaller &um, DDS *, bool)
00143 {
00144     um.get_uint32( d_buf ) ;
00145 
00146     return false;
00147 }
00148 
00149 void
00150 UInt32::compute_checksum(Crc32 &checksum)
00151 {
00152         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00153 }
00154 
00163 void
00164 UInt32::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00165 {
00166     if (!read_p())
00167         read();          // read() throws Error
00168 
00169     m.put_uint32( d_buf ) ;
00170 }
00171 
00172 void
00173 UInt32::deserialize(D4StreamUnMarshaller &um, DMR &)
00174 {
00175     um.get_uint32( d_buf ) ;
00176 }
00177 
00178 unsigned int
00179 UInt32::val2buf(void *val, bool)
00180 {
00181 
00182     // Jose Garcia
00183     // This method is public therefore and I believe it has being designed
00184     // to be use by read which must be implemented on the surrogated library,
00185     // thus if the pointer val is NULL, is an Internal Error.
00186     if (!val)
00187         throw InternalErr(__FILE__, __LINE__,
00188                           "The incoming pointer does not contain any data.");
00189 
00190     d_buf = *(dods_uint32 *)val;
00191 
00192     return width();
00193 }
00194 
00195 unsigned int
00196 UInt32::buf2val(void **val)
00197 {
00198     // Jose Garcia
00199     // The same comment justifying throwing an Error in val2buf applies here.
00200     if (!val)
00201         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00202 
00203     if (!*val)
00204         *val = new dods_uint32;
00205 
00206     *(dods_uint32 *)*val = d_buf;
00207 
00208     return width();
00209 }
00210 
00211 dods_uint32
00212 UInt32::value() const
00213 {
00214     return d_buf;
00215 }
00216 
00217 bool
00218 UInt32::set_value(dods_uint32 i)
00219 {
00220     d_buf = i;
00221     set_read_p(true);
00222 
00223     return true;
00224 }
00225 
00226 void
00227 UInt32::print_val(FILE *out, string space, bool print_decl_p)
00228 {
00229     ostringstream oss;
00230     print_val(oss, space, print_decl_p);
00231     fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
00232 }
00233 
00234 void
00235 UInt32::print_val(ostream &out, string space, bool print_decl_p)
00236 {
00237     if (print_decl_p) {
00238         print_decl(out, space, false);
00239         out << " = " << (unsigned int)d_buf << ";\n" ;
00240     }
00241     else
00242         out << (unsigned int)d_buf ;
00243 }
00244 
00245 bool
00246 UInt32::ops(BaseType *b, int op)
00247 {
00248     // Extract the Byte arg's value.
00249     if (!read_p() && !read()) {
00250         // Jose Garcia
00251         // Since the read method is virtual and implemented outside
00252         // libdap++ if we cannot read the data that is the problem
00253         // of the user or of whoever wrote the surrogate library
00254         // implemeting read therefore it is an internal error.
00255         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00256     }
00257 
00258     // Extract the second arg's value.
00259     if (!b || !(b->read_p() || b->read())) {
00260         // Jose Garcia
00261         // Since the read method is virtual and implemented outside
00262         // libdap++ if we cannot read the data that is the problem
00263         // of the user or of whoever wrote the surrogate library
00264         // implemeting read therefore it is an internal error.
00265         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00266     }
00267 
00268     switch (b->type()) {
00269         case dods_int8_c:
00270             return USCmp<dods_uint32, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00271         case dods_byte_c:
00272             return Cmp<dods_uint32, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00273         case dods_int16_c:
00274             return USCmp<dods_uint32, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00275         case dods_uint16_c:
00276             return Cmp<dods_uint32, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00277         case dods_int32_c:
00278             return USCmp<dods_uint32, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00279         case dods_uint32_c:
00280             return Cmp<dods_uint32, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00281         case dods_int64_c:
00282             return USCmp<dods_uint32, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00283         case dods_uint64_c:
00284             return Cmp<dods_uint32, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00285         case dods_float32_c:
00286             return USCmp<dods_uint32, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00287         case dods_float64_c:
00288             return USCmp<dods_uint32, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00289         default:
00290             return false;
00291     }
00292 }
00293 
00302 void
00303 UInt32::dump(ostream &strm) const
00304 {
00305     strm << DapIndent::LMarg << "UInt32::dump - ("
00306     << (void *)this << ")" << endl ;
00307     DapIndent::Indent() ;
00308     BaseType::dump(strm) ;
00309     strm << DapIndent::LMarg << "value: " << d_buf << endl ;
00310     DapIndent::UnIndent() ;
00311 }
00312 
00313 } // namespace libdap
00314