OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
RenamedArrayWrapper.cc
Go to the documentation of this file.
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 #include "RenamedArrayWrapper.h"
30 
31 #include <AttrTable.h>
32 #include <BaseType.h>
33 #include <DDS.h>
34 #include <dods-datatypes.h>
35 #include <Marshaller.h>
36 #include <UnMarshaller.h>
37 #include "NCMLDebug.h"
38 #include "NCMLUtil.h"
39 #include <sstream>
40 #include <vector>
41 
42 using namespace libdap;
43 using std::ostringstream;
44 using std::string;
45 
46 
47 namespace ncml_module
48 {
49  RenamedArrayWrapper::RenamedArrayWrapper()
50  : Array("", 0)
51  , _pArray(0)
52  , _orgName("")
53  {
54  }
55 
57  : Array(proto) // I think we need to do this for constraints
58  , _pArray(0)
59  , _orgName(proto._orgName)
60  {
61  copyLocalRepFrom(proto);
62  }
63 
64  RenamedArrayWrapper::RenamedArrayWrapper(libdap::Array* toBeWrapped)
65  :Array(*toBeWrapped) // ugh, this will copy a lot of stuff we might not want to copy either, but seems safest.
66  , _pArray(toBeWrapped)
67  , _orgName("")
68  {
69  NCML_ASSERT_MSG(_pArray, "RenamedArrayWrapper(): expected non-null Array to wrap!!");
70  _orgName = toBeWrapped->name();
71  set_read_p(false); // force it to reload if we need to
72  }
73 
75  {
76  destroy(); // local vars
77  }
78 
81  {
82  return new RenamedArrayWrapper(*this);
83  }
84 
87  {
88  if (&rhs == this)
89  {
90  return *this;
91  }
92  destroy();
93  copyLocalRepFrom(rhs);
94  return *this;
95  }
96 
99 
100  void
101  RenamedArrayWrapper::add_constraint(Dim_iter i, int start, int stride, int stop)
102  {
103  // Make sure the dimensions all match before we add and resync.
104  syncConstraints();
105  Array::add_constraint(i, start, stride, stop);
106  syncConstraints();
107  }
108 
109  void
111  {
112  // must keep our copy and the wrappee in sync since
113  // they could be used at both levels.
114  Array::reset_constraint();
115  syncConstraints(); // make sure dims all match
116  _pArray->reset_constraint();
117  }
118 
119  void
121  {
122  Array::clear_constraint();
123  syncConstraints(); // make dims match
124  _pArray->clear_constraint();
125  }
126 
127  string
129  {
130  return const_cast<const RenamedArrayWrapper*>(this)->toString();
131  }
132 
133  string
135  {
136  ostringstream oss;
137  oss << "RenamedArrayWrapper(" << this << "): " << endl;
138  oss << "\t_pArray=" << ((_pArray)?(_pArray->toString()):("NULL")) << endl;
139  return oss.str();
140  }
141 
142  void
143  RenamedArrayWrapper::dump(std::ostream &strm) const
144  {
145  strm << toString();
146  }
147 
148  bool
150  {
151  return _pArray->is_simple_type();
152  }
153 
154  bool
156  {
157  return _pArray->is_vector_type();
158  }
159 
160  bool
162  {
163  return _pArray->is_constructor_type();
164  }
165 
166  bool
168  {
169  return _pArray->synthesized_p();
170  }
171 
172  void
174  {
175  // keep us in sync, why not.
176  BaseType::set_synthesized_p(state);
177  _pArray->set_synthesized_p(state);
178  }
179 
180  int
181  RenamedArrayWrapper::element_count(bool leaves /* = false */)
182  {
183  return _pArray->element_count(leaves);
184  }
185 
186  bool
188  {
189  return _pArray->read_p();
190  }
191 
192  void
194  {
195  BaseType::set_read_p(state);
196  _pArray->set_read_p(state);
197  }
198 
199  bool
201  {
202  return _pArray->send_p();
203  }
204 
205  void
207  {
208  BaseType::set_send_p(state);
209  _pArray->set_send_p(state);
210  }
211 
213  AttrTable&
215  {
216  return _pArray->get_attr_table();
217  }
218 
219  void
221  {
222  _pArray->set_attr_table(at);
223  }
224 
225  bool
227  {
228  return _pArray->is_in_selection();
229  }
230 
231  void
233  {
234  BaseType::set_in_selection(state);
235  _pArray->set_in_selection(state);
236  }
237 
239  void
241  {
242  BaseType::set_parent(parent);
243  _pArray->set_parent(parent);
244  }
245 
246  BaseType*
248  {
249  return _pArray->get_parent();
250  }
251 
252  BaseType*
253  RenamedArrayWrapper::var(const string &name /* = "" */,
254  bool exact_match /* = true */,
255  btp_stack *s /* = 0 */)
256  {
257  return _pArray->var(name, exact_match, s);
258  }
259 
260  BaseType*
261  RenamedArrayWrapper::var(const string &name, btp_stack &s)
262  {
263  return _pArray->var(name, s);
264  }
265 
266  void
267  RenamedArrayWrapper::add_var(BaseType *bt, Part part /* = nil */)
268  {
269  _pArray->add_var(bt, part);
270  }
271 
272  bool
273  RenamedArrayWrapper::check_semantics(string &msg, bool all /* = false*/)
274  {
275  return _pArray->check_semantics(msg, all);
276  }
277 
278  bool
279  RenamedArrayWrapper::ops(BaseType *b, int op)
280  {
281  return _pArray->ops(b, op);
282  }
283 
284 #if FILE_METHODS // from libdap/BaseType.h, whether to include FILE* methods
285  void
287  string space /* = " "*/,
288  bool print_semi /* = true*/,
289  bool constraint_info /* = false*/,
290  bool constrained /* = false */)
291  {
292  syncConstraints();
293  withNewName();
294  _pArray->print_decl(out, space, print_semi, constraint_info, constrained);
295  withOrgName();
296  }
297 
298  void
300  string space /* = " "*/,
301  bool constrained /* = false */)
302  {
303  syncConstraints();
304  withNewName();
305  _pArray->print_xml(out, space, constrained);
306  withOrgName();
307  }
308 
309 
310  void
312  string space /* = ""*/,
313  bool print_decl_p /* = true*/)
314  {
315  syncConstraints();
316  withNewName();
317  print_val(out, space, print_decl_p);
318  withOrgName();
319  }
320 #endif // FILE_METHODS
321 
322  void
324  string space /* = " "*/,
325  bool print_semi /* = true*/,
326  bool constraint_info /* = false*/,
327  bool constrained /* = false*/)
328  {
329  syncConstraints();
330  withNewName();
331  _pArray->print_decl(out, space, print_semi, constraint_info, constrained);
332  withOrgName();
333  }
334 
335  void
337  string space /* = " " */,
338  bool constrained /* = false */)
339  {
340  syncConstraints();
341  withNewName();
342  _pArray->print_xml(out, space, constrained);
343  withOrgName();
344  }
345 
346  unsigned int
347  RenamedArrayWrapper::width(bool constrained)
348  {
349  syncConstraints();
350  return _pArray->width(constrained);
351  }
352 
353  unsigned int
355  {
356  syncConstraints();
357  return _pArray->buf2val(val);
358  }
359 
360  unsigned int
361  RenamedArrayWrapper::val2buf(void *val, bool reuse /* = false */)
362  {
363  syncConstraints();
364  return _pArray->val2buf(val, reuse);
365  }
366 
367  void
369  string space /* = ""*/,
370  bool print_decl_p /* = true*/)
371  {
372  syncConstraints();
373  withNewName();
374  print_val(out, space, print_decl_p);
375  withOrgName();
376  }
377 
378  bool
379  RenamedArrayWrapper::set_value(dods_byte *val, int sz)
380  {
381  syncConstraints();
382  return _pArray->set_value(val, sz);
383  }
384 
385  bool
386  RenamedArrayWrapper::set_value(vector<dods_byte> &val, int sz)
387  {
388  syncConstraints();
389  return _pArray->set_value(val, sz);
390  }
391 
392  bool
393  RenamedArrayWrapper::set_value(dods_int16 *val, int sz)
394  {
395  syncConstraints();
396  return _pArray->set_value(val, sz);
397  }
398 
399  bool
400  RenamedArrayWrapper::set_value(vector<dods_int16> &val, int sz)
401  {
402  syncConstraints();
403  return _pArray->set_value(val, sz);
404  }
405 
406  bool
407  RenamedArrayWrapper::set_value(dods_uint16 *val, int sz)
408  {
409  syncConstraints();
410  return _pArray->set_value(val, sz);
411  }
412 
413  bool
414  RenamedArrayWrapper::set_value(vector<dods_uint16> &val, int sz)
415  {
416  syncConstraints();
417  return _pArray->set_value(val, sz);
418  }
419 
420  bool
421  RenamedArrayWrapper::set_value(dods_int32 *val, int sz)
422  {
423  syncConstraints();
424  return _pArray->set_value(val, sz);
425  }
426 
427  bool
428  RenamedArrayWrapper::set_value(vector<dods_int32> &val, int sz)
429  {
430  syncConstraints();
431  return _pArray->set_value(val, sz);
432  }
433 
434  bool
435  RenamedArrayWrapper::set_value(dods_uint32 *val, int sz)
436  {
437  syncConstraints();
438  return _pArray->set_value(val, sz);
439  }
440 
441  bool
442  RenamedArrayWrapper::set_value(vector<dods_uint32> &val, int sz)
443  {
444  syncConstraints();
445  return _pArray->set_value(val, sz);
446  }
447 
448  bool
449  RenamedArrayWrapper::set_value(dods_float32 *val, int sz)
450  {
451  syncConstraints();
452  return _pArray->set_value(val, sz);
453  }
454 
455  bool
456  RenamedArrayWrapper::set_value(vector<dods_float32> &val, int sz)
457  {
458  syncConstraints();
459  return _pArray->set_value(val, sz);
460  }
461 
462  bool
463  RenamedArrayWrapper::set_value(dods_float64 *val, int sz)
464  {
465  syncConstraints();
466  return _pArray->set_value(val, sz);
467  }
468 
469  bool
470  RenamedArrayWrapper::set_value(vector<dods_float64> &val, int sz)
471  {
472  syncConstraints();
473  return _pArray->set_value(val, sz);
474  }
475 
476  bool
477  RenamedArrayWrapper::set_value(string *val, int sz)
478  {
479  syncConstraints();
480  return _pArray->set_value(val, sz);
481  }
482 
483  bool
484  RenamedArrayWrapper::set_value(vector<string> &val, int sz)
485  {
486  syncConstraints();
487  return _pArray->set_value(val, sz);
488  }
489 
490  void
491  RenamedArrayWrapper::value(dods_byte *b) const
492  {
493  syncConstraints();
494  _pArray->value(b);
495  }
496 
497  void
498  RenamedArrayWrapper::value(dods_int16 *b) const
499  {
500  syncConstraints();
501  _pArray->value(b);
502  }
503 
504  void
505  RenamedArrayWrapper::value(dods_uint16 *b) const
506  {
507  syncConstraints();
508  _pArray->value(b);
509  }
510 
511  void
512  RenamedArrayWrapper::value(dods_int32 *b) const
513  {
514  syncConstraints();
515  _pArray->value(b);
516  }
517 
518  void
519  RenamedArrayWrapper::value(dods_uint32 *b) const
520  {
521  syncConstraints();
522  _pArray->value(b);
523  }
524 
525  void
526  RenamedArrayWrapper::value(dods_float32 *b) const
527  {
528  syncConstraints();
529  _pArray->value(b);
530  }
531 
532  void
533  RenamedArrayWrapper::value(dods_float64 *b) const
534  {
535  syncConstraints();
536  _pArray->value(b);
537  }
538 
539  void
540  RenamedArrayWrapper::value(vector<string> &b) const
541  {
542  syncConstraints();
543  _pArray->value(b);
544  }
545 
546  void*
548  {
549  syncConstraints();
550  return _pArray->value();
551  }
552 
553  // DO THE REAL WORK
554  bool
556  {
557  // Read using the old name....
558  withOrgName();
559  bool ret = _pArray->read();
560  set_read_p(true); // get us too
561  withNewName();
562  return ret;
563  }
564 
565  void
566  RenamedArrayWrapper::intern_data(ConstraintEvaluator &eval, DDS &dds)
567  {
568  syncConstraints();
569 
570  // Force the correct read to be called, as with serialize...
571  // If not read in, read it in with the orgName and these constraints.
572  if (!_pArray->read_p())
573  {
574  withOrgName();
575  _pArray->read();
576  set_read_p(true);
577  }
578 
579  // Now we're back to the new name for intern_data purposes.
580  withNewName();
581  _pArray->intern_data(eval, dds);
582  }
583 
584  bool
585  RenamedArrayWrapper::serialize(ConstraintEvaluator &eval, DDS &dds,
586  Marshaller &m, bool ce_eval /* = true */)
587  {
588  BESDEBUG("ncml", "RenamedArrayWrapper::serialize(): Doing the magic for renamed read()!!" << endl);
589  // Push them down if we need to.
590  syncConstraints();
591 
592  // If not read in, read it in with the orgName and these constraints.
593  if (!_pArray->read_p())
594  {
595  withOrgName();
596  _pArray->read();
597  set_read_p(true);
598  }
599 
600  // Now we're back to the new name for printing purposes.
601  withNewName();
602 
603  // So call the actual serialize, which should hopefully respect read_p() being set!!
604  return _pArray->serialize(eval, dds, m, ce_eval);
605  }
606 
607  bool
608  RenamedArrayWrapper::deserialize(UnMarshaller &um, DDS *dds, bool reuse /* = false */)
609  {
610  // I *think* this should work
611  syncConstraints();
612  return _pArray->deserialize(um, dds, reuse);
613  }
614 
615 
618 
619  void
620  RenamedArrayWrapper::copyLocalRepFrom(const RenamedArrayWrapper& proto)
621  {
622  if (&proto == this)
623  {
624  return;
625  }
626 
627  if (proto._pArray)
628  {
629  _pArray = dynamic_cast<libdap::Array*>(proto._pArray->ptr_duplicate());
630  }
631  _orgName = proto._orgName;
632  }
633 
634  void
635  RenamedArrayWrapper::destroy()
636  {
637  SAFE_DELETE(_pArray);
638  _orgName = "";
639  }
640 
641  void
642  RenamedArrayWrapper::withNewName()
643  {
644  NCMLUtil::setVariableNameProperly(_pArray, name());
645  }
646 
647  void
648  RenamedArrayWrapper::withOrgName()
649  {
650  NCMLUtil::setVariableNameProperly(_pArray, _orgName);
651  }
652 
653  void
654  RenamedArrayWrapper::syncConstraints()
655  {
656  // First see if the number of dimensions is correct. We may not need to bother with this,
657  // just constraint propagation
658  if (_pArray->dimensions() != dimensions())
659  {
660  THROW_NCML_INTERNAL_ERROR("RenamedArrayWrapper::syncConstraints(): dimensions() of this and wrapped array do not match!");
661  }
662 
663  // If they match, iterate the shape and set make sure the values are set.
664  Array::Dim_iter thisEndIt = dim_end();
665  Array::Dim_iter thisIt, wrapIt;
666  for (thisIt = dim_begin(), wrapIt = _pArray->dim_begin(); thisIt != thisEndIt; ++thisIt, ++wrapIt)
667  {
668  Array::dimension& thisDim = *thisIt;
669  Array::dimension& wrapDim = *wrapIt;
670  wrapDim = thisDim; // copy them!
671  }
672  // this calculates it's length fine, then set it to the wrapped
673  // since it has no way to know we changed the dimensions...
674  update_length(this->length());
675  _pArray->set_length(this->length());
676  NCML_ASSERT_MSG(this->length() == _pArray->length(), "RenamedArrayWrapper::syncConstraints(): length() of this and wrapped do not match!!");
677  }
678 }
virtual unsigned int width(bool constrained=false)
virtual BaseType * get_parent() const
virtual int element_count(bool leaves=false)
virtual unsigned int buf2val(void **val)
#define SAFE_DELETE(a)
Definition: NCMLUtil.h:64
virtual bool ops(BaseType *b, int op)
virtual void set_read_p(bool state)
An abstract superclass for NCMLArray that handles the non-parameterized functionality and allows u...
virtual void print_val(ostream &out, string space="", bool print_decl_p=true)
virtual void dump(ostream &strm) const
virtual void set_synthesized_p(bool state)
#define NCML_ASSERT_MSG(cond, msg)
Definition: NCMLDebug.h:83
virtual void set_in_selection(bool state)
static class NCMLUtil overview
RenamedArrayWrapper & operator=(const RenamedArrayWrapper &rhs)
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
virtual void add_var(BaseType *bt, Part part=nil)
virtual void set_send_p(bool state)
virtual void print_decl(ostream &out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
virtual void intern_data(ConstraintEvaluator &eval, DDS &dds)
static void setVariableNameProperly(libdap::BaseType *pVar, const std::string &name)
Currently BaseType::set_name only sets in BaseType.
Definition: NCMLUtil.cc:398
virtual bool check_semantics(string &msg, bool all=false)
virtual libdap::AttrTable & get_attr_table()
We don't keep our own...
virtual bool set_value(dods_byte *val, int sz)
#define THROW_NCML_INTERNAL_ERROR(msg)
Definition: NCMLDebug.h:61
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
virtual void set_parent(BaseType *parent)
Keep these in sync so it doesn't matter where we get the parent from...
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
virtual RenamedArrayWrapper * ptr_duplicate()
virtual void set_attr_table(const libdap::AttrTable &at)
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
virtual unsigned int val2buf(void *val, bool reuse=false)
virtual bool is_constructor_type() const
A Decorator Pattern for wrapping a libdap::Array in order to change its name efficiently in the face ...
virtual void print_xml(ostream &out, string space=" ", bool constrained=false)
virtual void add_constraint(Dim_iter i, int start, int stride, int stop)
Wrappers.