libdap++  Updated for version 3.8.2
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  DBG(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  DBG(cerr << "Entering ~BaseType (" << this << ")" << endl);
142  DBG(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  DBG(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  DBG(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 
570 void
572 {
573  AttrTable *at = at_container->get_attr_table(name());
574 
575  DBG(cerr << "In BaseType::transfer_attributes; processing " << name() << endl);
576 
577  if (at) {
578  at->set_is_global_attribute(false);
579  DBG(cerr << "Processing AttrTable: " << at->get_name() << endl);
580 
581  AttrTable::Attr_iter at_p = at->attr_begin();
582  while (at_p != at->attr_end()) {
583  DBG(cerr << "About to append " << endl);
584  DBG(cerr << "attr name,type:" << at->get_name(at_p) << ", " << at->get_type(at_p) << endl);
585 
586  if (at->get_attr_type(at_p) == Attr_container)
588  at->get_name(at_p));
589  else
590  get_attr_table().append_attr(at->get_name(at_p),
591  at->get_type(at_p), at->get_attr_vector(at_p));
592 
593  at_p++;
594  }
595  }
596 }
597 
609 bool
611 {
612  return d_in_selection;
613 }
614 
624 void
626 {
627  d_in_selection = state;
628 }
629 
630 // Protected method.
637 void
639 {
640  if (!dynamic_cast<Constructor *>(parent)
641  && !dynamic_cast<Vector *>(parent))
642  throw InternalErr("Call to set_parent with incorrect variable type.");
643 
644  d_parent = parent;
645 }
646 
647 // Public method.
648 
654 BaseType *
656 {
657  return d_parent;
658 }
659 
660 // Documented in the header file.
661 BaseType *
662 BaseType::var(const string &/*name*/, bool /*exact_match*/, btp_stack */*s*/)
663 {
664  return static_cast<BaseType *>(0);
665 }
666 
683 BaseType *
684 BaseType::var(const string &, btp_stack &)
685 {
686  return static_cast<BaseType *>(0);
687 }
688 
718 void
720 {
721  throw InternalErr(__FILE__, __LINE__, "BaseType::add_var unimplemented");
722 }
723 
789 bool
791 {
792  if (_read_p)
793  return false;
794 
795  throw InternalErr("Unimplemented BaseType::read() method called.");
796 }
797 
798 void
800 {
801  dds.timeout_on();
802  DBG(cerr << "BaseType::intern_data: " << name() << endl);
803  if (!read_p())
804  read(); // read() throws Error and InternalErr
805 
806  dds.timeout_off();
807 }
808 
809 #if FILE_METHODS
810 
852 void
853 BaseType::print_decl(FILE *out, string space, bool print_semi,
854  bool constraint_info, bool constrained)
855 {
856  // if printing the constrained declaration, exit if this variable was not
857  // selected.
858  if (constrained && !send_p())
859  return;
860 
861  fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(),
862  id2www(_name).c_str()) ;
863 
864  if (constraint_info) {
865  if (send_p())
866  fprintf(out, ": Send True") ;
867  else
868  fprintf(out, ": Send False") ;
869  }
870 
871  if (print_semi)
872  fprintf(out, ";\n") ;
873 }
874 #endif
875 
918 void
919 BaseType::print_decl(ostream &out, string space, bool print_semi,
920  bool constraint_info, bool constrained)
921 {
922  // if printing the constrained declaration, exit if this variable was not
923  // selected.
924  if (constrained && !send_p())
925  return;
926 
927  out << space << type_name() << " " << id2www(_name) ;
928 
929  if (constraint_info) {
930  if (send_p())
931  out << ": Send True" ;
932  else
933  out << ": Send False" ;
934  }
935 
936  if (print_semi)
937  out << ";\n" ;
938 }
939 
940 #if FILE_METHODS
941 
947 void
948 BaseType::print_xml(FILE *out, string space, bool constrained)
949 {
950  if (constrained && !send_p())
951  return;
952 
953  fprintf(out, "%s<%s", space.c_str(), type_name().c_str());
954  if (!_name.empty())
955  fprintf(out, " name=\"%s\"", id2xml(_name).c_str());
956 
957  if (get_attr_table().get_size() > 0) {
958  fprintf(out, ">\n"); // close the variable's tag
959  get_attr_table().print_xml(out, space + " ", constrained);
960  // After attributes, print closing tag
961  fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str());
962  }
963  else {
964  fprintf(out, "/>\n"); // no attributes; just close tag.
965  }
966 }
967 #endif
968 
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 
996 // Compares the object's current state with the semantics of a particular
997 // type. This will typically be defined in ctor classes (which have
998 // complicated semantics). For BaseType, an object is semantically correct if
999 // it has both a non-null name and type.
1000 //
1001 // NB: This is not the same as an invariant -- during the parse objects exist
1002 // but have no name. Also, the bool ALL defaults to false for BaseType. It is
1003 // used by children of CtorType.
1004 //
1005 // Returns: true if the object is semantically correct, false otherwise.
1006 
1035 bool
1036 BaseType::check_semantics(string &msg, bool)
1037 {
1038  bool sem = (_type != dods_null_c && _name.length());
1039 
1040  if (!sem)
1041  msg = "Every variable must have both a name and a type\n";
1042 
1043  return sem;
1044 }
1045 
1080 bool
1082 {
1083  // Even though ops is a public method, it can never be called because
1084  // they will never have a BaseType object since this class is abstract,
1085  // however any of the child classes could by mistake call BaseType::ops
1086  // so this is an internal error. Jose Garcia
1087  throw InternalErr(__FILE__, __LINE__, "Unimplemented operator.");
1088 }
1089 
1090 } // namespace libdap
std::vector< entry * >::iterator Attr_iter
Definition: AttrTable.h:233
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:790
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:444
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:210
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:853
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:186
virtual Attr_iter attr_end()
Definition: AttrTable.cc:649
virtual ~BaseType()
Definition: BaseType.cc:139
virtual void print_xml(FILE *out, string space=" ", bool constrained=false)
Definition: BaseType.cc:948
Part
Names the parts of multi-section constructor data types.
Definition: BaseType.h:98
BaseType & operator=(const BaseType &rhs)
Definition: BaseType.cc:146
Contains the attributes for a dataset.
Definition: AttrTable.h:146
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:543
virtual void intern_data(ConstraintEvaluator &eval, DDS &dds)
Definition: BaseType.cc:799
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:208
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
Definition: AttrTable.cc:1209
string id2xml(string in, const string &not_allowed)
Definition: escaping.cc:266
virtual BaseType * get_parent()
Definition: BaseType.cc:655
virtual void set_is_global_attribute(bool ga)
Definition: AttrTable.h:281
void timeout_off()
Definition: DDS.cc:823
virtual void add_var(BaseType *bt, Part part=nil)
Add a variable.
Definition: BaseType.cc:719
Type
Identifies the data type.
Definition: BaseType.h:131
virtual string toString()
Definition: BaseType.cc:161
virtual void set_in_selection(bool state)
Definition: BaseType.cc:625
virtual bool is_in_selection()
Is this variable part of the current selection?
Definition: BaseType.cc:610
stack< BaseType * > btp_stack
Definition: BaseType.h:214
virtual void set_parent(BaseType *parent)
Definition: BaseType.cc:638
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
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:662
#define DBG(x)
Definition: debug.h:58
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:341
static void Indent()
Definition: DapIndent.cc:43
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:536
Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:238
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
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:640
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:214
void timeout_on()
Definition: DDS.cc:815
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:239
The basic data type for the DODS DAP types.
Definition: BaseType.h:190
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:552
string type_name() const
Returns the type of the class instance as a string.
Definition: BaseType.cc:252
virtual void set_attr_table(const AttrTable &at)
Definition: BaseType.cc:539
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: BaseType.cc:1081
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition: AttrTable.cc:585
virtual bool is_simple_type()
Returns true if the instance is a numeric, string or URL type variable.
Definition: BaseType.cc:295
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:200
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1309
virtual void transfer_attributes(AttrTable *at)
Definition: BaseType.cc:571
virtual bool send_p()
Should this variable be sent?
Definition: BaseType.cc:503
string id2www(string in, const string &allowable)
Definition: escaping.cc:151
string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:231
virtual bool check_semantics(string &msg, bool all=false)
Compare an object&#39;s current state with the semantics of its type.
Definition: BaseType.cc:1036
virtual void set_synthesized_p(bool state)
Definition: BaseType.cc:427