libdap++ Updated for version 3.8.2

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     DBG(cerr << "BaseType::_duplicate: " << bt._name << " send_p: "
00069             << bt._send_p << endl);
00070     _name = bt._name;
00071     _type = bt._type;
00072     _dataset = bt._dataset;
00073     _read_p = bt._read_p; // added, reza
00074     _send_p = bt._send_p; // added, reza
00075     d_in_selection = bt.d_in_selection;
00076     _synthesized_p = bt._synthesized_p; // 5/11/2001 jhrg
00077 
00078     d_parent = bt.d_parent; // copy pointers 6/4/2001 jhrg
00079 
00080     d_attr = bt.d_attr;  // Deep copy.
00081 }
00082 
00083 // Public mfuncs
00084 
00096 BaseType::BaseType(const string &n, const Type &t)
00097         : _name(n), _type(t), _dataset(""), _read_p(false), _send_p(false),
00098         d_in_selection(false), _synthesized_p(false), d_parent(0)
00099 {}
00100 
00114 BaseType::BaseType(const string &n, const string &d, const Type &t)
00115         : _name(n), _type(t), _dataset(d), _read_p(false), _send_p(false),
00116         d_in_selection(false), _synthesized_p(false), d_parent(0)
00117 {}
00118 
00120 BaseType::BaseType(const BaseType &copy_from) : DapObj()
00121 {
00122     _duplicate(copy_from);
00123 }
00124 
00125 BaseType::~BaseType()
00126 {
00127     DBG(cerr << "Entering ~BaseType (" << this << ")" << endl);
00128     DBG(cerr << "Exiting ~BaseType" << endl);
00129 }
00130 
00131 BaseType &
00132 BaseType::operator=(const BaseType &rhs)
00133 {
00134     if (this == &rhs)
00135         return *this;
00136 
00137     _duplicate(rhs);
00138 
00139     return *this;
00140 }
00141 
00146 string
00147 BaseType::toString()
00148 {
00149     ostringstream oss;
00150     oss << "BaseType (" << this << "):" << endl
00151     << "          _name: " << _name << endl
00152     << "          _type: " << type_name() << endl
00153     << "          _dataset: " << _dataset << endl
00154     << "          _read_p: " << _read_p << endl
00155     << "          _send_p: " << _send_p << endl
00156     << "          _synthesized_p: " << _synthesized_p << endl
00157     << "          d_parent: " << d_parent << endl
00158     << "          d_attr: " << hex << &d_attr << dec << endl;
00159 
00160     return oss.str();
00161 }
00162 
00171 void
00172 BaseType::dump(ostream &strm) const
00173 {
00174     strm << DapIndent::LMarg << "BaseType::dump - ("
00175     << (void *)this << ")" << endl ;
00176     DapIndent::Indent() ;
00177 
00178     strm << DapIndent::LMarg << "name: " << _name << endl ;
00179     strm << DapIndent::LMarg << "type: " << type_name() << endl ;
00180     strm << DapIndent::LMarg << "dataset: " << _dataset << endl ;
00181     strm << DapIndent::LMarg << "read_p: " << _read_p << endl ;
00182     strm << DapIndent::LMarg << "send_p: " << _send_p << endl ;
00183     strm << DapIndent::LMarg << "synthesized_p: " << _synthesized_p << endl ;
00184     strm << DapIndent::LMarg << "parent: " << (void *)d_parent << endl ;
00185     strm << DapIndent::LMarg << "attributes: " << endl ;
00186     DapIndent::Indent() ;
00187     d_attr.dump(strm) ;
00188     DapIndent::UnIndent() ;
00189 
00190     DapIndent::UnIndent() ;
00191 }
00192 
00195 string
00196 BaseType::name() const
00197 {
00198     return _name;
00199 }
00200 
00202 void
00203 BaseType::set_name(const string &n)
00204 {
00205     string name = n;
00206     _name = www2id(name); // www2id writes into its param.
00207 }
00208 
00216 string
00217 BaseType::dataset() const
00218 {
00219     return _dataset;
00220 }
00221 
00223 Type
00224 BaseType::type() const
00225 {
00226     return _type;
00227 }
00228 
00230 void
00231 BaseType::set_type(const Type &t)
00232 {
00233     _type = t;
00234 }
00235 
00237 string
00238 BaseType::type_name() const
00239 {
00240     switch (_type) {
00241     case dods_null_c:
00242         return string("Null");
00243     case dods_byte_c:
00244         return string("Byte");
00245     case dods_int16_c:
00246         return string("Int16");
00247     case dods_uint16_c:
00248         return string("UInt16");
00249     case dods_int32_c:
00250         return string("Int32");
00251     case dods_uint32_c:
00252         return string("UInt32");
00253     case dods_float32_c:
00254         return string("Float32");
00255     case dods_float64_c:
00256         return string("Float64");
00257     case dods_str_c:
00258         return string("String");
00259     case dods_url_c:
00260         return string("Url");
00261     case dods_array_c:
00262         return string("Array");
00263     case dods_structure_c:
00264         return string("Structure");
00265     case dods_sequence_c:
00266         return string("Sequence");
00267     case dods_grid_c:
00268         return string("Grid");
00269     default:
00270         cerr << "BaseType::type_name: Undefined type" << endl;
00271         return string("");
00272     }
00273 }
00274 
00280 bool
00281 BaseType::is_simple_type()
00282 {
00283     switch (type()) {
00284     case dods_null_c:
00285     case dods_byte_c:
00286     case dods_int16_c:
00287     case dods_uint16_c:
00288     case dods_int32_c:
00289     case dods_uint32_c:
00290     case dods_float32_c:
00291     case dods_float64_c:
00292     case dods_str_c:
00293     case dods_url_c:
00294         return true;
00295 
00296     case dods_array_c:
00297     case dods_structure_c:
00298     case dods_sequence_c:
00299     case dods_grid_c:
00300         return false;
00301     }
00302 
00303     return false;
00304 }
00305 
00309 bool
00310 BaseType::is_vector_type()
00311 {
00312     switch (type()) {
00313     case dods_null_c:
00314     case dods_byte_c:
00315     case dods_int16_c:
00316     case dods_uint16_c:
00317     case dods_int32_c:
00318     case dods_uint32_c:
00319     case dods_float32_c:
00320     case dods_float64_c:
00321     case dods_str_c:
00322     case dods_url_c:
00323         return false;
00324 
00325     case dods_array_c:
00326         return true;
00327 
00328     case dods_structure_c:
00329     case dods_sequence_c:
00330     case dods_grid_c:
00331         return false;
00332     }
00333 
00334     return false;
00335 }
00336 
00341 bool
00342 BaseType::is_constructor_type()
00343 {
00344     switch (type()) {
00345     case dods_null_c:
00346     case dods_byte_c:
00347     case dods_int16_c:
00348     case dods_uint16_c:
00349     case dods_int32_c:
00350     case dods_uint32_c:
00351     case dods_float32_c:
00352     case dods_float64_c:
00353     case dods_str_c:
00354     case dods_url_c:
00355     case dods_array_c:
00356         return false;
00357 
00358     case dods_structure_c:
00359     case dods_sequence_c:
00360     case dods_grid_c:
00361         return true;
00362     }
00363 
00364     return false;
00365 }
00366 
00392 int
00393 BaseType::element_count(bool)
00394 {
00395     return 1;
00396 }
00397 
00401 bool
00402 BaseType::synthesized_p()
00403 {
00404     return _synthesized_p;
00405 }
00406 
00412 void
00413 BaseType::set_synthesized_p(bool state)
00414 {
00415     _synthesized_p = state;
00416 }
00417 
00418 // Return the state of _read_p (true if the value of the variable has been
00419 // read (and is in memory) false otherwise).
00420 
00429 bool
00430 BaseType::read_p()
00431 {
00432     return _read_p;
00433 }
00434 
00468 void
00469 BaseType::set_read_p(bool state)
00470 {
00471     if (! _synthesized_p) {
00472         DBG(cerr << "Changing read_p state of " << name() << " to "
00473                  << state << endl);
00474         _read_p = state;
00475     }
00476 }
00477 
00488 bool
00489 BaseType::send_p()
00490 {
00491     return _send_p;
00492 }
00493 
00502 void
00503 BaseType::set_send_p(bool state)
00504 {
00505     DBG(cerr << "Calling BaseType::set_send_p() for: " << this->name()
00506         << endl);
00507     _send_p = state;
00508 }
00509 
00510 
00516 AttrTable &
00517 BaseType::get_attr_table()
00518 {
00519     return d_attr;
00520 }
00521 
00524 void
00525 BaseType::set_attr_table(const AttrTable &at)
00526 {
00527     d_attr = at;
00528 }
00529 
00541 bool
00542 BaseType::is_in_selection()
00543 {
00544     return d_in_selection;
00545 }
00546 
00556 void
00557 BaseType::set_in_selection(bool state)
00558 {
00559     d_in_selection = state;
00560 }
00561 
00562 // Protected method.
00569 void
00570 BaseType::set_parent(BaseType *parent)
00571 {
00572     if (!dynamic_cast<Constructor *>(parent)
00573         && !dynamic_cast<Vector *>(parent))
00574         throw InternalErr("Call to set_parent with incorrect variable type.");
00575 
00576     d_parent = parent;
00577 }
00578 
00579 // Public method.
00580 
00586 BaseType *
00587 BaseType::get_parent()
00588 {
00589     return d_parent;
00590 }
00591 
00592 // Documented in the header file.
00593 BaseType *
00594 BaseType::var(const string &/*name*/, bool /*exact_match*/, btp_stack */*s*/)
00595 {
00596     return static_cast<BaseType *>(0);
00597 }
00598 
00615 BaseType *
00616 BaseType::var(const string &, btp_stack &)
00617 {
00618     return static_cast<BaseType *>(0);
00619 }
00620 
00650 void
00651 BaseType::add_var(BaseType *, Part)
00652 {
00653     throw InternalErr(__FILE__, __LINE__, "BaseType::add_var unimplemented");
00654 }
00655 
00721 bool
00722 BaseType::read()
00723 {
00724     if (_read_p)
00725         return false;
00726 
00727     throw InternalErr("Unimplemented BaseType::read() method called.");
00728 }
00729 
00730 void
00731 BaseType::intern_data(ConstraintEvaluator &, DDS &dds)
00732 {
00733     dds.timeout_on();
00734     DBG(cerr << "BaseType::intern_data: " << name() << endl);
00735     if (!read_p())
00736         read();          // read() throws Error and InternalErr
00737 
00738     dds.timeout_off();
00739 }
00740 //#if FILE_METHODS
00783 void
00784 BaseType::print_decl(FILE *out, string space, bool print_semi,
00785                      bool constraint_info, bool constrained)
00786 {
00787     // if printing the constrained declaration, exit if this variable was not
00788     // selected.
00789     if (constrained && !send_p())
00790         return;
00791 
00792     fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(),
00793             id2www(_name).c_str()) ;
00794 
00795     if (constraint_info) {
00796         if (send_p())
00797             fprintf(out, ": Send True") ;
00798         else
00799             fprintf(out, ": Send False") ;
00800     }
00801 
00802     if (print_semi)
00803         fprintf(out, ";\n") ;
00804 }
00805 //#endif
00848 void
00849 BaseType::print_decl(ostream &out, string space, bool print_semi,
00850                      bool constraint_info, bool constrained)
00851 {
00852     // if printing the constrained declaration, exit if this variable was not
00853     // selected.
00854     if (constrained && !send_p())
00855         return;
00856 
00857     out << space << type_name() << " " << id2www(_name) ;
00858 
00859     if (constraint_info) {
00860         if (send_p())
00861             out << ": Send True" ;
00862         else
00863             out << ": Send False" ;
00864     }
00865 
00866     if (print_semi)
00867         out << ";\n" ;
00868 }
00869 //#if FILE_METHODS
00876 void
00877 BaseType::print_xml(FILE *out, string space, bool constrained)
00878 {
00879     if (constrained && !send_p())
00880         return;
00881 
00882     fprintf(out, "%s<%s", space.c_str(), type_name().c_str());
00883     if (!_name.empty())
00884         fprintf(out, " name=\"%s\"", id2xml(_name).c_str());
00885 
00886     if (get_attr_table().get_size() > 0) {
00887         fprintf(out, ">\n"); // close the variable's tag
00888         get_attr_table().print_xml(out, space + "    ", constrained);
00889         // After attributes, print closing tag
00890         fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str());
00891     }
00892     else {
00893         fprintf(out, "/>\n"); // no attributes; just close tag.
00894     }
00895 }
00896 //#endif
00903 void
00904 BaseType::print_xml(ostream &out, string space, bool constrained)
00905 {
00906     if (constrained && !send_p())
00907         return;
00908 
00909     out << space << "<" << type_name() ;
00910     if (!_name.empty())
00911         out << " name=\"" << id2xml(_name) << "\"" ;
00912 
00913     if (get_attr_table().get_size() > 0) {
00914         out << ">\n" ;
00915         get_attr_table().print_xml(out, space + "    ", constrained);
00916         // After attributes, print closing tag
00917         out << space << "</" << type_name() << ">\n" ;
00918     }
00919     else {
00920         out << "/>\n" ;
00921     }
00922 }
00923 
00924 // Compares the object's current state with the semantics of a particular
00925 // type. This will typically be defined in ctor classes (which have
00926 // complicated semantics). For BaseType, an object is semantically correct if
00927 // it has both a non-null name and type.
00928 //
00929 // NB: This is not the same as an invariant -- during the parse objects exist
00930 // but have no name. Also, the bool ALL defaults to false for BaseType. It is
00931 // used by children of CtorType.
00932 //
00933 // Returns: true if the object is semantically correct, false otherwise.
00934 
00963 bool
00964 BaseType::check_semantics(string &msg, bool)
00965 {
00966     bool sem = (_type != dods_null_c && _name.length());
00967 
00968     if (!sem)
00969         msg = "Every variable must have both a name and a type\n";
00970 
00971     return sem;
00972 }
00973 
01008 bool
01009 BaseType::ops(BaseType *, int)
01010 {
01011     // Even though ops is a public method, it can never be called because
01012     // they will never have a BaseType object since this class is abstract,
01013     // however any of the child classes could by mistake call BaseType::ops
01014     // so this is an internal error. Jose Garcia
01015     throw InternalErr(__FILE__, __LINE__, "Unimplemented operator.");
01016 }
01017 
01018 } // namespace libdap