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 
00073     d_parent = bt.d_parent; // copy pointers 6/4/2001 jhrg
00074 
00075     d_attr = bt.d_attr;  // Deep copy.
00076 }
00077 
00078 // Public mfuncs
00079 
00091 BaseType::BaseType(const string &n, const Type &t)
00092         : _name(n), _type(t), _read_p(false), _send_p(false),
00093         d_in_selection(false), _synthesized_p(false), d_parent(0)
00094 {}
00095 
00097 BaseType::BaseType(const BaseType &copy_from) : DapObj()
00098 {
00099     _duplicate(copy_from);
00100 }
00101 
00102 BaseType::~BaseType()
00103 {
00104     DBG(cerr << "Entering ~BaseType (" << this << ")" << endl);
00105     DBG(cerr << "Exiting ~BaseType" << endl);
00106 }
00107 
00108 BaseType &
00109 BaseType::operator=(const BaseType &rhs)
00110 {
00111     if (this == &rhs)
00112         return *this;
00113 
00114     _duplicate(rhs);
00115 
00116     return *this;
00117 }
00118 
00123 string
00124 BaseType::toString()
00125 {
00126     ostringstream oss;
00127     oss << "BaseType (" << this << "):" << endl
00128     << "          _name: " << _name << endl
00129     << "          _type: " << type_name() << endl
00130     << "          _read_p: " << _read_p << endl
00131     << "          _send_p: " << _send_p << endl
00132     << "          _synthesized_p: " << _synthesized_p << endl
00133     << "          d_parent: " << d_parent << endl
00134     << "          d_attr: " << hex << &d_attr << dec << endl;
00135 
00136     return oss.str();
00137 }
00138 
00147 void
00148 BaseType::dump(ostream &strm) const
00149 {
00150     strm << DapIndent::LMarg << "BaseType::dump - ("
00151     << (void *)this << ")" << endl ;
00152     DapIndent::Indent() ;
00153 
00154     strm << DapIndent::LMarg << "name: " << _name << endl ;
00155     strm << DapIndent::LMarg << "type: " << type_name() << endl ;
00156     strm << DapIndent::LMarg << "read_p: " << _read_p << endl ;
00157     strm << DapIndent::LMarg << "send_p: " << _send_p << endl ;
00158     strm << DapIndent::LMarg << "synthesized_p: " << _synthesized_p << endl ;
00159     strm << DapIndent::LMarg << "parent: " << (void *)d_parent << endl ;
00160     strm << DapIndent::LMarg << "attributes: " << endl ;
00161     DapIndent::Indent() ;
00162     d_attr.dump(strm) ;
00163     DapIndent::UnIndent() ;
00164 
00165     DapIndent::UnIndent() ;
00166 }
00167 
00170 string
00171 BaseType::name() const
00172 {
00173     return _name;
00174 }
00175 
00177 void
00178 BaseType::set_name(const string &n)
00179 {
00180     string name = n;
00181     _name = www2id(name); // www2id writes into its param.
00182 }
00183 
00185 Type
00186 BaseType::type() const
00187 {
00188     return _type;
00189 }
00190 
00192 void
00193 BaseType::set_type(const Type &t)
00194 {
00195     _type = t;
00196 }
00197 
00199 string
00200 BaseType::type_name() const
00201 {
00202     switch (_type) {
00203     case dods_null_c:
00204         return string("Null");
00205     case dods_byte_c:
00206         return string("Byte");
00207     case dods_int16_c:
00208         return string("Int16");
00209     case dods_uint16_c:
00210         return string("UInt16");
00211     case dods_int32_c:
00212         return string("Int32");
00213     case dods_uint32_c:
00214         return string("UInt32");
00215     case dods_float32_c:
00216         return string("Float32");
00217     case dods_float64_c:
00218         return string("Float64");
00219     case dods_str_c:
00220         return string("String");
00221     case dods_url_c:
00222         return string("Url");
00223     case dods_array_c:
00224         return string("Array");
00225     case dods_structure_c:
00226         return string("Structure");
00227     case dods_sequence_c:
00228         return string("Sequence");
00229     case dods_grid_c:
00230         return string("Grid");
00231     default:
00232         cerr << "BaseType::type_name: Undefined type" << endl;
00233         return string("");
00234     }
00235 }
00236 
00242 bool
00243 BaseType::is_simple_type()
00244 {
00245     switch (type()) {
00246     case dods_null_c:
00247     case dods_byte_c:
00248     case dods_int16_c:
00249     case dods_uint16_c:
00250     case dods_int32_c:
00251     case dods_uint32_c:
00252     case dods_float32_c:
00253     case dods_float64_c:
00254     case dods_str_c:
00255     case dods_url_c:
00256         return true;
00257 
00258     case dods_array_c:
00259     case dods_structure_c:
00260     case dods_sequence_c:
00261     case dods_grid_c:
00262         return false;
00263     }
00264 
00265     return false;
00266 }
00267 
00271 bool
00272 BaseType::is_vector_type()
00273 {
00274     switch (type()) {
00275     case dods_null_c:
00276     case dods_byte_c:
00277     case dods_int16_c:
00278     case dods_uint16_c:
00279     case dods_int32_c:
00280     case dods_uint32_c:
00281     case dods_float32_c:
00282     case dods_float64_c:
00283     case dods_str_c:
00284     case dods_url_c:
00285         return false;
00286 
00287     case dods_array_c:
00288         return true;
00289 
00290     case dods_structure_c:
00291     case dods_sequence_c:
00292     case dods_grid_c:
00293         return false;
00294     }
00295 
00296     return false;
00297 }
00298 
00303 bool
00304 BaseType::is_constructor_type()
00305 {
00306     switch (type()) {
00307     case dods_null_c:
00308     case dods_byte_c:
00309     case dods_int16_c:
00310     case dods_uint16_c:
00311     case dods_int32_c:
00312     case dods_uint32_c:
00313     case dods_float32_c:
00314     case dods_float64_c:
00315     case dods_str_c:
00316     case dods_url_c:
00317     case dods_array_c:
00318         return false;
00319 
00320     case dods_structure_c:
00321     case dods_sequence_c:
00322     case dods_grid_c:
00323         return true;
00324     }
00325 
00326     return false;
00327 }
00328 
00354 int
00355 BaseType::element_count(bool)
00356 {
00357     return 1;
00358 }
00359 
00363 bool
00364 BaseType::synthesized_p()
00365 {
00366     return _synthesized_p;
00367 }
00368 
00374 void
00375 BaseType::set_synthesized_p(bool state)
00376 {
00377     _synthesized_p = state;
00378 }
00379 
00380 // Return the state of _read_p (true if the value of the variable has been
00381 // read (and is in memory) false otherwise).
00382 
00391 bool
00392 BaseType::read_p()
00393 {
00394     return _read_p;
00395 }
00396 
00430 void
00431 BaseType::set_read_p(bool state)
00432 {
00433     if (! _synthesized_p) {
00434         DBG(cerr << "Changing read_p state of " << name() << endl);
00435         _read_p = state;
00436     }
00437 }
00438 
00449 bool
00450 BaseType::send_p()
00451 {
00452     return _send_p;
00453 }
00454 
00463 void
00464 BaseType::set_send_p(bool state)
00465 {
00466     DBG(cerr << "Calling BaseType::set_send_p() for: " << this->name()
00467         << endl);
00468     _send_p = state;
00469 }
00470 
00471 
00477 AttrTable &
00478 BaseType::get_attr_table()
00479 {
00480     return d_attr;
00481 }
00482 
00485 void
00486 BaseType::set_attr_table(const AttrTable &at)
00487 {
00488     d_attr = at;
00489 }
00490 
00502 bool
00503 BaseType::is_in_selection()
00504 {
00505     return d_in_selection;
00506 }
00507 
00517 void
00518 BaseType::set_in_selection(bool state)
00519 {
00520     d_in_selection = state;
00521 }
00522 
00523 // Protected method.
00530 void
00531 BaseType::set_parent(BaseType *parent)
00532 {
00533     if (!dynamic_cast<Constructor *>(parent)
00534         && !dynamic_cast<Vector *>(parent))
00535         throw InternalErr("Call to set_parent with incorrect variable type.");
00536 
00537     d_parent = parent;
00538 }
00539 
00540 // Public method.
00541 
00547 BaseType *
00548 BaseType::get_parent()
00549 {
00550     return d_parent;
00551 }
00552 
00553 // Documented in the header file.
00554 BaseType *
00555 BaseType::var(const string &/*name*/, bool /*exact_match*/, btp_stack */*s*/)
00556 {
00557     return static_cast<BaseType *>(0);
00558 }
00559 
00576 BaseType *
00577 BaseType::var(const string &, btp_stack &)
00578 {
00579     return static_cast<BaseType *>(0);
00580 }
00581 
00611 void
00612 BaseType::add_var(BaseType *, Part)
00613 {
00614     throw InternalErr(__FILE__, __LINE__, "BaseType::add_var unimplemented");
00615 }
00616 
00687 bool
00688 BaseType::read(const string &)
00689 {
00690     if (_read_p)
00691         return false;
00692 
00693     throw InternalErr("Unimplemented BaseType::read() method called.");
00694 }
00695 
00696 void
00697 BaseType::intern_data(const string &dataset, ConstraintEvaluator &, DDS &dds)
00698 {
00699     dds.timeout_on();
00700 
00701     if (!read_p())
00702         read(dataset);          // read() throws Error and InternalErr
00703 
00704     dds.timeout_off();
00705 }
00706 
00749 void
00750 BaseType::print_decl(FILE *out, string space, bool print_semi,
00751                      bool constraint_info, bool constrained)
00752 {
00753     // if printing the constrained declaration, exit if this variable was not
00754     // selected.
00755     if (constrained && !send_p())
00756         return;
00757 
00758     fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(),
00759             id2www(_name).c_str()) ;
00760 
00761     if (constraint_info) {
00762         if (send_p())
00763             fprintf(out, ": Send True") ;
00764         else
00765             fprintf(out, ": Send False") ;
00766     }
00767 
00768     if (print_semi)
00769         fprintf(out, ";\n") ;
00770 }
00771 
00814 void
00815 BaseType::print_decl(ostream &out, string space, bool print_semi,
00816                      bool constraint_info, bool constrained)
00817 {
00818     // if printing the constrained declaration, exit if this variable was not
00819     // selected.
00820     if (constrained && !send_p())
00821         return;
00822 
00823     out << space << type_name() << " " << id2www(_name) ;
00824 
00825     if (constraint_info) {
00826         if (send_p())
00827             out << ": Send True" ;
00828         else
00829             out << ": Send False" ;
00830     }
00831 
00832     if (print_semi)
00833         out << ";\n" ;
00834 }
00835 
00842 void
00843 BaseType::print_xml(FILE *out, string space, bool constrained)
00844 {
00845     if (constrained && !send_p())
00846         return;
00847 
00848     fprintf(out, "%s<%s", space.c_str(), type_name().c_str());
00849     if (!_name.empty())
00850         fprintf(out, " name=\"%s\"", id2xml(_name).c_str());
00851 
00852     if (get_attr_table().get_size() > 0) {
00853         fprintf(out, ">\n"); // close the variable's tag
00854         get_attr_table().print_xml(out, space + "    ", constrained);
00855         // After attributes, print closing tag
00856         fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str());
00857     }
00858     else {
00859         fprintf(out, "/>\n"); // no attributes; just close tag.
00860     }
00861 }
00862 
00869 void
00870 BaseType::print_xml(ostream &out, string space, bool constrained)
00871 {
00872     if (constrained && !send_p())
00873         return;
00874 
00875     out << space << "<" << type_name() ;
00876     if (!_name.empty())
00877         out << " name=\"" << id2xml(_name) << "\"" ;
00878 
00879     if (get_attr_table().get_size() > 0) {
00880         out << ">\n" ;
00881         get_attr_table().print_xml(out, space + "    ", constrained);
00882         // After attributes, print closing tag
00883         out << space << "</" << type_name() << ">\n" ;
00884     }
00885     else {
00886         out << "/>\n" ;
00887     }
00888 }
00889 
00890 // Compares the object's current state with the semantics of a particular
00891 // type. This will typically be defined in ctor classes (which have
00892 // complicated semantics). For BaseType, an object is semantically correct if
00893 // it has both a non-null name and type.
00894 //
00895 // NB: This is not the same as an invariant -- during the parse objects exist
00896 // but have no name. Also, the bool ALL defaults to false for BaseType. It is
00897 // used by children of CtorType.
00898 //
00899 // Returns: true if the object is semantically correct, false otherwise.
00900 
00929 bool
00930 BaseType::check_semantics(string &msg, bool)
00931 {
00932     bool sem = (_type != dods_null_c && _name.length());
00933 
00934     if (!sem)
00935         msg = "Every variable must have both a name and a type\n";
00936 
00937     return sem;
00938 }
00939 
00976 bool
00977 BaseType::ops(BaseType *, int, const string &)
00978 {
00979     // Even though ops is a public method, it can never be called because
00980     // they will never have a BaseType object since this class is abstract,
00981     // however any of the child classes could by mistake call BaseType::ops
00982     // so this is an internal error. Jose Garcia
00983     throw InternalErr(__FILE__, __LINE__, "Unimplemented operator.");
00984 }

Generated on Sat Jan 19 04:05:25 2008 for libdap++ by  doxygen 1.5.4