libdap++  Updated for version 3.11.7
BaseType.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1994-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // Implementation for BaseType.
33 //
34 // jhrg 9/6/94
35 
36 #include "config.h"
37 
38 #include <cstdio> // for stdin and stdout
39 
40 #include <sstream>
41 #include <string>
42 
43 //#define DODS_DEBUG
44 
45 #include "BaseType.h"
46 #include "Byte.h"
47 #include "Int16.h"
48 #include "UInt16.h"
49 #include "Int32.h"
50 #include "UInt32.h"
51 #include "Float32.h"
52 #include "Float64.h"
53 #include "Str.h"
54 #include "Url.h"
55 #include "Array.h"
56 #include "Structure.h"
57 #include "Sequence.h"
58 #include "Grid.h"
59 
60 #include "InternalErr.h"
61 
62 #include "util.h"
63 #include "escaping.h"
64 
65 #include "debug.h"
66 
67 using namespace std;
68 
69 namespace libdap {
70 
71 // Protected copy mfunc
72 
79 void
80 BaseType::_duplicate(const BaseType &bt)
81 {
82  DBG2(cerr << "BaseType::_duplicate: " << bt._name << " send_p: "
83  << bt._send_p << endl);
84  _name = bt._name;
85  _type = bt._type;
86  _dataset = bt._dataset;
87  _read_p = bt._read_p; // added, reza
88  _send_p = bt._send_p; // added, reza
89  d_in_selection = bt.d_in_selection;
90  _synthesized_p = bt._synthesized_p; // 5/11/2001 jhrg
91 
92  d_parent = bt.d_parent; // copy pointers 6/4/2001 jhrg
93 
94  d_attr = bt.d_attr; // Deep copy.
95 }
96 
97 // Public mfuncs
98 
110 BaseType::BaseType(const string &n, const Type &t)
111  : _name(n), _type(t), _dataset(""), _read_p(false), _send_p(false),
112  d_in_selection(false), _synthesized_p(false), d_parent(0)
113 {}
114 
128 BaseType::BaseType(const string &n, const string &d, const Type &t)
129  : _name(n), _type(t), _dataset(d), _read_p(false), _send_p(false),
130  d_in_selection(false), _synthesized_p(false), d_parent(0)
131 {}
132 
134 BaseType::BaseType(const BaseType &copy_from) : DapObj()
135 {
136  _duplicate(copy_from);
137 }
138 
140 {
141  DBG2(cerr << "Entering ~BaseType (" << this << ")" << endl);
142  DBG2(cerr << "Exiting ~BaseType" << endl);
143 }
144 
145 BaseType &
147 {
148  if (this == &rhs)
149  return *this;
150 
151  _duplicate(rhs);
152 
153  return *this;
154 }
155 
160 string
162 {
163  ostringstream oss;
164  oss << "BaseType (" << this << "):" << endl
165  << " _name: " << _name << endl
166  << " _type: " << type_name() << endl
167  << " _dataset: " << _dataset << endl
168  << " _read_p: " << _read_p << endl
169  << " _send_p: " << _send_p << endl
170  << " _synthesized_p: " << _synthesized_p << endl
171  << " d_parent: " << d_parent << endl
172  << " d_attr: " << hex << &d_attr << dec << endl;
173 
174  return oss.str();
175 }
176 
185 void
186 BaseType::dump(ostream &strm) const
187 {
188  strm << DapIndent::LMarg << "BaseType::dump - ("
189  << (void *)this << ")" << endl ;
191 
192  strm << DapIndent::LMarg << "name: " << _name << endl ;
193  strm << DapIndent::LMarg << "type: " << type_name() << endl ;
194  strm << DapIndent::LMarg << "dataset: " << _dataset << endl ;
195  strm << DapIndent::LMarg << "read_p: " << _read_p << endl ;
196  strm << DapIndent::LMarg << "send_p: " << _send_p << endl ;
197  strm << DapIndent::LMarg << "synthesized_p: " << _synthesized_p << endl ;
198  strm << DapIndent::LMarg << "parent: " << (void *)d_parent << endl ;
199  strm << DapIndent::LMarg << "attributes: " << endl ;
201  d_attr.dump(strm) ;
203 
205 }
206 
209 string
211 {
212  return _name;
213 }
214 
216 void
217 BaseType::set_name(const string &n)
218 {
219  string name = n;
220  _name = www2id(name); // www2id writes into its param.
221 }
222 
230 string
232 {
233  return _dataset;
234 }
235 
237 Type
239 {
240  return _type;
241 }
242 
244 void
246 {
247  _type = t;
248 }
249 
251 string
253 {
254  switch (_type) {
255  case dods_null_c:
256  return string("Null");
257  case dods_byte_c:
258  return string("Byte");
259  case dods_int16_c:
260  return string("Int16");
261  case dods_uint16_c:
262  return string("UInt16");
263  case dods_int32_c:
264  return string("Int32");
265  case dods_uint32_c:
266  return string("UInt32");
267  case dods_float32_c:
268  return string("Float32");
269  case dods_float64_c:
270  return string("Float64");
271  case dods_str_c:
272  return string("String");
273  case dods_url_c:
274  return string("Url");
275  case dods_array_c:
276  return string("Array");
277  case dods_structure_c:
278  return string("Structure");
279  case dods_sequence_c:
280  return string("Sequence");
281  case dods_grid_c:
282  return string("Grid");
283  default:
284  cerr << "BaseType::type_name: Undefined type" << endl;
285  return string("");
286  }
287 }
288 
294 bool
296 {
297  switch (type()) {
298  case dods_null_c:
299  case dods_byte_c:
300  case dods_int16_c:
301  case dods_uint16_c:
302  case dods_int32_c:
303  case dods_uint32_c:
304  case dods_float32_c:
305  case dods_float64_c:
306  case dods_str_c:
307  case dods_url_c:
308  return true;
309 
310  case dods_array_c:
311  case dods_structure_c:
312  case dods_sequence_c:
313  case dods_grid_c:
314  return false;
315  }
316 
317  return false;
318 }
319 
323 bool
325 {
326  switch (type()) {
327  case dods_null_c:
328  case dods_byte_c:
329  case dods_int16_c:
330  case dods_uint16_c:
331  case dods_int32_c:
332  case dods_uint32_c:
333  case dods_float32_c:
334  case dods_float64_c:
335  case dods_str_c:
336  case dods_url_c:
337  return false;
338 
339  case dods_array_c:
340  return true;
341 
342  case dods_structure_c:
343  case dods_sequence_c:
344  case dods_grid_c:
345  return false;
346  }
347 
348  return false;
349 }
350 
355 bool
357 {
358  switch (type()) {
359  case dods_null_c:
360  case dods_byte_c:
361  case dods_int16_c:
362  case dods_uint16_c:
363  case dods_int32_c:
364  case dods_uint32_c:
365  case dods_float32_c:
366  case dods_float64_c:
367  case dods_str_c:
368  case dods_url_c:
369  case dods_array_c:
370  return false;
371 
372  case dods_structure_c:
373  case dods_sequence_c:
374  case dods_grid_c:
375  return true;
376  }
377 
378  return false;
379 }
380 
406 int
408 {
409  return 1;
410 }
411 
415 bool
417 {
418  return _synthesized_p;
419 }
420 
426 void
428 {
429  _synthesized_p = state;
430 }
431 
432 // Return the state of _read_p (true if the value of the variable has been
433 // read (and is in memory) false otherwise).
434 
443 bool
445 {
446  return _read_p;
447 }
448 
482 void
484 {
485  if (! _synthesized_p) {
486  DBG2(cerr << "Changing read_p state of " << name() << " to "
487  << state << endl);
488  _read_p = state;
489  }
490 }
491 
502 bool
504 {
505  return _send_p;
506 }
507 
516 void
518 {
519  DBG2(cerr << "Calling BaseType::set_send_p() for: " << this->name()
520  << endl);
521  _send_p = state;
522 }
523 
524 
530 AttrTable &
532 {
533  return d_attr;
534 }
535 
538 void
540 {
541  d_attr = at;
542 }
543 
571  AttrTable *at = at_container->get_attr_table(name());
572 
573  DBG(cerr << "In BaseType::transfer_attributes; processing " << name() << endl);
574 
575  if (at) {
576  at->set_is_global_attribute(false);
577  DBG(cerr << "Processing AttrTable: " << at->get_name() << endl);
578 
579  AttrTable::Attr_iter at_p = at->attr_begin();
580  while (at_p != at->attr_end()) {
581  DBG(cerr << "About to append " << "attr name, type:" << at->get_name(at_p) << ", " << at->get_type(at_p) << endl);
582 
583  if (at->get_attr_type(at_p) == Attr_container)
584  get_attr_table().append_container(new AttrTable(*at->get_attr_table(at_p)), at->get_name(at_p));
585  else
586  get_attr_table().append_attr(at->get_name(at_p), at->get_type(at_p), at->get_attr_vector(at_p));
587 
588  at_p++;
589  }
590  }
591 }
592 
604 bool
606 {
607  return d_in_selection;
608 }
609 
619 void
621 {
622  d_in_selection = state;
623 }
624 
625 // Protected method.
634 void
636 {
637  if (!dynamic_cast<Constructor *>(parent)
638  && !dynamic_cast<Vector *>(parent)
639  && parent != 0)
640  throw InternalErr("Call to set_parent with incorrect variable type.");
641 
642  d_parent = parent;
643 }
644 
645 // Public method.
646 
652 BaseType *
654 {
655  return d_parent;
656 }
657 
658 // Documented in the header file.
659 BaseType *
660 BaseType::var(const string &/*name*/, bool /*exact_match*/, btp_stack */*s*/)
661 {
662  return static_cast<BaseType *>(0);
663 }
664 
681 BaseType *
682 BaseType::var(const string &, btp_stack &)
683 {
684  return static_cast<BaseType *>(0);
685 }
686 
716 void
718 {
719  throw InternalErr(__FILE__, __LINE__, "BaseType::add_var unimplemented");
720 }
721 
787 bool
789 {
790  if (_read_p)
791  return false;
792 
793  throw InternalErr("Unimplemented BaseType::read() method called for the variable named: " + name());
794 }
795 
796 void
798 {
799  dds.timeout_on();
800  DBG2(cerr << "BaseType::intern_data: " << name() << endl);
801  if (!read_p())
802  read(); // read() throws Error and InternalErr
803 
804  dds.timeout_off();
805 }
806 
807 #if FILE_METHODS
808 
850 void
851 BaseType::print_decl(FILE *out, string space, bool print_semi,
852  bool constraint_info, bool constrained)
853 {
854  // if printing the constrained declaration, exit if this variable was not
855  // selected.
856  if (constrained && !send_p())
857  return;
858 
859  fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(),
860  id2www(_name).c_str()) ;
861 
862  if (constraint_info) {
863  if (send_p())
864  fprintf(out, ": Send True") ;
865  else
866  fprintf(out, ": Send False") ;
867  }
868 
869  if (print_semi)
870  fprintf(out, ";\n") ;
871 }
872 #endif
873 
916 void
917 BaseType::print_decl(ostream &out, string space, bool print_semi,
918  bool constraint_info, bool constrained)
919 {
920  // if printing the constrained declaration, exit if this variable was not
921  // selected.
922  if (constrained && !send_p())
923  return;
924 
925  out << space << type_name() << " " << id2www(_name) ;
926 
927  if (constraint_info) {
928  if (send_p())
929  out << ": Send True" ;
930  else
931  out << ": Send False" ;
932  }
933 
934  if (print_semi)
935  out << ";\n" ;
936 }
937 
938 #if FILE_METHODS
939 
946 void
947 BaseType::print_xml(FILE *out, string space, bool constrained)
948 {
949  if (constrained && !send_p())
950  return;
951 
952  fprintf(out, "%s<%s", space.c_str(), type_name().c_str());
953  if (!_name.empty())
954  fprintf(out, " name=\"%s\"", id2xml(_name).c_str());
955 
956  if (get_attr_table().get_size() > 0) {
957  fprintf(out, ">\n"); // close the variable's tag
958  get_attr_table().print_xml(out, space + " ", constrained);
959  // After attributes, print closing tag
960  fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str());
961  }
962  else {
963  fprintf(out, "/>\n"); // no attributes; just close tag.
964  }
965 }
966 #endif
967 
975 void
976 BaseType::print_xml(ostream &out, string space, bool constrained)
977 {
978  if (constrained && !send_p())
979  return;
980 
981  out << space << "<" << type_name() ;
982  if (!_name.empty())
983  out << " name=\"" << id2xml(_name) << "\"" ;
984 
985  if (get_attr_table().get_size() > 0) {
986  out << ">\n" ;
987  get_attr_table().print_xml(out, space + " ", constrained);
988  // After attributes, print closing tag
989  out << space << "</" << type_name() << ">\n" ;
990  }
991  else {
992  out << "/>\n" ;
993  }
994 }
995 
1002 void
1003 BaseType::print_xml_writer(XMLWriter &xml, bool constrained)
1004 {
1005  if (constrained && !send_p())
1006  return;
1007 
1008  if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*)type_name().c_str()) < 0)
1009  throw InternalErr(__FILE__, __LINE__, "Could not write " + type_name() + " element");
1010 
1011  if (!_name.empty())
1012  if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)_name.c_str()) < 0)
1013  throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
1014 
1015  if (get_attr_table().get_size() > 0)
1017 
1018  if (xmlTextWriterEndElement(xml.get_writer()) < 0)
1019  throw InternalErr(__FILE__, __LINE__, "Could not end " + type_name() + " element");
1020 
1021 }
1022 
1023 // Compares the object's current state with the semantics of a particular
1024 // type. This will typically be defined in ctor classes (which have
1025 // complicated semantics). For BaseType, an object is semantically correct if
1026 // it has both a non-null name and type.
1027 //
1028 // NB: This is not the same as an invariant -- during the parse objects exist
1029 // but have no name. Also, the bool ALL defaults to false for BaseType. It is
1030 // used by children of CtorType.
1031 //
1032 // Returns: true if the object is semantically correct, false otherwise.
1033 
1062 bool
1063 BaseType::check_semantics(string &msg, bool)
1064 {
1065  bool sem = (_type != dods_null_c && _name.length());
1066 
1067  if (!sem)
1068  msg = "Every variable must have both a name and a type\n";
1069 
1070  return sem;
1071 }
1072 
1107 bool
1109 {
1110  // Even though ops is a public method, it can never be called because
1111  // they will never have a BaseType object since this class is abstract,
1112  // however any of the child classes could by mistake call BaseType::ops
1113  // so this is an internal error. Jose Garcia
1114  throw InternalErr(__FILE__, __LINE__, "Unimplemented operator.");
1115 }
1116 
1126 unsigned int
1127 BaseType::width(bool /*constrained*/)
1128 {
1129  return width();
1130 }
1131 
1132 } // namespace libdap
std::vector< entry * >::iterator Attr_iter
Definition: AttrTable.h:237
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:788
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:444
static void UnIndent()
Definition: DapIndent.cc:49
virtual void print_decl(FILE *out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
Definition: BaseType.cc:851
virtual Attr_iter attr_end()
Definition: AttrTable.cc:724
virtual ~BaseType()
Definition: BaseType.cc:139
virtual void print_xml(FILE *out, string space=" ", bool constrained=false)
Definition: BaseType.cc:947
Part
Names the parts of multi-section constructor data types.
Definition: BaseType.h:102
BaseType & operator=(const BaseType &rhs)
Definition: BaseType.cc:146
Contains the attributes for a dataset.
Definition: AttrTable.h:150
virtual void set_name(const string &n)
Sets the name of the class instance.
Definition: BaseType.cc:217
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
Definition: AttrTable.cc:618
virtual void intern_data(ConstraintEvaluator &eval, DDS &dds)
Definition: BaseType.cc:797
virtual void print_xml_writer(XMLWriter &xml, bool constrained=false)
Definition: BaseType.cc:1003
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
Definition: AttrTable.cc:1303
string id2xml(string in, const string &not_allowed)
Definition: escaping.cc:270
virtual BaseType * get_parent()
Definition: BaseType.cc:653
virtual unsigned int width()=0
Returns the size of the class instance data.
virtual void set_is_global_attribute(bool ga)
Definition: AttrTable.h:285
void print_xml_writer(XMLWriter &xml)
Definition: AttrTable.cc:1409
void timeout_off()
Definition: DDS.cc:758
virtual void add_var(BaseType *bt, Part part=nil)
Add a variable.
Definition: BaseType.cc:717
Type
Identifies the data type.
Definition: BaseType.h:135
Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:238
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:242
virtual string toString()
Definition: BaseType.cc:161
#define DBG2(x)
Definition: debug.h:73
virtual void set_in_selection(bool state)
Definition: BaseType.cc:620
virtual bool is_in_selection()
Is this variable part of the current selection?
Definition: BaseType.cc:605
stack< BaseType * > btp_stack
Definition: BaseType.h:218
virtual void set_parent(BaseType *parent)
Definition: BaseType.cc:635
A class for software fault reporting.
Definition: InternalErr.h:64
virtual bool is_vector_type()
Returns true if the instance is a vector (i.e., array) type variable.
Definition: BaseType.cc:324
virtual bool is_constructor_type()
Returns true if the instance is a constructor (i.e., Structure, Sequence or Grid) type variable...
Definition: BaseType.cc:356
string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:231
xmlTextWriterPtr get_writer()
Definition: XMLWriter.h:36
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Returns a pointer to a member of a constructor class.
Definition: BaseType.cc:660
#define DBG(x)
Definition: debug.h:58
string type_name() const
Returns the type of the class instance as a string.
Definition: BaseType.cc:252
virtual int element_count(bool leaves=false)
Count the members of constructor types.
Definition: BaseType.cc:407
virtual void set_send_p(bool state)
Definition: BaseType.cc:517
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:414
static void Indent()
Definition: DapIndent.cc:43
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:612
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:186
void set_type(const Type &t)
Sets the type of the class instance.
Definition: BaseType.cc:245
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:483
virtual bool synthesized_p()
Definition: BaseType.cc:416
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:210
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:716
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:218
void timeout_on()
Definition: DDS.cc:750
void _duplicate(const BaseType &bt)
Perform a deep copy.
Definition: BaseType.cc:80
BaseType(const string &n, const Type &t)
The BaseType constructor.
Definition: BaseType.cc:110
Evaluate a constraint expression.
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:78
virtual AttrTable & get_attr_table()
Definition: BaseType.cc:531
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition: AttrTable.cc:311
The basic data type for the DODS DAP types.
Definition: BaseType.h:194
libdap base object for common functionality of libdap objects
Definition: DapObj.h:55
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
Definition: AttrTable.cc:626
virtual void set_attr_table(const AttrTable &at)
Definition: BaseType.cc:539
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1483
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: BaseType.cc:1108
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition: AttrTable.cc:658
virtual bool is_simple_type()
Returns true if the instance is a numeric, string or URL type variable.
Definition: BaseType.cc:295
virtual void transfer_attributes(AttrTable *at)
Definition: BaseType.cc:570
virtual bool send_p()
Should this variable be sent?
Definition: BaseType.cc:503
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:235
string id2www(string in, const string &allowable)
Definition: escaping.cc:151
virtual bool check_semantics(string &msg, bool all=false)
Compare an object's current state with the semantics of its type.
Definition: BaseType.cc:1063
virtual void set_synthesized_p(bool state)
Definition: BaseType.cc:427