libdap++  Updated for version 3.14.0
Structure.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 the class Structure
33 //
34 // jhrg 9/14/94
35 
36 //#define DODS_DEBUG
37 
38 #include "config.h"
39 
40 #include <sstream>
41 
42 #include "Byte.h"
43 #include "Int16.h"
44 #include "UInt16.h"
45 #include "Int32.h"
46 #include "UInt32.h"
47 #include "Float32.h"
48 #include "Float64.h"
49 #include "Str.h"
50 #include "Url.h"
51 #include "Array.h"
52 #include "Structure.h"
53 #include "Sequence.h"
54 #include "Grid.h"
55 
56 #include "DDS.h"
57 #include "ConstraintEvaluator.h"
58 
59 #include "D4Attributes.h"
60 #include "D4Group.h"
61 
62 #include "XDRStreamMarshaller.h"
63 #include "util.h"
64 #include "debug.h"
65 #include "InternalErr.h"
66 #include "escaping.h"
67 
68 using std::cerr;
69 using std::endl;
70 
71 namespace libdap {
72 
73 #if 0
74 
78 void
79 Structure::m_duplicate(const Structure &s)
80 {
82 #if 0
83  Structure &cs = const_cast<Structure &>(s);
84 
85  DBG(cerr << "Copying structure: " << name() << endl);
86 
87  for (Vars_iter i = cs.d_vars.begin(); i != cs.d_vars.end(); i++) {
88  DBG(cerr << "Copying field: " << (*i)->name() << endl);
89  // Jose Garcia
90  // I think this assert here is part of a debugging
91  // process since it is going along with a DBG call
92  // I leave it here since it can be remove by defining NDEBUG.
93  // assert(*i);
94  BaseType *btp = (*i)->ptr_duplicate();
95  btp->set_parent(this);
96  d_vars.push_back(btp);
97  }
98 #endif
99 }
100 #endif
101 
110 {}
111 
121 Structure::Structure(const string &n, const string &d)
123 {}
124 
127 {
128  DBG(cerr << "In Structure::copy_ctor for " << name() << endl);
129  //m_duplicate(rhs);
130 }
131 
133 {
134 }
135 
136 BaseType *
138 {
139  return new Structure(*this);
140 }
141 
151 BaseType *
153 {
154  // For this class, ptr_duplicate() calls the const ctor which calls
155  // Constructor's const ctor which calls Constructor::m_duplicate().
156  // Here we replicate some of that functionality, but instead call
157  // transform_to_dap4() on the contained variables.
158 
159  // Structure *dest = static_cast<Structure*>(ptr_duplicate());
160  Structure *dest = new Structure(name());
161 
162  Constructor::transform_to_dap4(root, dest);
163  dest->set_parent(container);
164 
165  return dest;
166 }
167 
168 Structure &
170 {
171  DBG(cerr << "Entering Structure::operator=" << endl);
172  if (this == &rhs)
173  return *this;
174 
175  dynamic_cast<Constructor &>(*this) = rhs; // run Constructor=
176 
177  //m_duplicate(rhs);
178 
179  DBG(cerr << "Exiting Structure::operator=" << endl);
180  return *this;
181 }
182 
183 #if 0
184 int
185 Structure::element_count(bool leaves)
186 {
187  if (!leaves)
188  return d_vars.size();
189  else {
190  int i = 0;
191  for (Vars_iter j = d_vars.begin(); j != d_vars.end(); j++) {
192  i += (*j)->element_count(leaves);
193  }
194  return i;
195  }
196 }
197 #endif
198 
199 bool
201 {
202  bool linear = true;
203  for (Vars_iter i = d_vars.begin(); linear && i != d_vars.end(); i++) {
204  if ((*i)->type() == dods_structure_c)
205  linear = linear && static_cast<Structure*>((*i))->is_linear();
206  else
207  linear = linear && (*i)->is_simple_type();
208  }
209 
210  return linear;
211 }
212 
213 #if 0
214 void
215 Structure::set_send_p(bool state)
216 {
217  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
218  (*i)->set_send_p(state);
219  }
220 
221  BaseType::set_send_p(state);
222 }
223 
224 void
225 Structure::set_read_p(bool state)
226 {
227  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
228  (*i)->set_read_p(state);
229  }
230 
231  BaseType::set_read_p(state);
232 }
233 #endif
234 #if 0
235 
240 void
241 Structure::set_in_selection(bool state)
242 {
243  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
244  (*i)->set_in_selection(state);
245  }
246 
248 }
249 #endif
250 
251 void
253 {
254  for (Vars_iter i = var_begin(); i != var_end(); i++) {
255  if ((*i)->type() == dods_sequence_c)
256  static_cast<Sequence&>(**i).set_leaf_sequence(++level);
257  else if ((*i)->type() == dods_structure_c)
258  static_cast<Structure&>(**i).set_leaf_sequence(level);
259  }
260 }
261 
262 #if 0
263 
267 void
269 {
270  // Jose Garcia
271  // Passing and invalid pointer to an object is a developer's error.
272  if (!bt)
273  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
274 
275  if (bt->is_dap4_only_type())
276  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
277 
278  // Jose Garcia
279  // Now we add a copy of bt so the external user is able to destroy bt as
280  // he/she wishes. The policy is: "If it is allocated outside, it is
281  // deallocated outside, if it is allocated inside, it is deallocated
282  // inside"
283  BaseType *btp = bt->ptr_duplicate();
284  btp->set_parent(this);
285  d_vars.push_back(btp);
286 }
287 
292 void
293 Structure::add_var_nocopy(BaseType *bt, Part)
294 {
295  if (!bt)
296  throw InternalErr(__FILE__, __LINE__, "The BaseType parameter cannot be null.");
297 
298  if (bt->is_dap4_only_type())
299  throw InternalErr(__FILE__, __LINE__, "Attempt to add a DAP4 type to a DAP2 Structure.");
300 
301  bt->set_parent(this);
302  d_vars.push_back(bt);
303 }
304 
305 
309 void
310 Structure::del_var(const string &n)
311 {
312  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
313  if ((*i)->name() == n) {
314  BaseType *bt = *i ;
315  d_vars.erase(i) ;
316  delete bt ; bt = 0;
317  return;
318  }
319  }
320 }
321 #endif
322 #if 0
323 
328 bool Structure::read()
329 {
330  if (!read_p()) {
331  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
332  (*i)->read();
333  }
334  set_read_p(true);
335  }
336 
337  return false;
338 }
339 #endif
340 #if 0
341 // TODO Recode to use width(bool)
342 unsigned int
344 {
345  unsigned int sz = 0;
346 
347  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
348  sz += (*i)->width();
349  }
350 
351  return sz;
352 }
353 
361 unsigned int
362 Structure::width(bool constrained)
363 {
364  unsigned int sz = 0;
365 
366  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
367  if (constrained) {
368  if ((*i)->send_p())
369  sz += (*i)->width(constrained);
370  }
371  else {
372  sz += (*i)->width(constrained);
373  }
374  }
375 
376  return sz;
377 }
378 #endif
379 
380 #if 0
381 void
382 Structure::intern_data(ConstraintEvaluator & eval, DDS & dds)
383 {
384  DBG(cerr << "Structure::intern_data: " << name() << endl);
385  if (!read_p())
386  read(); // read() throws Error and InternalErr
387 
388  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
389  if ((*i)->send_p()) {
390  (*i)->intern_data(eval, dds);
391  }
392  }
393 }
394 
395 bool
396 Structure::serialize(ConstraintEvaluator &eval, DDS &dds,
397  Marshaller &m, bool ce_eval)
398 {
399  dds.timeout_on();
400 
401  if (!read_p())
402  read(); // read() throws Error and InternalErr
403 
404 #if EVAL
405  if (ce_eval && !eval.eval_selection(dds, dataset()))
406  return true;
407 #endif
408 
409  dds.timeout_off();
410 
411  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
412  if ((*i)->send_p()) {
413 #ifdef CHECKSUMS
414  XDRStreamMarshaller *sm = dynamic_cast<XDRStreamMarshaller*>(&m);
415  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
416  sm->reset_checksum();
417 
418  (*i)->serialize(eval, dds, m, false);
419 
420  if (sm && sm->checksums() && (*i)->type() != dods_structure_c && (*i)->type() != dods_grid_c)
421  sm->get_checksum();
422 #else
423  (*i)->serialize(eval, dds, m, false);
424 #endif
425  }
426  }
427 
428  return true;
429 }
430 
431 bool
432 Structure::deserialize(UnMarshaller &um, DDS *dds, bool reuse)
433 {
434  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
435  (*i)->deserialize(um, dds, reuse);
436  }
437 
438  return false;
439 }
440 #endif
441 #if 0
442 
451 unsigned int
452 Structure::val2buf(void *, bool)
453 {
454  return sizeof(Structure);
455 }
456 
460 unsigned int
461 Structure::buf2val(void **)
462 {
463  return sizeof(Structure);
464 }
465 #endif
466 
467 #if 0
468 BaseType *
469 Structure::var(const string &name, bool exact_match, btp_stack *s)
470 {
471  string n = www2id(name);
472 
473  if (exact_match)
474  return m_exact_match(n, s);
475  else
476  return m_leaf_match(n, s);
477 }
478 
480 BaseType *
481 Structure::var(const string &n, btp_stack &s)
482 {
483  string name = www2id(n);
484 
485  BaseType *btp = m_exact_match(name, &s);
486  if (btp)
487  return btp;
488 
489  return m_leaf_match(name, &s);
490 }
491 #endif
492 #if 0
493 // Private method to find a variable using the shorthand name. This
494 // should be moved to Constructor.
495 BaseType *
496 Structure::m_leaf_match(const string &name, btp_stack *s)
497 {
498  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
499  if ((*i)->name() == name) {
500  if (s) {
501  DBG(cerr << "Pushing " << this->name() << endl);
502  s->push(static_cast<BaseType *>(this));
503  }
504  return *i;
505  }
506  if ((*i)->is_constructor_type()) {
507  BaseType *btp = (*i)->var(name, false, s);
508  if (btp) {
509  if (s) {
510  DBG(cerr << "Pushing " << this->name() << endl);
511  s->push(static_cast<BaseType *>(this));
512  }
513  return btp;
514  }
515  }
516  }
517 
518  return 0;
519 }
520 
521 // Breadth-first search for NAME. If NAME contains one or more dots (.)
522 // TODO The btp_stack is not needed since there are 'back pointers' in
523 // BaseType.
524 BaseType *
525 Structure::m_exact_match(const string &name, btp_stack *s)
526 {
527  // Look for name at the top level first.
528  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
529  if ((*i)->name() == name) {
530  if (s)
531  s->push(static_cast<BaseType *>(this));
532 
533  return *i;
534  }
535  }
536 
537  // If it was not found using the simple search, look for a dot and
538  // search the hierarchy.
539  string::size_type dot_pos = name.find("."); // zero-based index of `.'
540  if (dot_pos != string::npos) {
541  string aggregate = name.substr(0, dot_pos);
542  string field = name.substr(dot_pos + 1);
543 
544  BaseType *agg_ptr = var(aggregate);
545  if (agg_ptr) {
546  if (s)
547  s->push(static_cast<BaseType *>(this));
548 
549  return agg_ptr->var(field, true, s); // recurse
550  }
551  else
552  return 0; // qualified names must be *fully* qualified
553  }
554 
555  return 0;
556 }
557 #endif
558 #if 0
559 void
560 Structure::print_val(FILE *out, string space, bool print_decl_p)
561 {
562  ostringstream oss;
563  print_val(oss, space, print_decl_p);
564  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
565 }
566 
567 void
568 Structure::print_val(ostream &out, string space, bool print_decl_p)
569 {
570  if (print_decl_p) {
571  print_decl(out, space, false);
572  out << " = " ;
573  }
574 
575  out << "{ " ;
576  for (Vars_citer i = d_vars.begin(); i != d_vars.end();
577  i++, (void)(i != d_vars.end() && out << ", ")) {
578  (*i)->print_val(out, "", false);
579  }
580 
581  out << " }" ;
582 
583  if (print_decl_p)
584  out << ";\n" ;
585 }
586 #endif
587 
588 #if 0
589 bool
590 Structure::check_semantics(string &msg, bool all)
591 {
592  if (!BaseType::check_semantics(msg))
593  return false;
594 
595  bool status = true;
596 
597  if (!unique_names(d_vars, name(), type_name(), msg))
598  return false;
599 
600  if (all) {
601  for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
602  //assert(*i);
603  if (!(*i)->check_semantics(msg, true)) {
604  status = false;
605  goto exit;
606  }
607  }
608  }
609 
610 exit:
611  return status;
612 }
613 #endif
614 
623 void
624 Structure::dump(ostream &strm) const
625 {
626  strm << DapIndent::LMarg << "Structure::dump - ("
627  << (void *)this << ")" << endl ;
629  Constructor::dump(strm) ;
631 }
632 
633 } // namespace libdap
634 
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:421
virtual void set_in_selection(bool state)
Set the in_selection property.
Definition: Constructor.cc:805
static void UnIndent()
Definition: DapIndent.cc:51
Structure & operator=(const Structure &rhs)
Definition: Structure.cc:169
void m_duplicate(const Constructor &s)
Definition: Constructor.cc:69
virtual BaseType * ptr_duplicate()
Definition: Structure.cc:137
virtual BaseType * var(const string &name, bool exact_match=true, btp_stack *s=0)
btp_stack no longer needed; use back pointers (BaseType::get_parent())
Definition: Constructor.cc:242
Part
Names the parts of multi-section constructor data types.
Definition: Type.h:48
std::vector< BaseType * > d_vars
Definition: Constructor.h:49
virtual unsigned int width(bool constrained=false) const
Definition: Constructor.cc:224
virtual void intern_data(ConstraintEvaluator &eval, DDS &dds)
Definition: Constructor.cc:465
std::vector< BaseType * >::iterator Vars_iter
Definition: Constructor.h:62
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:125
Structure(const string &n)
Definition: Structure.cc:109
virtual BaseType * transform_to_dap4(D4Group *root, Constructor *container)
Definition: Structure.cc:152
BaseType * transform_to_dap4(D4Group *root, Constructor *dest)
DAP2 to DAP4 transform.
Definition: Constructor.cc:137
virtual void add_var_nocopy(BaseType *bt, Part part=nil)
Definition: Constructor.cc:407
Holds a structure (aggregate) type.
Definition: Structure.h:83
virtual void add_var(BaseType *bt, Part part=nil)
Definition: Constructor.cc:382
virtual void set_in_selection(bool state)
Definition: BaseType.cc:624
virtual int element_count(bool leaves=false)
Count the members of constructor types.
Definition: Constructor.cc:169
virtual void set_parent(BaseType *parent)
Definition: BaseType.cc:639
virtual void set_leaf_sequence(int level=1)
Traverse Structure, set Sequence leaf nodes.
Definition: Structure.cc:252
A class for software fault reporting.
Definition: InternalErr.h:64
string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:299
#define DBG(x)
Definition: debug.h:58
string type_name() const
Returns the type of the class instance as a string.
Definition: BaseType.cc:320
virtual void set_send_p(bool state)
Definition: BaseType.cc:498
static void Indent()
Definition: DapIndent.cc:45
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
Move data to the net.
Definition: Constructor.cc:479
virtual bool read()
simple implementation of read that iterates through vars and calls read on them
Definition: Constructor.cc:451
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:454
virtual bool check_semantics(string &msg, bool all=false)
Compare an object's current state with the semantics of its type.
Definition: Constructor.cc:763
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Constructor.cc:823
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
Receive data from the net.
Definition: Constructor.cc:514
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: Constructor.cc:624
virtual unsigned int val2buf(void *, bool)
Loads class data.
Definition: Constructor.h:115
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:261
BaseType * m_leaf_match(const string &name, btp_stack *s=0)
Definition: Constructor.cc:269
std::vector< BaseType * >::const_iterator Vars_citer
Definition: Constructor.h:61
virtual BaseType * ptr_duplicate()=0
string www2id(const string &in, const string &escape, const string &except)
Definition: escaping.cc:220
virtual unsigned int buf2val(void **)
Reads the class data.
Definition: Constructor.h:118
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:80
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
virtual void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
Definition: Constructor.cc:600
virtual bool is_linear()
Check to see whether this variable can be printed simply.
Definition: Structure.cc:200
Vars_iter var_begin()
Definition: Constructor.cc:331
BaseType * m_exact_match(const string &name, btp_stack *s=0)
Definition: Constructor.cc:296
bool unique_names(vector< BaseType * > l, const string &var_name, const string &type_name, string &msg)
Definition: util.cc:491
Vars_iter var_end()
Definition: Constructor.cc:339
virtual void del_var(const string &name)
Definition: Constructor.cc:423
virtual void set_send_p(bool state)
Definition: Constructor.cc:183
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Structure.cc:624
virtual ~Structure()
Definition: Structure.cc:132
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: Constructor.cc:193
virtual bool check_semantics(string &msg, bool all=false)
Compare an object's current state with the semantics of its type.
Definition: BaseType.cc:1111