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)
227 static inline void delete_bt(
BaseType *bt_ptr)
233 static inline void delete_rows(
BaseTypeRow *bt_row_ptr)
235 for_each(bt_row_ptr->begin(), bt_row_ptr->end(), delete_bt);
241 Sequence::~Sequence()
248 if (!d_values.empty()) {
249 for_each(d_values.begin(), d_values.end(), delete_rows);
257 Sequence::operator=(
const Sequence &rhs)
259 if (
this == &rhs)
return *
this;
282 for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
283 oss << (*i)->toString();
294 bool seq_found =
false;
295 for (Vars_iter iter = d_vars.begin(); linear && iter != d_vars.end(); iter++) {
296 if ((*iter)->type() == dods_sequence_c) {
307 else if ((*iter)->type() == dods_structure_c) {
312 linear = (*iter)->is_simple_type();
326 if (row >= d_values.size())
return 0;
327 return d_values[row];
365 if (!bt_row_ptr)
return 0;
367 BaseTypeRow::iterator bt_row_iter = bt_row_ptr->begin();
368 BaseTypeRow::iterator bt_row_end = bt_row_ptr->end();
369 while (bt_row_iter != bt_row_end && (*bt_row_iter)->name() !=
name)
372 if (bt_row_iter == bt_row_end)
387 if (!bt_row_ptr)
return 0;
389 if (i >= bt_row_ptr->size())
return 0;
391 return (*bt_row_ptr)[i];
418 int Sequence::number_of_rows()
const 420 return d_values.size();
442 if ((*i)->type() == dods_sequence_c)
500 DBG2(cerr <<
"Entering Sequence::read_row for " <<
name() <<
", row number " << row <<
", current row " << d_row_number << endl);
501 if (row < d_row_number)
throw InternalErr(
"Trying to back up inside a sequence!");
503 if (row == d_row_number) {
504 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() << endl);
509 while (!eof && d_row_number < row) {
530 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() <<
" with eof: " << eof << endl);
541 inline bool Sequence::is_end_of_rows(
int i)
543 return ((d_ending_row_number == -1) ?
false : (i > d_ending_row_number));
611 if (is_leaf_sequence())
612 status = serialize_leaf(dds, eval, m, ce_eval);
614 status = serialize_parent_part_one(dds, eval, m);
625 DBG2(cerr <<
"Entering serialize_parent_part_one for " <<
name() << endl);
627 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
633 bool status =
read_row(i, dds, eval,
false);
634 DBG2(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
636 while (status && !is_end_of_rows(i)) {
643 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
653 if ((*iter)->send_p() && (*iter)->type() == dods_sequence_c) (*iter)->serialize(eval, dds, m);
658 status =
read_row(i, dds, eval,
false);
659 DBG(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
667 if (d_top_most || d_wrote_soi) {
668 DBG(cerr <<
"Writing End of Sequence marker" << endl);
669 write_end_of_sequence(m);
688 DBG(cerr <<
"Entering serialize_parent_part_two for " <<
name() << endl);
691 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
694 DBG(cerr <<
"Writing Start of Instance marker" << endl);
696 write_start_of_instance(m);
699 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
701 DBG(cerr <<
"Sequence::serialize_parent_part_two(), serializing " 702 << (*iter)->name() << endl);
703 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
704 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
705 (*iter)->serialize(eval, dds, m,
false);
709 d_unsent_data =
false;
717 DBG(cerr <<
"Entering Sequence::serialize_leaf for " <<
name() << endl);
718 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
722 bool status =
read_row(i, dds, eval, ce_eval);
723 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
735 if (status && !is_end_of_rows(i)) {
737 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
741 while (status && !is_end_of_rows(i)) {
744 DBG(cerr <<
"Writing Start of Instance marker" << endl);
746 write_start_of_instance(m);
749 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
750 DBG(cerr <<
"Sequence::serialize_leaf(), serializing " 751 << (*iter)->name() << endl);
752 if ((*iter)->send_p()) {
753 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
754 (*iter)->serialize(eval, dds, m,
false);
760 status =
read_row(i, dds, eval, ce_eval);
761 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
766 if (d_wrote_soi || d_top_most) {
767 DBG(cerr <<
"Writing End of Sequence marker" << endl);
768 write_end_of_sequence(m);
798 DBG(cerr <<
"Sequence::intern_data - for " <<
name() << endl); DBG2(cerr <<
" intern_data, values: " << &d_values << endl);
803 sequence_values_stack_t sequence_values_stack;
805 sequence_values_stack.push(&d_values);
807 intern_data_private(eval, dds, sequence_values_stack);
810 void Sequence::intern_data_private(
ConstraintEvaluator &eval,
DDS &dds, sequence_values_stack_t &sequence_values_stack)
812 DBG(cerr <<
"Entering intern_data_private for " <<
name() << endl);
814 if (is_leaf_sequence())
815 intern_data_for_leaf(dds, eval, sequence_values_stack);
817 intern_data_parent_part_one(dds, eval, sequence_values_stack);
821 sequence_values_stack_t & sequence_values_stack)
823 DBG(cerr <<
"Entering intern_data_parent_part_one for " <<
name() << endl);
831 bool status =
read_row(i, dds, eval,
false);
839 SequenceValues::size_type orig_stack_size = sequence_values_stack.size();
844 if ((*iter)->send_p()) {
845 switch ((*iter)->type()) {
846 case dods_sequence_c:
847 static_cast<Sequence&
>(**iter).intern_data_private(eval, dds, sequence_values_stack);
851 (*iter)->intern_data(eval, dds);
859 status =
read_row(i, dds, eval,
false);
869 if (sequence_values_stack.size() > orig_stack_size) {
870 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
871 <<
") off stack; size: " << sequence_values_stack.size() << endl);
872 sequence_values_stack.pop();
875 DBG(cerr <<
"Leaving intern_data_parent_part_one for " <<
name() << endl);
879 sequence_values_stack_t &sequence_values_stack)
881 DBG(cerr <<
"Entering intern_data_parent_part_two for " <<
name() << endl);
884 if (btp && btp->
type() == dods_sequence_c) {
885 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
888 DBG2(cerr <<
" stack size: " << sequence_values_stack.size() << endl);
890 DBG2(cerr <<
" using values = " << (
void *)values << endl);
898 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
899 row_data->push_back((*iter)->ptr_duplicate());
901 else if ((*iter)->send_p()) {
905 throw InternalErr(__FILE__, __LINE__,
"Expected a Sequence.");
907 row_data->push_back(tmp);
908 DBG2(cerr <<
" pushing d_values of " << tmp->
name()
909 <<
" (" << &(tmp->d_values)
910 <<
") on stack; size: " << sequence_values_stack.size()
915 sequence_values_stack.push(&(tmp->d_values));
919 DBG2(cerr <<
" pushing values for " <<
name()
920 <<
" to " << values << endl);
921 values->push_back(row_data);
925 DBG(cerr <<
"Leaving intern_data_parent_part_two for " <<
name() << endl);
928 void Sequence::intern_data_for_leaf(
DDS &dds,
ConstraintEvaluator &eval, sequence_values_stack_t &sequence_values_stack)
930 DBG(cerr <<
"Entering intern_data_for_leaf for " <<
name() << endl);
934 DBG2(cerr <<
" reading row " << i << endl);
935 bool status =
read_row(i, dds, eval,
true);
936 DBG2(cerr <<
" status: " << status << endl); DBG2(cerr <<
" ending row number: " <<
get_ending_row_number() << endl);
940 if (btp && btp->
type() == dods_sequence_c) {
944 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
951 DBG2(cerr <<
" using values = " << values << endl);
959 if ((*iter)->send_p()) {
960 row_data->push_back((*iter)->ptr_duplicate());
964 DBG2(cerr <<
" pushing values for " <<
name()
965 <<
" to " << values << endl);
967 values->push_back(row_data);
971 status =
read_row(i, dds, eval,
true);
974 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
975 <<
") off stack; size: " << sequence_values_stack.size() << endl);
976 sequence_values_stack.pop();
979 DBG(cerr <<
"Leaving intern_data_for_leaf for " <<
name() << endl);
1007 if (!dd)
throw InternalErr(
"Expected argument 'dds' to be a DataDDS!");
1009 DBG2(cerr <<
"Reading from server/protocol version: " 1010 << dd->get_protocol_major() <<
"." << dd->get_protocol_minor()
1014 if (dd->get_protocol_major() < 2) {
1016 string(
"The protocl version (") + dd->get_protocol()
1017 +
") indicates that this\nis an old server which may not correctly transmit Sequence variables.\nContact the server administrator.");
1022 unsigned char marker = read_marker(um);
1023 if (is_end_of_sequence(marker))
1025 else if (is_start_of_instance(marker)) {
1027 DBG2(cerr <<
"Reading row " << d_row_number <<
" of " 1031 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1034 DBG2(cerr <<
"Deserialized " << bt_ptr->
name() <<
" (" 1035 << bt_ptr <<
") = "); DBG2(bt_ptr->
print_val(stderr,
""));
1036 bt_row_ptr->push_back(bt_ptr);
1039 d_values.push_back(bt_row_ptr);
1042 throw Error(
"I could not read the expected Sequence data stream marker!");
1063 return d_starting_row_number;
1078 return d_row_stride;
1094 return d_ending_row_number;
1107 if (stop < start)
throw Error(malformed_expr,
"Starting row number must precede the ending row number.");
1109 d_starting_row_number = start;
1110 d_row_stride = stride;
1111 d_ending_row_number = stop;
1114 void Sequence::print_one_row(FILE *out,
int row,
string space,
bool print_row_num)
1117 print_one_row(oss, row, space, print_row_num);
1118 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1121 void Sequence::print_one_row(ostream &out,
int row,
string space,
bool print_row_num)
1123 if (print_row_num) out <<
"\n" << space << row <<
": ";
1137 while (j < elements && !bt_ptr) {
1140 if (bt_ptr->
type() == dods_sequence_c)
1141 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1148 while (j < elements) {
1152 if (bt_ptr->
type() == dods_sequence_c)
1153 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1162 void Sequence::print_val_by_rows(FILE *out,
string space,
bool print_decl_p,
bool print_row_numbers)
1165 print_val_by_rows(oss, space, print_decl_p, print_row_numbers);
1166 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1169 void Sequence::print_val_by_rows(ostream &out,
string space,
bool print_decl_p,
bool print_row_numbers)
1178 int rows = number_of_rows() - 1;
1180 for (i = 0; i < rows; ++i) {
1181 print_one_row(out, i, space, print_row_numbers);
1184 print_one_row(out, i, space, print_row_numbers);
1188 if (print_decl_p) out <<
";\n";
1193 print_val_by_rows(out, space, print_decl_p,
false);
1198 print_val_by_rows(out, space, print_decl_p,
false);
1201 void Sequence::set_leaf_p(
bool state)
1203 d_leaf_sequence = state;
1206 bool Sequence::is_leaf_sequence()
1208 return d_leaf_sequence;
1237 bool has_child_sequence =
false;
1239 if (lvl == 1) d_top_most =
true;
1241 DBG2(cerr <<
"Processing sequence " <<
name() << endl);
1243 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1249 if ((*iter)->type() == dods_sequence_c && (*iter)->send_p()) {
1250 if (has_child_sequence)
1251 throw Error(
"This implementation does not support more than one nested sequence at a level. Contact the server administrator.");
1253 has_child_sequence =
true;
1256 else if ((*iter)->type() == dods_structure_c) {
1261 if (!has_child_sequence)
1266 DBG2(cerr <<
"is_leaf_sequence(): " << is_leaf_sequence() <<
" (" <<
name() <<
")" << endl);
1279 strm << DapIndent::LMarg <<
"Sequence::dump - (" << (
void *)
this <<
")" << endl;
1280 DapIndent::Indent();
1282 strm << DapIndent::LMarg <<
"# rows deserialized: " << d_row_number << endl;
1283 strm << DapIndent::LMarg <<
"bracket notation information:" << endl;
1284 DapIndent::Indent();
1285 strm << DapIndent::LMarg <<
"starting row #: " << d_starting_row_number << endl;
1286 strm << DapIndent::LMarg <<
"row stride: " << d_row_stride << endl;
1287 strm << DapIndent::LMarg <<
"ending row #: " << d_ending_row_number << endl;
1288 DapIndent::UnIndent();
1290 strm << DapIndent::LMarg <<
"data been sent? " << d_unsent_data << endl;
1291 strm << DapIndent::LMarg <<
"start of instance? " << d_wrote_soi << endl;
1292 strm << DapIndent::LMarg <<
"is leaf sequence? " << d_leaf_sequence << endl;
1293 strm << DapIndent::LMarg <<
"top most in hierarchy? " << d_top_most << endl;
1294 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)
virtual void add_var_nocopy(BaseType *bt, Part part=nil)
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_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 transform_to_dap4(D4Group *root, Constructor *dest)
DAP2 to DAP4 transform.
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
virtual void transform_to_dap4(D4Group *root, Constructor *container)
A class for error processing.
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