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

Generated on Fri Feb 8 05:08:57 2008 for libdap++ by  doxygen 1.5.4