libdap  Updated for version 3.17.0
Int8.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) 2012 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 #include "config.h"
00027 
00028 #include <cassert>
00029 #include <sstream>
00030 
00031 #include "Byte.h"           // synonymous with UInt8 and Char
00032 #include "Int8.h"
00033 #include "Int16.h"
00034 #include "UInt16.h"
00035 #include "Int32.h"
00036 #include "UInt32.h"
00037 #include "Int64.h"
00038 #include "UInt64.h"
00039 #include "Float32.h"
00040 #include "Float64.h"
00041 #include "Str.h"
00042 #include "Url.h"
00043 
00044 #include "D4StreamMarshaller.h"
00045 #include "D4StreamUnMarshaller.h"
00046 
00047 #include "DDS.h"
00048 #include "util.h"
00049 #include "parser.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 namespace libdap {
00059 
00067 Int8::Int8(const string &n) : BaseType(n, dods_int8_c, true /*is_dap4*/), d_buf(0)
00068 {}
00069 
00082 Int8::Int8(const string &n, const string &d) : BaseType(n, d, dods_int8_c, true /*is_dap4*/), d_buf(0)
00083 {}
00084 
00085 Int8::Int8(const Int8 &copy_from) : BaseType(copy_from)
00086 {
00087     d_buf = copy_from.d_buf;
00088 }
00089 
00090 BaseType *
00091 Int8::ptr_duplicate()
00092 {
00093     return new Int8(*this);
00094 }
00095 
00096 Int8 &
00097 Int8::operator=(const Int8 &rhs)
00098 {
00099     if (this == &rhs)
00100         return *this;
00101 
00102     static_cast<BaseType &>(*this) = rhs;
00103 
00104     d_buf = rhs.d_buf;
00105 
00106     return *this;
00107 }
00108 
00109 unsigned int
00110 Int8::width(bool) const
00111 {
00112     return sizeof(dods_int8);
00113 }
00114 
00115 void
00116 Int8::compute_checksum(Crc32 &checksum)
00117 {
00118         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00119 }
00120 
00129 void
00130 Int8::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00131 {
00132     if (!read_p())
00133         read();          // read() throws Error
00134 
00135     m.put_int8( d_buf ) ;
00136 }
00137 
00138 void
00139 Int8::deserialize(D4StreamUnMarshaller &um, DMR &)
00140 {
00141     um.get_int8( d_buf ) ;
00142 }
00143 
00144 dods_int8
00145 Int8::value() const
00146 {
00147     return d_buf;
00148 }
00149 
00150 bool
00151 Int8::set_value(dods_int8 i)
00152 {
00153     d_buf = i;
00154     set_read_p(true);
00155 
00156     return true;
00157 }
00158 
00159 void Int8::print_val(ostream &out, string space, bool print_decl_p)
00160 {
00161         if (print_decl_p) {
00162                 print_decl(out, space, false);
00163                 out << " = " << d_buf << ";\n";
00164         }
00165         else
00166                 out << (int)d_buf;
00167 }
00168 
00169 bool
00170 Int8::ops(BaseType *b, int op)
00171 {
00172     // Get the arg's value.
00173     if (!read_p() && !read())
00174         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00175 
00176     // Get the second arg's value.
00177     if (!b->read_p() && !b->read())
00178         throw InternalErr(__FILE__, __LINE__, "This value not read!");
00179 
00180     switch (b->type()) {
00181         case dods_int8_c:
00182             return Cmp<dods_int8, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00183         case dods_byte_c:
00184             return SUCmp<dods_int8, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00185         case dods_int16_c:
00186             return Cmp<dods_int8, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00187         case dods_uint16_c:
00188             return SUCmp<dods_int8, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00189         case dods_int32_c:
00190             return Cmp<dods_int8, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00191         case dods_uint32_c:
00192             return SUCmp<dods_int8, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00193         case dods_int64_c:
00194             return Cmp<dods_int8, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00195         case dods_uint64_c:
00196             return SUCmp<dods_int8, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00197         case dods_float32_c:
00198             return Cmp<dods_int8, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00199         case dods_float64_c:
00200             return Cmp<dods_int8, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00201         default:
00202             return false;
00203     }
00204 #if 0
00205     switch (b->type()) {
00206         case dods_byte_c:
00207             return rops<dods_int8, dods_byte, SUCmp<dods_int8, dods_byte> >(d_buf, static_cast<Byte*>(b)->value());
00208         case dods_int16_c:
00209             return rops<dods_int8, dods_int16, Cmp<dods_int8, dods_int16> >(d_buf, static_cast<Int16 *>(b)->value());
00210         case dods_uint16_c:
00211             return rops<dods_int8, dods_uint16, SUCmp<dods_int8, dods_uint16> >(d_buf, static_cast<UInt16 *>(b)->value());
00212         case dods_int32_c:
00213             return rops<dods_int8, dods_int32, Cmp<dods_int8, dods_int32> >(d_buf, static_cast<Int32 *>(b)->value());
00214         case dods_uint32_c:
00215             return rops<dods_int8, dods_uint32, SUCmp<dods_int8, dods_uint32> >(d_buf, static_cast<UInt32 *>(b)->value());
00216         case dods_int64_c:
00217             return rops<dods_int8, dods_int64, Cmp<dods_int8, dods_int64> >(d_buf, static_cast<Int64 *>(b)->value());
00218         case dods_uint64_c:
00219             return rops<dods_int8, dods_uint64, Cmp<dods_int8, dods_uint64> >(d_buf, static_cast<UInt64 *>(b)->value());
00220         case dods_float32_c:
00221             return rops<dods_int8, dods_float32, Cmp<dods_int64, dods_float32> >(d_buf, static_cast<Float32 *>(b)->value());
00222         case dods_float64_c:
00223             return rops<dods_int8, dods_float64, Cmp<dods_int64, dods_float64> >(d_buf, static_cast<Float64 *>(b)->value());
00224         default:
00225             return false;
00226     }
00227 #endif
00228 }
00229 
00238 void
00239 Int8::dump(ostream &strm) const
00240 {
00241     strm << DapIndent::LMarg << "Int8::dump - ("
00242     << (void *)this << ")" << endl ;
00243     DapIndent::Indent() ;
00244     BaseType::dump(strm) ;
00245     strm << DapIndent::LMarg << "value: " << d_buf << endl ;
00246     DapIndent::UnIndent() ;
00247 }
00248 
00249 } // namespace libdap
00250