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