Byte.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 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 Byte.
00033 //
00034 // jhrg 9/7/94
00035 
00036 
00037 #include "config.h"
00038 
00039 static char rcsid[] not_used =
00040     { "$Id: Byte.cc 17002 2007-08-27 19:16:51Z pwest $"
00041     };
00042 
00043 #include <stdlib.h>
00044 
00045 #include "Byte.h"
00046 //#include "Int16.h"
00047 #include "DDS.h"
00048 #include "Operators.h"
00049 
00050 #include "util.h"
00051 #include "parser.h"
00052 #include "dods-limits.h"
00053 #include "InternalErr.h"
00054 
00055 
00056 using std::cerr;
00057 using std::endl;
00058 
00069 Byte::Byte(const string & n): BaseType(n, dods_byte_c)
00070 {}
00071 
00072 Byte::Byte(const Byte & copy_from): BaseType(copy_from)
00073 {
00074     _buf = copy_from._buf;
00075 }
00076 
00077 BaseType *Byte::ptr_duplicate()
00078 {
00079     return new Byte(*this);
00080 }
00081 
00082 Byte & Byte::operator=(const Byte & rhs)
00083 {
00084     if (this == &rhs)
00085         return *this;
00086 
00087     dynamic_cast < BaseType & >(*this) = rhs;
00088 
00089     _buf = rhs._buf;
00090 
00091     return *this;
00092 }
00093 
00094 unsigned int Byte::width()
00095 {
00096     return sizeof(dods_byte);
00097 }
00098 
00109 bool Byte::serialize(const string & dataset, ConstraintEvaluator & eval,
00110                      DDS & dds, Marshaller &m, bool ce_eval)
00111 {
00112     dds.timeout_on();
00113 
00114     if (!read_p())
00115         read(dataset);          // read() throws Error and InternalErr
00116 
00117 #if EVAL
00118     if (ce_eval && !eval.eval_selection(dds, dataset))
00119         return true;
00120 #endif
00121 
00122     dds.timeout_off();
00123 
00124     m.put_byte( _buf ) ;
00125 
00126     return true;
00127 }
00128 
00132 bool Byte::deserialize(UnMarshaller &um, DDS *, bool)
00133 {
00134     um.get_byte( _buf ) ;
00135 
00136     return false;
00137 }
00138 
00144 unsigned int Byte::val2buf(void *val, bool)
00145 {
00146     // Jose Garcia
00147     // This method is public therefore and I believe it has being designed
00148     // to be use by read which must be implemented on the surrogate library,
00149     // thus if the pointer val is NULL, is an Internal Error.
00150     if (!val)
00151         throw InternalErr("the incoming pointer does not contain any data.");
00152 
00153     _buf = *(dods_byte *) val;
00154 
00155     return width();
00156 }
00157 
00158 unsigned int Byte::buf2val(void **val)
00159 {
00160     // Jose Garcia
00161     // The same comment justifying throwing an Error in val2buf applies here.
00162     if (!val)
00163         throw InternalErr("NULL pointer");
00164 
00165     if (!*val)
00166         *val = new dods_byte;
00167 
00168     *(dods_byte *) * val = _buf;
00169 
00170     return width();
00171 }
00172 
00177 bool Byte::set_value(dods_byte value)
00178 {
00179     _buf = value;
00180     set_read_p(true);
00181 
00182     return true;
00183 }
00184 
00187 dods_byte Byte::value() const
00188 {
00189     return _buf;
00190 }
00191 
00192 void Byte::print_val(FILE * out, string space, bool print_decl_p)
00193 {
00194     if (print_decl_p) {
00195         print_decl(out, space, false);
00196         fprintf(out, " = %d;\n", (int) _buf);
00197     }
00198     else
00199         fprintf(out, "%d", (int) _buf);
00200 }
00201 
00202 void Byte::print_val(ostream &out, string space, bool print_decl_p)
00203 {
00204     if (print_decl_p) {
00205         print_decl(out, space, false);
00206         out << " = " << (int)_buf << ";\n" ;
00207     }
00208     else
00209         out << (int)_buf ;
00210 }
00211 
00212 bool Byte::ops(BaseType * b, int op, const string & dataset)
00213 {
00214 
00215     // Extract the Byte arg's value.
00216     if (!read_p() && !read(dataset)) {
00217         cerr << "This value not read!" << endl;
00218         // Jose Garcia
00219         // Since the read method is virtual and implemented outside
00220         // libdap++ if we can not read the data that is the problem
00221         // of the user or of whoever wrote the surrogate library
00222         // implementing read therefore it is an internal error.
00223         throw InternalErr("This value not read!");
00224     }
00225     // Extract the second arg's value.
00226     if (!b->read_p() && !b->read(dataset)) {
00227         cerr << "This value not read!" << endl;
00228         // Jose Garcia
00229         // Since the read method is virtual and implemented outside
00230         // libdap++ if we can not read the data that is the problem
00231         // of the user or of whoever wrote the surrogate library
00232         // implementing read therefore it is an internal error.
00233         throw InternalErr("This value not read!");
00234     }
00235 
00236     switch (b->type()) {
00237     case dods_byte_c:
00238         return rops < dods_byte, dods_byte, Cmp < dods_byte, dods_byte > >
00239                (_buf, dynamic_cast < Byte * >(b)->_buf, op);
00240     case dods_int16_c:
00241         return rops < dods_byte, dods_int16, USCmp < dods_byte,
00242                dods_int16 > > (_buf, dynamic_cast < Int16 * >(b)->_buf, op);
00243     case dods_uint16_c:
00244         return rops < dods_byte, dods_uint16, Cmp < dods_byte,
00245                dods_uint16 > > (_buf, dynamic_cast < UInt16 * >(b)->_buf, op);
00246     case dods_int32_c:
00247         return rops < dods_byte, dods_int32, USCmp < dods_byte,
00248                dods_int32 > > (_buf, dynamic_cast < Int32 * >(b)->_buf, op);
00249     case dods_uint32_c:
00250         return rops < dods_byte, dods_uint32, Cmp < dods_byte,
00251                dods_uint32 > > (_buf, dynamic_cast < UInt32 * >(b)->_buf, op);
00252     case dods_float32_c:
00253         return rops < dods_byte, dods_float32, Cmp < dods_byte,
00254                dods_float32 > > (_buf, dynamic_cast < Float32 * >(b)->_buf,
00255                                  op);
00256     case dods_float64_c:
00257         return rops < dods_byte, dods_float64, Cmp < dods_byte,
00258                dods_float64 > > (_buf, dynamic_cast < Float64 * >(b)->_buf,
00259                                  op);
00260     default:
00261         return false;
00262     }
00263 }
00264 
00273 void Byte::dump(ostream & strm) const
00274 {
00275     strm << DapIndent::LMarg << "Byte::dump - ("
00276     << (void *) this << ")" << endl;
00277     DapIndent::Indent();
00278     BaseType::dump(strm);
00279     strm << DapIndent::LMarg << "value: " << _buf << endl;
00280     DapIndent::UnIndent();
00281 }

Generated on Wed Nov 28 16:23:14 2007 for libdap++ by  doxygen 1.4.7