BaseType.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 BaseType.
00033 //
00034 // jhrg 9/6/94
00035 
00036 #include "config.h"
00037 
00038 #include <stdio.h>  // for stdin and stdout
00039 
00040 #include <sstream>
00041 #include <string>
00042 
00043 // #define DODS_DEBUG
00044 
00045 #include "BaseType.h"
00046 #include "InternalErr.h"
00047 
00048 #include "util.h"
00049 #include "escaping.h"
00050 
00051 #include "debug.h"
00052 
00053 using namespace std;
00054 
00055 // Protected copy mfunc
00056 
00063 void
00064 BaseType::_duplicate(const BaseType &bt)
00065 {
00066     _name = bt._name;
00067     _type = bt._type;
00068     _read_p = bt._read_p; // added, reza
00069     _send_p = bt._send_p; // added, reza
00070     d_in_selection = bt.d_in_selection;
00071     _synthesized_p = bt._synthesized_p; // 5/11/2001 jhrg
00072     _xdr_coder = bt._xdr_coder; // just copy this function pointer
00073 
00074     d_parent = bt.d_parent; // copy pointers 6/4/2001 jhrg
00075 
00076     d_attr = bt.d_attr;  // Deep copy.
00077 }
00078 
00079 // Public mfuncs
00080 
00101 BaseType::BaseType(const string &n, const Type &t, xdrproc_t xdr)
00102         : _name(n), _type(t), _xdr_coder(xdr), _read_p(false), _send_p(false),
00103         d_in_selection(false), _synthesized_p(false), d_parent(0)
00104 {}
00105 
00107 BaseType::BaseType(const BaseType &copy_from) : DapObj()
00108 {
00109     _duplicate(copy_from);
00110 }
00111 
00112 BaseType::~BaseType()
00113 {
00114     DBG(cerr << "Entering ~BaseType (" << this << ")" << endl);
00115     DBG(cerr << "Exiting ~BaseType" << endl);
00116 }
00117 
00118 BaseType &
00119 BaseType::operator=(const BaseType &rhs)
00120 {
00121     if (this == &rhs)
00122         return *this;
00123 
00124     _duplicate(rhs);
00125 
00126     return *this;
00127 }
00128 
00133 string
00134 BaseType::toString()
00135 {
00136     ostringstream oss;
00137     oss << "BaseType (" << this << "):" << endl
00138     << "          _name: " << _name << endl
00139     << "          _type: " << type_name() << endl
00140     << "          _read_p: " << _read_p << endl
00141     << "          _send_p: " << _send_p << endl
00142     << "          _synthesized_p: " << _synthesized_p << endl
00143     << "          d_parent: " << d_parent << endl
00144     << "          d_attr: " << hex << &d_attr << dec << endl;
00145 
00146     return oss.str();
00147 }
00148 
00157 void
00158 BaseType::dump(ostream &strm) const
00159 {
00160     strm << DapIndent::LMarg << "BaseType::dump - ("
00161     << (void *)this << ")" << endl ;
00162     DapIndent::Indent() ;
00163 
00164     strm << DapIndent::LMarg << "name: " << _name << endl ;
00165     strm << DapIndent::LMarg << "type: " << type_name() << endl ;
00166     strm << DapIndent::LMarg << "read_p: " << _read_p << endl ;
00167     strm << DapIndent::LMarg << "send_p: " << _send_p << endl ;
00168     strm << DapIndent::LMarg << "synthesized_p: " << _synthesized_p << endl ;
00169     strm << DapIndent::LMarg << "parent: " << (void *)d_parent << endl ;
00170     strm << DapIndent::LMarg << "attributes: " << endl ;
00171     DapIndent::Indent() ;
00172     d_attr.dump(strm) ;
00173     DapIndent::UnIndent() ;
00174 
00175     DapIndent::UnIndent() ;
00176 }
00177 
00180 string
00181 BaseType::name() const
00182 {
00183     return _name;
00184 }
00185 
00187 void
00188 BaseType::set_name(const string &n)
00189 {
00190     string name = n;
00191     _name = www2id(name); // www2id writes into its param.
00192 }
00193 
00195 Type
00196 BaseType::type() const
00197 {
00198     return _type;
00199 }
00200 
00202 void
00203 BaseType::set_type(const Type &t)
00204 {
00205     _type = t;
00206 }
00207 
00209 string
00210 BaseType::type_name() const
00211 {
00212     switch (_type) {
00213     case dods_null_c:
00214         return string("Null");
00215     case dods_byte_c:
00216         return string("Byte");
00217     case dods_int16_c:
00218         return string("Int16");
00219     case dods_uint16_c:
00220         return string("UInt16");
00221     case dods_int32_c:
00222         return string("Int32");
00223     case dods_uint32_c:
00224         return string("UInt32");
00225     case dods_float32_c:
00226         return string("Float32");
00227     case dods_float64_c:
00228         return string("Float64");
00229     case dods_str_c:
00230         return string("String");
00231     case dods_url_c:
00232         return string("Url");
00233     case dods_array_c:
00234         return string("Array");
00235     case dods_structure_c:
00236         return string("Structure");
00237     case dods_sequence_c:
00238         return string("Sequence");
00239     case dods_grid_c:
00240         return string("Grid");
00241     default:
00242         cerr << "BaseType::type_name: Undefined type" << endl;
00243         return string("");
00244     }
00245 }
00246 
00249 bool
00250 BaseType::is_simple_type()
00251 {
00252     switch (type()) {
00253     case dods_null_c:
00254     case dods_byte_c:
00255     case dods_int16_c:
00256     case dods_uint16_c:
00257     case dods_int32_c:
00258     case dods_uint32_c:
00259     case dods_float32_c:
00260     case dods_float64_c:
00261     case dods_str_c:
00262     case dods_url_c:
00263         return true;
00264 
00265     case dods_array_c:
00266     case dods_structure_c:
00267     case dods_sequence_c:
00268     case dods_grid_c:
00269         return false;
00270     }
00271 
00272     return false;
00273 }
00274 
00276 bool
00277 BaseType::is_vector_type()
00278 {
00279     switch (type()) {
00280     case dods_null_c:
00281     case dods_byte_c:
00282     case dods_int16_c:
00283     case dods_uint16_c:
00284     case dods_int32_c:
00285     case dods_uint32_c:
00286     case dods_float32_c:
00287     case dods_float64_c:
00288     case dods_str_c:
00289     case dods_url_c:
00290         return false;
00291 
00292     case dods_array_c:
00293         return true;
00294 
00295     case dods_structure_c:
00296     case dods_sequence_c:
00297     case dods_grid_c:
00298         return false;
00299     }
00300 
00301     return false;
00302 }
00303 
00306 bool
00307 BaseType::is_constructor_type()
00308 {
00309     switch (type()) {
00310     case dods_null_c:
00311     case dods_byte_c:
00312     case dods_int16_c:
00313     case dods_uint16_c:
00314     case dods_int32_c:
00315     case dods_uint32_c:
00316     case dods_float32_c:
00317     case dods_float64_c:
00318     case dods_str_c:
00319     case dods_url_c:
00320     case dods_array_c:
00321         return false;
00322 
00323     case dods_structure_c:
00324     case dods_sequence_c:
00325     case dods_grid_c:
00326         return true;
00327     }
00328 
00329     return false;
00330 }
00331 
00357 int
00358 BaseType::element_count(bool)
00359 {
00360     return 1;
00361 }
00362 
00366 bool
00367 BaseType::synthesized_p()
00368 {
00369     return _synthesized_p;
00370 }
00371 
00377 void
00378 BaseType::set_synthesized_p(bool state)
00379 {
00380     _synthesized_p = state;
00381 }
00382 
00383 // Return the state of _read_p (true if the value of the variable has been
00384 // read (and is in memory) false otherwise).
00385 
00394 bool
00395 BaseType::read_p()
00396 {
00397     return _read_p;
00398 }
00399 
00433 void
00434 BaseType::set_read_p(bool state)
00435 {
00436     if (! _synthesized_p) {
00437         DBG(cerr << "Changing read_p state of " << name() << endl);
00438         _read_p = state;
00439     }
00440 }
00441 
00452 bool
00453 BaseType::send_p()
00454 {
00455     return _send_p;
00456 }
00457 
00465 void
00466 BaseType::set_send_p(bool state)
00467 {
00468     DBG(cerr << "Calling BaseType::set_send_p() for: " << this->name()
00469         << endl);
00470     _send_p = state;
00471 }
00472 
00473 
00479 AttrTable &
00480 BaseType::get_attr_table()
00481 {
00482     return d_attr;
00483 }
00484 
00487 void
00488 BaseType::set_attr_table(const AttrTable &at)
00489 {
00490     d_attr = at;
00491 }
00492 
00504 bool
00505 BaseType::is_in_selection()
00506 {
00507     return d_in_selection;
00508 }
00509 
00519 void
00520 BaseType::set_in_selection(bool state)
00521 {
00522     d_in_selection = state;
00523 }
00524 
00525 // Protected method.
00532 void
00533 BaseType::set_parent(BaseType *parent)
00534 {
00535     if (!dynamic_cast<Constructor *>(parent)
00536         && !dynamic_cast<Vector *>(parent))
00537         throw InternalErr("Call to set_parent with incorrect variable type.");
00538 
00539     d_parent = parent;
00540 }
00541 
00542 // Public method.
00543 
00549 BaseType *
00550 BaseType::get_parent()
00551 {
00552     return d_parent;
00553 }
00554 
00555 // Documented in the header file.
00556 BaseType *
00557 BaseType::var(const string &/*name*/, bool /*exact_match*/, btp_stack */*s*/)
00558 {
00559     return static_cast<BaseType *>(0);
00560 }
00561 
00578 BaseType *
00579 BaseType::var(const string &, btp_stack &)
00580 {
00581     return static_cast<BaseType *>(0);
00582 }
00583 
00613 void
00614 BaseType::add_var(BaseType *, Part)
00615 {
00616     throw InternalErr(__FILE__, __LINE__, "BaseType::add_var unimplemented");
00617 }
00618 
00689 bool
00690 BaseType::read(const string &)
00691 {
00692     if (_read_p)
00693         return false;
00694 
00695     throw InternalErr("Unimplemented BaseType::read() method called.");
00696 }
00697 
00712 xdrproc_t
00713 BaseType::xdr_coder()
00714 {
00715     return _xdr_coder;
00716 }
00717 
00718 
00761 void
00762 BaseType::print_decl(FILE *out, string space, bool print_semi,
00763                      bool constraint_info, bool constrained)
00764 {
00765     // if printing the constrained declaration, exit if this variable was not
00766     // selected.
00767     if (constrained && !send_p())
00768         return;
00769 
00770     fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(),
00771             id2www(_name).c_str()) ;
00772 
00773     if (constraint_info) {
00774         if (send_p())
00775             fprintf(stdout, ": Send True") ;
00776         else
00777             fprintf(stdout, ": Send False") ;
00778     }
00779 
00780     if (print_semi)
00781         fprintf(out, ";\n") ;
00782 }
00783 
00790 void
00791 BaseType::print_xml(FILE *out, string space, bool constrained)
00792 {
00793     if (constrained && !send_p())
00794         return;
00795 
00796     fprintf(out, "%s<%s", space.c_str(), type_name().c_str());
00797     if (!_name.empty())
00798         fprintf(out, " name=\"%s\"", id2xml(_name).c_str());
00799 
00800     if (get_attr_table().get_size() > 0) {
00801         fprintf(out, ">\n"); // close the variable's tag
00802         get_attr_table().print_xml(out, space + "    ", constrained);
00803         // After attributes, print closing tag
00804         fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str());
00805     }
00806     else {
00807         fprintf(out, "/>\n"); // no attributes; just close tag.
00808     }
00809 }
00810 
00811 // Compares the object's current state with the semantics of a particular
00812 // type. This will typically be defined in ctor classes (which have
00813 // complicated semantics). For BaseType, an object is semantically correct if
00814 // it has both a non-null name and type.
00815 //
00816 // NB: This is not the same as an invariant -- during the parse objects exist
00817 // but have no name. Also, the bool ALL defaults to false for BaseType. It is
00818 // used by children of CtorType.
00819 //
00820 // Returns: true if the object is semantically correct, false otherwise.
00821 
00850 bool
00851 BaseType::check_semantics(string &msg, bool)
00852 {
00853     bool sem = (_type != dods_null_c && _name.length());
00854 
00855     if (!sem)
00856         msg = "Every variable must have both a name and a type\n";
00857 
00858     return sem;
00859 }
00860 
00897 bool
00898 BaseType::ops(BaseType *, int, const string &)
00899 {
00900     // Even though ops is a public method, it can never be called because
00901     // they will never have a BaseType object since this class is abstract,
00902     // however any of the child classes could by mistake call BaseType::ops
00903     // so this is an internal error. Jose Garcia
00904     throw InternalErr(__FILE__, __LINE__, "Unimplemented operator.");
00905 }

Generated on Wed Jun 27 12:56:38 2007 for libdap++ by  doxygen 1.4.7