41#include "Constructor.h"
45#include "D4StreamMarshaller.h"
46#include "D4StreamUnMarshaller.h"
50#include "D4Attributes.h"
53#include "InternalErr.h"
65void Constructor::m_duplicate(
const Constructor &c) {
70 for (
auto var : c.d_vars) {
71 BaseType *btp =
var->ptr_duplicate();
72 btp->set_parent(
this);
73 d_vars.push_back(btp);
85 BaseType *d4_var = dest->var((*i)->name());
91 (*i)->transform_to_dap4(root , dest );
94 dest->attributes()->transform_to_dap4(get_attr_table());
95 dest->set_is_dap4(
true);
98string Constructor::FQN()
const {
99 if (get_parent() == 0)
101 else if (get_parent()->type() == dods_group_c)
102 return get_parent()->FQN() + name();
103 else if (get_parent()->type() == dods_array_c)
104 return get_parent()->FQN();
106 return get_parent()->FQN() +
"." + name();
109int Constructor::element_count(
bool leaves) {
111 return d_vars.size();
114 for (
auto var : d_vars) {
115 i +=
var->element_count(leaves);
121void Constructor::set_send_p(
bool state) {
122 for (
auto var : d_vars) {
123 var->set_send_p(state);
126 BaseType::set_send_p(state);
141 for (
auto var : d_vars) {
142 var->set_read_p(state);
145 BaseType::set_read_p(state);
159 for (
auto var : d_vars) {
162 sz +=
var->width(constrained);
164 sz +=
var->width(constrained);
179 for (
auto var : d_vars) {
182 sz +=
var->width_ll(constrained);
184 sz +=
var->width_ll(constrained);
195 return m_exact_match(n, s);
197 return m_leaf_match(n, s);
206 BaseType *btp = m_exact_match(name, &s);
210 return m_leaf_match(name, &s);
214BaseType *Constructor::m_leaf_match(
const string &name, btp_stack *s) {
215 for (
auto var : d_vars) {
216 if (
var->name() == name) {
218 s->push(
static_cast<BaseType *
>(
this));
222 if (
var->is_constructor_type()) {
223 BaseType *btp =
var->var(name,
false, s);
226 s->push(
static_cast<BaseType *
>(
this));
237BaseType *Constructor::m_exact_match(
const string &name, btp_stack *s) {
239 for (
auto var : d_vars) {
240 if (
var->name() == name) {
242 s->push(
static_cast<BaseType *
>(
this));
250 string::size_type dot_pos = name.find(
".");
251 if (dot_pos != string::npos) {
252 string aggregate = name.substr(0, dot_pos);
253 string field = name.substr(dot_pos + 1);
255 BaseType *agg_ptr =
var(aggregate);
258 s->push(
static_cast<BaseType *
>(
this));
260 return agg_ptr->var(field,
true, s);
304 throw InternalErr(__FILE__, __LINE__,
"The BaseType parameter cannot be null.");
306 if (i < 0 || i >= (
int)(d_vars.size()))
307 throw InternalErr(__FILE__, __LINE__,
"The index must be within the variable vector range..");
309 bt->set_parent(
this);
326 throw InternalErr(__FILE__, __LINE__,
"The BaseType parameter cannot be null.");
342 throw InternalErr(__FILE__, __LINE__,
"The BaseType parameter cannot be null.");
344 bt->set_parent(
this);
345 d_vars.push_back(bt);
360 auto to_remove = stable_partition(d_vars.begin(), d_vars.end(), [n](
BaseType *btp) { return btp->name() != n; });
361 for_each(to_remove, d_vars.end(), [](
BaseType *btp) { delete btp; });
362 d_vars.erase(to_remove, d_vars.end());
388 for (
auto var : d_vars) {
393 BaseType::set_read_p(
true);
401 throw Error(
string(
"A method usable only with DAP2 variables was called on a DAP4 variable (")
409 for (
auto var : d_vars) {
411 var->intern_data(eval, dds);
420 if (ce_eval && !eval.eval_selection(dds,
dataset()))
423 for (
auto var : d_vars) {
426 XDRStreamMarshaller *sm =
dynamic_cast<XDRStreamMarshaller *
>(&m);
427 if (sm && sm->checksums() &&
var->type() != dods_structure_c &&
var->type() != dods_grid_c)
428 sm->reset_checksum();
430 var->serialize(eval, dds, m,
false);
432 if (sm && sm->checksums() &&
var->type() != dods_structure_c &&
var->type() != dods_grid_c)
437 var->serialize(eval, dds, m,
true);
446 for (
auto var : d_vars) {
447 var->deserialize(um, dds, reuse);
453void Constructor::compute_checksum(Crc32 &) {
454 throw InternalErr(__FILE__, __LINE__,
"Computing a checksum alone is not supported for Constructor types.");
457void Constructor::intern_data() {
461 for (
auto var : d_vars) {
493 for (
auto var : d_vars) {
495 var->serialize(m, dmr, filter);
501 for (
auto var : d_vars) {
502 var->deserialize(um, dmr);
506void Constructor::print_decl(FILE *out,
string space,
bool print_semi,
bool constraint_info,
bool constrained) {
508 print_decl(oss, space, print_semi, constraint_info, constrained);
509 fwrite(oss.str().data(),
sizeof(
char), oss.str().length(), out);
512void Constructor::print_decl(ostream &out,
string space,
bool print_semi,
bool constraint_info,
bool constrained) {
513 if (constrained && !
send_p())
517 for (
auto var : d_vars) {
518 var->print_decl(out, space +
" ",
true, constraint_info, constrained);
520 out << space <<
"} " <<
id2www(name());
522 if (constraint_info) {
524 out <<
": Send True";
526 out <<
": Send False";
533void Constructor::print_val(FILE *out,
string space,
bool print_decl_p) {
535 print_val(oss, space, print_decl_p);
536 fwrite(oss.str().data(),
sizeof(
char), oss.str().length(), out);
539void Constructor::print_val(ostream &out,
string space,
bool print_decl_p) {
541 print_decl(out, space,
false);
546 for (Vars_citer i = d_vars.begin(), e = d_vars.end(); i != e; i++, (
void)(i != e && out <<
", ")) {
547 (*i)->print_val(out,
"",
false);
561 print_xml_writer(xml, constrained);
562 fwrite(xml.get_doc(),
sizeof(
char), xml.get_doc_size(), out);
570 print_xml_writer(xml, constrained);
571 out << xml.get_doc();
574void Constructor::print_xml_writer(
XMLWriter &xml,
bool constrained) {
575 if (constrained && !
send_p())
578 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
type_name().c_str()) < 0)
582 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar *)
"name", (
const xmlChar *)name().c_str()) < 0)
583 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
587 if (!is_dap4() && get_attr_table().get_size() > 0)
588 get_attr_table().print_xml_writer(xml);
591 for_each(d_vars.begin(), d_vars.end(),
592 [&xml, constrained](
BaseType *btp) { btp->print_xml_writer(xml, constrained); });
595 attributes()->print_dap4(xml);
597 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
601void Constructor::print_dap4(XMLWriter &xml,
bool constrained) {
602 if (constrained && !
send_p())
605 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
type_name().c_str()) < 0)
606 throw InternalErr(__FILE__, __LINE__,
"Could not write " +
type_name() +
" element");
609 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar *)
"name", (
const xmlChar *)name().c_str()) < 0)
610 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
613 for_each(d_vars.begin(), d_vars.end(),
614 [&xml, constrained](
BaseType *btp) { btp->print_dap4(xml, constrained); });
616 attributes()->print_dap4(xml);
618 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
619 throw InternalErr(__FILE__, __LINE__,
"Could not end " +
type_name() +
" element");
622bool Constructor::check_semantics(
string &msg,
bool all) {
623 if (!BaseType::check_semantics(msg))
626 if (!unique_names(d_vars, name(),
type_name(), msg))
630 for (
auto var : d_vars) {
631 if (!
var->check_semantics(msg,
true)) {
657 for (
auto var : d_vars) {
658 var->set_in_selection(state);
661 BaseType::set_in_selection(state);
664void Constructor::transfer_attributes(
AttrTable *at_container) {
668 BaseType::transfer_attributes(at_container);
669 for (
auto var : d_vars) {
670 var->transfer_attributes(at);
675AttrTable *Constructor::make_dropped_vars_attr_table(vector<BaseType *> *dropped_vars) {
676 AttrTable *dv_table =
nullptr;
677 if (!dropped_vars->empty()) {
678 dv_table =
new AttrTable;
679 dv_table->set_name(
"dap4:dropped_members");
681 vector<BaseType *>::iterator dvIter = dropped_vars->begin();
682 vector<BaseType *>::iterator dvEnd = dropped_vars->end();
684 for (; dvIter != dvEnd; dvIter++, i++) {
685 BaseType *bt = (*dvIter);
687 AttrTable *bt_attr_table =
new AttrTable(bt->get_attr_table());
688 bt_attr_table->set_name(bt->name());
691 if (bt->is_vector_type()) {
692 Array *array =
dynamic_cast<Array *
>(bt);
694 type_name = array->prototype()->type_name();
697 for (; d_iter < end; d_iter++) {
699 ostringstream dim_size;
700 dim_size << (*d_iter).size;
701 bt_attr_table->append_attr(
"array_dimensions",
AttrType_to_String(Attr_uint32), dim_size.str());
706 bt_attr_table->append_attr(
"dap4:type",
"String",
type_name);
707 dv_table->append_container(bt_attr_table, bt_attr_table->get_name());
723 bool has_projected_dap4 =
false;
725 has_projected_dap4 = attributes()->has_dap4_types(FQN(), inventory);
727 has_projected_dap4 |=
var->is_dap4_projected(inventory);
730 return has_projected_dap4;
742 strm << DapIndent::LMarg <<
"Constructor::dump - (" << (
void *)
this <<
")" << endl;
744 BaseType::dump(strm);
745 strm << DapIndent::LMarg <<
"vars: " << endl;
748 for (
auto var : d_vars) {
752 DapIndent::UnIndent();
753 DapIndent::UnIndent();
std::vector< dimension >::iterator Dim_iter
Contains the attributes for a dataset.
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Evaluate a constraint expression.
Vars_iter get_vars_iter(int i)
BaseType * var(const string &name, bool exact_match=true, btp_stack *s=nullptr) override
btp_stack no longer needed; use back pointers (BaseType::get_parent())
void set_var_index(BaseType *bt, int i)
Set the ith element of d_vars to a variable object.
void add_var(BaseType *bt, Part part=nil) override
void print_xml(ostream &out, string space=" ", bool constrained=false) override
void set_read_p(bool state) override
Set the 'read_p' property for the Constructor and its members.
bool is_dap4_projected(std::vector< std::string > &inventory) override
bool read() override
Read the elements of Constructor marked for transmission.
void set_in_selection(bool state) override
Set the in_selection property.
void add_var_nocopy(BaseType *bt, Part part=nil) override
const vector< BaseType * > & variables() const
unsigned int width(bool constrained=false) const override
BaseType * get_var_index(int i)
virtual bool is_linear()
Check to see whether this variable can be printed simply.
virtual void del_var(const string &name)
Remove an element from a Constructor.
void dump(ostream &strm) const override
dumps information about this object
int64_t width_ll(bool constrained=false) const override
Get the width of the Constructor's fields.
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
Read data from the stream made by D4StreamMarshaller.
A class for error processing.
A class for software fault reporting.
abstract base class used to marshal/serialize dap data objects
abstract base class used to unmarshall/deserialize dap data objects
top level DAP object to house generic methods
virtual bool read_p()
Has this variable been read?
virtual string type_name() const
Returns the type of the class instance as a string.
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=nullptr)
Returns a pointer to a member of a constructor class.
string www2id(const string &in, const string &escape, const string &except)
virtual bool send_p()
Should this variable be sent?
string AttrType_to_String(const AttrType at)
Part
Names the parts of multi-section constructor data types.
virtual string dataset() const
Returns the name of the dataset used to create this instance.
string id2www(string in, const string &allowable)