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 <cstdio>  // 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 namespace libdap {
00056 
00057 // Protected copy mfunc
00058 
00065 void
00066 BaseType::_duplicate(const BaseType &bt)
00067 {
00068     _name = bt._name;
00069     _type = bt._type;
00070     _read_p = bt._read_p; // added, reza
00071     _send_p = bt._send_p; // added, reza
00072     d_in_selection = bt.d_in_selection;
00073     _synthesized_p = bt._synthesized_p; // 5/11/2001 jhrg
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 
00093 BaseType::BaseType(const string &n, const Type &t)
00094         : _name(n), _type(t), _read_p(false), _send_p(false),
00095         d_in_selection(false), _synthesized_p(false), d_parent(0)
00096 {}
00097 
00099 BaseType::BaseType(const BaseType &copy_from) : DapObj()
00100 {
00101     _duplicate(copy_from);
00102 }
00103 
00104 BaseType::~BaseType()
00105 {
00106     DBG(cerr << "Entering ~BaseType (" << this << ")" << endl);
00107     DBG(cerr << "Exiting ~BaseType" << endl);
00108 }
00109 
00110 BaseType &
00111 BaseType::operator=(const BaseType &rhs)
00112 {
00113     if (this == &rhs)
00114         return *this;
00115 
00116     _duplicate(rhs);
00117 
00118     return *this;
00119 }
00120 
00125 string
00126 BaseType::toString()
00127 {
00128     ostringstream oss;
00129     oss << "BaseType (" << this << "):" << endl
00130     << "          _name: " << _name << endl
00131     << "          _type: " << type_name() << endl
00132     << "          _read_p: " << _read_p << endl
00133     << "          _send_p: " << _send_p << endl
00134     << "          _synthesized_p: " << _synthesized_p << endl
00135     << "          d_parent: " << d_parent << endl
00136     << "          d_attr: " << hex << &d_attr << dec << endl;
00137 
00138     return oss.str();
00139 }
00140 
00149 void
00150 BaseType::dump(ostream &strm) const
00151 {
00152     strm << DapIndent::LMarg << "BaseType::dump - ("
00153     << (void *)this << ")" << endl ;
00154     DapIndent::Indent() ;
00155 
00156     strm << DapIndent::LMarg << "name: " << _name << endl ;
00157     strm << DapIndent::LMarg << "type: " << type_name() << endl ;
00158     strm << DapIndent::LMarg << "read_p: " << _read_p << endl ;
00159     strm << DapIndent::LMarg << "send_p: " << _send_p << endl ;
00160     strm << DapIndent::LMarg << "synthesized_p: " << _synthesized_p << endl ;
00161     strm << DapIndent::LMarg << "parent: " << (void *)d_parent << endl ;
00162     strm << DapIndent::LMarg << "attributes: " << endl ;
00163     DapIndent::Indent() ;
00164     d_attr.dump(strm) ;
00165     DapIndent::UnIndent() ;
00166 
00167     DapIndent::UnIndent() ;
00168 }
00169 
00172 string
00173 BaseType::name() const
00174 {
00175     return _name;
00176 }
00177 
00179 void
00180 BaseType::set_name(const string &n)
00181 {
00182     string name = n;
00183     _name = www2id(name); // www2id writes into its param.
00184 }
00185 
00187 Type
00188 BaseType::type() const
00189 {
00190     return _type;
00191 }
00192 
00194 void
00195 BaseType::set_type(const Type &t)
00196 {
00197     _type = t;
00198 }
00199 
00201 string
00202 BaseType::type_name() const
00203 {
00204     switch (_type) {
00205     case dods_null_c:
00206         return string("Null");
00207     case dods_byte_c:
00208         return string("Byte");
00209     case dods_int16_c:
00210         return string("Int16");
00211     case dods_uint16_c:
00212         return string("UInt16");
00213     case dods_int32_c:
00214         return string("Int32");
00215     case dods_uint32_c:
00216         return string("UInt32");
00217     case dods_float32_c:
00218         return string("Float32");
00219     case dods_float64_c:
00220         return string("Float64");
00221     case dods_str_c:
00222         return string("String");
00223     case dods_url_c:
00224         return string("Url");
00225     case dods_array_c:
00226         return string("Array");
00227     case dods_structure_c:
00228         return string("Structure");
00229     case dods_sequence_c:
00230         return string("Sequence");
00231     case dods_grid_c:
00232         return string("Grid");
00233     default:
00234         cerr << "BaseType::type_name: Undefined type" << endl;
00235         return string("");
00236     }
00237 }
00238 
00244 bool
00245 BaseType::is_simple_type()
00246 {
00247     switch (type()) {
00248     case dods_null_c:
00249     case dods_byte_c:
00250     case dods_int16_c:
00251     case dods_uint16_c:
00252     case dods_int32_c:
00253     case dods_uint32_c:
00254     case dods_float32_c:
00255     case dods_float64_c:
00256     case dods_str_c:
00257     case dods_url_c:
00258         return true;
00259 
00260     case dods_array_c:
00261     case dods_structure_c:
00262     case dods_sequence_c:
00263     case dods_grid_c:
00264         return false;
00265     }
00266 
00267     return false;
00268 }
00269 
00273 bool
00274 BaseType::is_vector_type()
00275 {
00276     switch (type()) {
00277     case dods_null_c:
00278     case dods_byte_c:
00279     case dods_int16_c:
00280     case dods_uint16_c:
00281     case dods_int32_c:
00282     case dods_uint32_c:
00283     case dods_float32_c:
00284     case dods_float64_c:
00285     case dods_str_c:
00286     case dods_url_c:
00287         return false;
00288 
00289     case dods_array_c:
00290         return true;
00291 
00292     case dods_structure_c:
00293     case dods_sequence_c:
00294     case dods_grid_c:
00295         return false;
00296     }
00297 
00298     return false;
00299 }
00300 
00305 bool
00306 BaseType::is_constructor_type()
00307 {
00308     switch (type()) {
00309     case dods_null_c:
00310     case dods_byte_c:
00311     case dods_int16_c:
00312     case dods_uint16_c:
00313     case dods_int32_c:
00314     case dods_uint32_c:
00315     case dods_float32_c:
00316     case dods_float64_c:
00317     case dods_str_c:
00318     case dods_url_c:
00319     case dods_array_c:
00320         return false;
00321 
00322     case dods_structure_c:
00323     case dods_sequence_c:
00324     case dods_grid_c:
00325         return true;
00326     }
00327 
00328     return false;
00329 }
00330 
00356 int
00357 BaseType::element_count(bool)
00358 {
00359     return 1;
00360 }
00361 
00365 bool
00366 BaseType::synthesized_p()
00367 {
00368     return _synthesized_p;
00369 }
00370 
00376 void
00377 BaseType::set_synthesized_p(bool state)
00378 {
00379     _synthesized_p = state;
00380 }
00381 
00382 // Return the state of _read_p (true if the value of the variable has been
00383 // read (and is in memory) false otherwise).
00384 
00393 bool
00394 BaseType::read_p()
00395 {
00396     return _read_p;
00397 }
00398 
00432 void
00433 BaseType::set_read_p(bool state)
00434 {
00435     if (! _synthesized_p) {
00436         DBG(cerr << "Changing read_p state of " << name() << endl);
00437         _read_p = state;
00438     }
00439 }
00440 
00451 bool
00452 BaseType::send_p()
00453 {
00454     return _send_p;
00455 }
00456 
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 
00698 void
00699 BaseType::intern_data(const string &dataset, ConstraintEvaluator &, DDS &dds)
00700 {
00701     dds.timeout_on();
00702 
00703     if (!read_p())
00704         read(dataset);          // read() throws Error and InternalErr
00705 
00706     dds.timeout_off();
00707 }
00708 
00751 void
00752 BaseType::print_decl(FILE *out, string space, bool print_semi,
00753                      bool constraint_info, bool constrained)
00754 {
00755     // if printing the constrained declaration, exit if this variable was not
00756     // selected.
00757     if (constrained && !send_p())
00758         return;
00759 
00760     fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(),
00761             id2www(_name).c_str()) ;
00762 
00763     if (constraint_info) {
00764         if (send_p())
00765             fprintf(out, ": Send True") ;
00766         else
00767             fprintf(out, ": Send False") ;
00768     }
00769 
00770     if (print_semi)
00771         fprintf(out, ";\n") ;
00772 }
00773 
00816 void
00817 BaseType::print_decl(ostream &out, string space, bool print_semi,
00818                      bool constraint_info, bool constrained)
00819 {
00820     // if printing the constrained declaration, exit if this variable was not
00821     // selected.
00822     if (constrained && !send_p())
00823         return;
00824 
00825     out << space << type_name() << " " << id2www(_name) ;
00826 
00827     if (constraint_info) {
00828         if (send_p())
00829             out << ": Send True" ;
00830         else
00831             out << ": Send False" ;
00832     }
00833 
00834     if (print_semi)
00835         out << ";\n" ;
00836 }
00837 
00844 void
00845 BaseType::print_xml(FILE *out, string space, bool constrained)
00846 {
00847     if (constrained && !send_p())
00848         return;
00849 
00850     fprintf(out, "%s<%s", space.c_str(), type_name().c_str());
00851     if (!_name.empty())
00852         fprintf(out, " name=\"%s\"", id2xml(_name).c_str());
00853 
00854     if (get_attr_table().get_size() > 0) {
00855         fprintf(out, ">\n"); // close the variable's tag
00856         get_attr_table().print_xml(out, space + "    ", constrained);
00857         // After attributes, print closing tag
00858         fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str());
00859     }
00860     else {
00861         fprintf(out, "/>\n"); // no attributes; just close tag.
00862     }
00863 }
00864 
00871 void
00872 BaseType::print_xml(ostream &out, string space, bool constrained)
00873 {
00874     if (constrained && !send_p())
00875         return;
00876 
00877     out << space << "<" << type_name() ;
00878     if (!_name.empty())
00879         out << " name=\"" << id2xml(_name) << "\"" ;
00880 
00881     if (get_attr_table().get_size() > 0) {
00882         out << ">\n" ;
00883         get_attr_table().print_xml(out, space + "    ", constrained);
00884         // After attributes, print closing tag
00885         out << space << "</" << type_name() << ">\n" ;
00886     }
00887     else {
00888         out << "/>\n" ;
00889     }
00890 }
00891 
00892 // Compares the object's current state with the semantics of a particular
00893 // type. This will typically be defined in ctor classes (which have
00894 // complicated semantics). For BaseType, an object is semantically correct if
00895 // it has both a non-null name and type.
00896 //
00897 // NB: This is not the same as an invariant -- during the parse objects exist
00898 // but have no name. Also, the bool ALL defaults to false for BaseType. It is
00899 // used by children of CtorType.
00900 //
00901 // Returns: true if the object is semantically correct, false otherwise.
00902 
00931 bool
00932 BaseType::check_semantics(string &msg, bool)
00933 {
00934     bool sem = (_type != dods_null_c && _name.length());
00935 
00936     if (!sem)
00937         msg = "Every variable must have both a name and a type\n";
00938 
00939     return sem;
00940 }
00941 
00978 bool
00979 BaseType::ops(BaseType *, int, const string &)
00980 {
00981     // Even though ops is a public method, it can never be called because
00982     // they will never have a BaseType object since this class is abstract,
00983     // however any of the child classes could by mistake call BaseType::ops
00984     // so this is an internal error. Jose Garcia
00985     throw InternalErr(__FILE__, __LINE__, "Unimplemented operator.");
00986 }
00987 
00988 } // namespace libdap

Generated on Wed Mar 5 15:27:10 2008 for libdap++ by  doxygen 1.5.4