54 #include "Structure.h" 58 #include "Marshaller.h" 59 #include "UnMarshaller.h" 63 #include "InternalErr.h" 68 #include "InternalErr.h" 71 #include "D4Attributes.h" 72 #include "D4Sequence.h" 74 #include "Constructor.h" 77 #undef CLEAR_LOCAL_DATA 83 static const unsigned char end_of_sequence = 0xA5;
84 static const unsigned char start_of_instance = 0x5A;
88 void Sequence::m_duplicate(
const Sequence &s)
90 DBG(cerr <<
"In Sequence::m_duplicate" << endl);
92 d_row_number = s.d_row_number;
93 d_starting_row_number = s.d_starting_row_number;
94 d_ending_row_number = s.d_ending_row_number;
95 d_row_stride = s.d_row_stride;
96 d_leaf_sequence = s.d_leaf_sequence;
97 d_unsent_data = s.d_unsent_data;
98 d_wrote_soi = s.d_wrote_soi;
99 d_top_most = s.d_top_most;
101 Sequence &cs =
const_cast<Sequence &
>(s);
104 for (vector<BaseTypeRow *>::iterator rows_iter = cs.d_values.begin(); rows_iter != cs.d_values.end(); rows_iter++) {
111 for (BaseTypeRow::iterator bt_row_iter = src_bt_row_ptr->begin(); bt_row_iter != src_bt_row_ptr->end();
113 BaseType *src_bt_ptr = *bt_row_iter;
114 BaseType *dest_bt_ptr = src_bt_ptr->ptr_duplicate();
115 dest_bt_row_ptr->push_back(dest_bt_ptr);
118 d_values.push_back(dest_bt_row_ptr);
122 static void write_end_of_sequence(Marshaller &m)
124 m.put_opaque((
char *) &end_of_sequence, 1);
127 static void write_start_of_instance(Marshaller &m)
129 m.put_opaque((
char *) &start_of_instance, 1);
132 static unsigned char read_marker(UnMarshaller &um)
134 unsigned char marker;
135 um.get_opaque((
char *) &marker, 1);
140 static bool is_start_of_instance(
unsigned char marker)
142 return (marker == start_of_instance);
145 static bool is_end_of_sequence(
unsigned char marker)
147 return (marker == end_of_sequence);
160 Sequence::Sequence(
const string &n) :
161 Constructor(n, dods_sequence_c), d_row_number(-1), d_starting_row_number(-1), d_row_stride(1), d_ending_row_number(
162 -1), d_unsent_data(false), d_wrote_soi(false), d_leaf_sequence(false), d_top_most(false)
177 Constructor(n, d, dods_sequence_c), d_row_number(-1), d_starting_row_number(-1),
178 d_row_stride(1), d_ending_row_number(-1), d_unsent_data(false),
179 d_wrote_soi(false), d_leaf_sequence(false), d_top_most(false)
223 static inline void delete_bt(
BaseType *bt_ptr)
229 static inline void delete_rows(
BaseTypeRow *bt_row_ptr)
231 for_each(bt_row_ptr->begin(), bt_row_ptr->end(), delete_bt);
237 Sequence::~Sequence()
244 if (!d_values.empty()) {
245 for_each(d_values.begin(), d_values.end(), delete_rows);
253 Sequence::operator=(
const Sequence &rhs)
255 if (
this == &rhs)
return *
this;
278 for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
279 oss << (*i)->toString();
290 bool seq_found =
false;
291 for (Vars_iter iter = d_vars.begin(); linear && iter != d_vars.end(); iter++) {
292 if ((*iter)->type() == dods_sequence_c) {
303 else if ((*iter)->type() == dods_structure_c) {
308 linear = (*iter)->is_simple_type();
322 if (row >= d_values.size())
return 0;
323 return d_values[row];
361 if (!bt_row_ptr)
return 0;
363 BaseTypeRow::iterator bt_row_iter = bt_row_ptr->begin();
364 BaseTypeRow::iterator bt_row_end = bt_row_ptr->end();
365 while (bt_row_iter != bt_row_end && (*bt_row_iter)->name() !=
name)
368 if (bt_row_iter == bt_row_end)
383 if (!bt_row_ptr)
return 0;
385 if (i >= bt_row_ptr->size())
return 0;
387 return (*bt_row_ptr)[i];
414 int Sequence::number_of_rows()
const 416 return d_values.size();
438 if ((*i)->type() == dods_sequence_c)
496 DBG2(cerr <<
"Entering Sequence::read_row for " <<
name() <<
", row number " << row <<
", current row " << d_row_number << endl);
497 if (row < d_row_number)
throw InternalErr(
"Trying to back up inside a sequence!");
499 if (row == d_row_number) {
500 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() << endl);
505 while (!eof && d_row_number < row) {
526 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() <<
" with eof: " << eof << endl);
537 inline bool Sequence::is_end_of_rows(
int i)
539 return ((d_ending_row_number == -1) ?
false : (i > d_ending_row_number));
607 if (is_leaf_sequence())
608 status = serialize_leaf(dds, eval, m, ce_eval);
610 status = serialize_parent_part_one(dds, eval, m);
621 DBG2(cerr <<
"Entering serialize_parent_part_one for " <<
name() << endl);
623 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
629 bool status =
read_row(i, dds, eval,
false);
630 DBG2(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
632 while (status && !is_end_of_rows(i)) {
639 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
649 if ((*iter)->send_p() && (*iter)->type() == dods_sequence_c) (*iter)->serialize(eval, dds, m);
654 status =
read_row(i, dds, eval,
false);
655 DBG(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
663 if (d_top_most || d_wrote_soi) {
664 DBG(cerr <<
"Writing End of Sequence marker" << endl);
665 write_end_of_sequence(m);
684 DBG(cerr <<
"Entering serialize_parent_part_two for " <<
name() << endl);
687 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
690 DBG(cerr <<
"Writing Start of Instance marker" << endl);
692 write_start_of_instance(m);
695 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
697 DBG(cerr <<
"Sequence::serialize_parent_part_two(), serializing " 698 << (*iter)->name() << endl);
699 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
700 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
701 (*iter)->serialize(eval, dds, m,
false);
705 d_unsent_data =
false;
713 DBG(cerr <<
"Entering Sequence::serialize_leaf for " <<
name() << endl);
714 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
718 bool status =
read_row(i, dds, eval, ce_eval);
719 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
731 if (status && !is_end_of_rows(i)) {
733 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
737 while (status && !is_end_of_rows(i)) {
740 DBG(cerr <<
"Writing Start of Instance marker" << endl);
742 write_start_of_instance(m);
745 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
746 DBG(cerr <<
"Sequence::serialize_leaf(), serializing " 747 << (*iter)->name() << endl);
748 if ((*iter)->send_p()) {
749 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
750 (*iter)->serialize(eval, dds, m,
false);
756 status =
read_row(i, dds, eval, ce_eval);
757 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
762 if (d_wrote_soi || d_top_most) {
763 DBG(cerr <<
"Writing End of Sequence marker" << endl);
764 write_end_of_sequence(m);
794 DBG(cerr <<
"Sequence::intern_data - for " <<
name() << endl); DBG2(cerr <<
" intern_data, values: " << &d_values << endl);
799 sequence_values_stack_t sequence_values_stack;
801 sequence_values_stack.push(&d_values);
803 intern_data_private(eval, dds, sequence_values_stack);
806 void Sequence::intern_data_private(
ConstraintEvaluator &eval,
DDS &dds, sequence_values_stack_t &sequence_values_stack)
808 DBG(cerr <<
"Entering intern_data_private for " <<
name() << endl);
810 if (is_leaf_sequence())
811 intern_data_for_leaf(dds, eval, sequence_values_stack);
813 intern_data_parent_part_one(dds, eval, sequence_values_stack);
817 sequence_values_stack_t & sequence_values_stack)
819 DBG(cerr <<
"Entering intern_data_parent_part_one for " <<
name() << endl);
827 bool status =
read_row(i, dds, eval,
false);
835 SequenceValues::size_type orig_stack_size = sequence_values_stack.size();
840 if ((*iter)->send_p()) {
841 switch ((*iter)->type()) {
842 case dods_sequence_c:
843 static_cast<Sequence&
>(**iter).intern_data_private(eval, dds, sequence_values_stack);
847 (*iter)->intern_data(eval, dds);
855 status =
read_row(i, dds, eval,
false);
865 if (sequence_values_stack.size() > orig_stack_size) {
866 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
867 <<
") off stack; size: " << sequence_values_stack.size() << endl);
868 sequence_values_stack.pop();
871 DBG(cerr <<
"Leaving intern_data_parent_part_one for " <<
name() << endl);
875 sequence_values_stack_t &sequence_values_stack)
877 DBG(cerr <<
"Entering intern_data_parent_part_two for " <<
name() << endl);
880 if (btp && btp->
type() == dods_sequence_c) {
881 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
884 DBG2(cerr <<
" stack size: " << sequence_values_stack.size() << endl);
886 DBG2(cerr <<
" using values = " << (
void *)values << endl);
894 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
895 row_data->push_back((*iter)->ptr_duplicate());
897 else if ((*iter)->send_p()) {
901 throw InternalErr(__FILE__, __LINE__,
"Expected a Sequence.");
903 row_data->push_back(tmp);
904 DBG2(cerr <<
" pushing d_values of " << tmp->
name()
905 <<
" (" << &(tmp->d_values)
906 <<
") on stack; size: " << sequence_values_stack.size()
911 sequence_values_stack.push(&(tmp->d_values));
915 DBG2(cerr <<
" pushing values for " <<
name()
916 <<
" to " << values << endl);
917 values->push_back(row_data);
921 DBG(cerr <<
"Leaving intern_data_parent_part_two for " <<
name() << endl);
924 void Sequence::intern_data_for_leaf(
DDS &dds,
ConstraintEvaluator &eval, sequence_values_stack_t &sequence_values_stack)
926 DBG(cerr <<
"Entering intern_data_for_leaf for " <<
name() << endl);
930 DBG2(cerr <<
" reading row " << i << endl);
931 bool status =
read_row(i, dds, eval,
true);
932 DBG2(cerr <<
" status: " << status << endl); DBG2(cerr <<
" ending row number: " <<
get_ending_row_number() << endl);
936 if (btp && btp->
type() == dods_sequence_c) {
940 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
947 DBG2(cerr <<
" using values = " << values << endl);
955 if ((*iter)->send_p()) {
956 row_data->push_back((*iter)->ptr_duplicate());
960 DBG2(cerr <<
" pushing values for " <<
name()
961 <<
" to " << values << endl);
963 values->push_back(row_data);
967 status =
read_row(i, dds, eval,
true);
970 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
971 <<
") off stack; size: " << sequence_values_stack.size() << endl);
972 sequence_values_stack.pop();
975 DBG(cerr <<
"Leaving intern_data_for_leaf for " <<
name() << endl);
1003 if (!dd)
throw InternalErr(
"Expected argument 'dds' to be a DataDDS!");
1005 DBG2(cerr <<
"Reading from server/protocol version: " 1006 << dd->get_protocol_major() <<
"." << dd->get_protocol_minor()
1010 if (dd->get_protocol_major() < 2) {
1012 string(
"The protocl version (") + dd->get_protocol()
1013 +
") indicates that this\nis an old server which may not correctly transmit Sequence variables.\nContact the server administrator.");
1018 unsigned char marker = read_marker(um);
1019 if (is_end_of_sequence(marker))
1021 else if (is_start_of_instance(marker)) {
1023 DBG2(cerr <<
"Reading row " << d_row_number <<
" of " 1027 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1030 DBG2(cerr <<
"Deserialized " << bt_ptr->
name() <<
" (" 1031 << bt_ptr <<
") = "); DBG2(bt_ptr->
print_val(stderr,
""));
1032 bt_row_ptr->push_back(bt_ptr);
1035 d_values.push_back(bt_row_ptr);
1038 throw Error(
"I could not read the expected Sequence data stream marker!");
1059 return d_starting_row_number;
1074 return d_row_stride;
1090 return d_ending_row_number;
1103 if (stop < start)
throw Error(malformed_expr,
"Starting row number must precede the ending row number.");
1105 d_starting_row_number = start;
1106 d_row_stride = stride;
1107 d_ending_row_number = stop;
1110 void Sequence::print_one_row(FILE *out,
int row,
string space,
bool print_row_num)
1113 print_one_row(oss, row, space, print_row_num);
1114 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1117 void Sequence::print_one_row(ostream &out,
int row,
string space,
bool print_row_num)
1119 if (print_row_num) out <<
"\n" << space << row <<
": ";
1133 while (j < elements && !bt_ptr) {
1136 if (bt_ptr->
type() == dods_sequence_c)
1137 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1144 while (j < elements) {
1148 if (bt_ptr->
type() == dods_sequence_c)
1149 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1158 void Sequence::print_val_by_rows(FILE *out,
string space,
bool print_decl_p,
bool print_row_numbers)
1161 print_val_by_rows(oss, space, print_decl_p, print_row_numbers);
1162 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1165 void Sequence::print_val_by_rows(ostream &out,
string space,
bool print_decl_p,
bool print_row_numbers)
1174 int rows = number_of_rows() - 1;
1176 for (i = 0; i < rows; ++i) {
1177 print_one_row(out, i, space, print_row_numbers);
1180 print_one_row(out, i, space, print_row_numbers);
1184 if (print_decl_p) out <<
";\n";
1189 print_val_by_rows(out, space, print_decl_p,
false);
1194 print_val_by_rows(out, space, print_decl_p,
false);
1197 void Sequence::set_leaf_p(
bool state)
1199 d_leaf_sequence = state;
1202 bool Sequence::is_leaf_sequence()
1204 return d_leaf_sequence;
1233 bool has_child_sequence =
false;
1235 if (lvl == 1) d_top_most =
true;
1237 DBG2(cerr <<
"Processing sequence " <<
name() << endl);
1239 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1245 if ((*iter)->type() == dods_sequence_c && (*iter)->send_p()) {
1246 if (has_child_sequence)
1247 throw Error(
"This implementation does not support more than one nested sequence at a level. Contact the server administrator.");
1249 has_child_sequence =
true;
1252 else if ((*iter)->type() == dods_structure_c) {
1257 if (!has_child_sequence)
1262 DBG2(cerr <<
"is_leaf_sequence(): " << is_leaf_sequence() <<
" (" <<
name() <<
")" << endl);
1275 strm << DapIndent::LMarg <<
"Sequence::dump - (" << (
void *)
this <<
")" << endl;
1276 DapIndent::Indent();
1278 strm << DapIndent::LMarg <<
"# rows deserialized: " << d_row_number << endl;
1279 strm << DapIndent::LMarg <<
"bracket notation information:" << endl;
1280 DapIndent::Indent();
1281 strm << DapIndent::LMarg <<
"starting row #: " << d_starting_row_number << endl;
1282 strm << DapIndent::LMarg <<
"row stride: " << d_row_stride << endl;
1283 strm << DapIndent::LMarg <<
"ending row #: " << d_ending_row_number << endl;
1284 DapIndent::UnIndent();
1286 strm << DapIndent::LMarg <<
"data been sent? " << d_unsent_data << endl;
1287 strm << DapIndent::LMarg <<
"start of instance? " << d_wrote_soi << endl;
1288 strm << DapIndent::LMarg <<
"is leaf sequence? " << d_leaf_sequence << endl;
1289 strm << DapIndent::LMarg <<
"top most in hierarchy? " << d_top_most << endl;
1290 DapIndent::UnIndent();
virtual void clear_local_data()
virtual bool read_p()
Has this variable been read?
virtual string name() const
Returns the name of the class instance.
abstract base class used to unmarshall/deserialize dap data objects
bool get_unsent_data() const
Get the unsent data property.
void set_unsent_data(bool usd)
Set the unsent data property.
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
Receive data from the net.
virtual bool read_row(int row, DDS &dds, ConstraintEvaluator &eval, bool ce_eval=true)
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
Deserialize (read from the network) the entire Sequence.
virtual bool is_dap2_only_type()
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
BaseType * transform_to_dap4(D4Group *root, Constructor *dest)
DAP2 to DAP4 transform.
virtual BaseTypeRow * row_value(size_t row)
Get a whole row from the sequence.
vector< BaseTypeRow * > SequenceValues
virtual void set_row_number_constraint(int start, int stop, int stride=1)
Holds a structure (aggregate) type.
virtual string toString()
virtual int element_count(bool leaves=false)
Count the members of constructor types.
virtual void set_parent(BaseType *parent)
virtual void set_leaf_sequence(int level=1)
Traverse Structure, set Sequence leaf nodes.
A class for software fault reporting.
virtual bool is_linear()
Check to see whether this variable can be printed simply.
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Sequence(const string &n)
The Sequence constructor.
virtual Type type() const
Returns the type of the class instance.
virtual void set_length(int count)
virtual bool read()
simple implementation of read that iterates through vars and calls read on them
virtual void set_leaf_sequence(int lvl=1)
Mark the Sequence which holds the leaf elements.
virtual void dump(ostream &strm) const
dumps information about this object
virtual string toString()
virtual BaseType * ptr_duplicate()=0
int get_starting_row_number()
Get the starting row number.
Evaluate a constraint expression.
virtual SequenceValues & value_ref()
virtual SequenceValues value()
void reset_row_number()
Rest the row number counter.
virtual int length() const
virtual BaseType * get_parent() const
The basic data type for the DODS DAP types.
abstract base class used to marshal/serialize dap data objects
virtual BaseType * var_value(size_t row, const string &name)
Get the BaseType pointer to the named variable of a given row.
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.
virtual void print_val(ostream &out, string space="", bool print_decl_p=true)
Prints the value of the variable.
bool eval_selection(DDS &dds, const std::string &dataset)
Evaluate a boolean-valued constraint expression. This is main method for the evaluator and is called ...
virtual void intern_data()
Read data into this variable.
virtual void dump(ostream &strm) const
dumps information about this object
A class for error processing.
virtual BaseType * transform_to_dap4(D4Group *root, Constructor *container)
virtual int get_ending_row_number()
Get the ending row number.
virtual BaseType * ptr_duplicate()
virtual int get_row_stride()
Get the row stride.
virtual void set_value(SequenceValues &values)
virtual string dataset() const
Returns the name of the dataset used to create this instance.
virtual void set_read_p(bool state)
Sets the value of the read_p property.
vector< BaseType * > BaseTypeRow