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();
450 void Sequence::load_prototypes_with_values(
int row_number)
452 vector<BaseType*> *row = d_values.at(row_number);
460 if ((*i)->type() != row->at(pos)->var()->type())
461 throw InternalErr(__FILE__, __LINE__,
"Expected types to match when loading values for selection expression evaluation.");
464 switch ((*i)->type()) {
469 static_cast<Int16*
>(*i)->set_value(static_cast<Int16*>(row->at(pos++))->
value());
472 static_cast<Int32*
>(*i)->set_value(static_cast<Int32*>(row->at(pos++))->
value());
475 static_cast<UInt16*
>(*i)->set_value(static_cast<UInt16*>(row->at(pos++))->
value());
478 static_cast<UInt32*
>(*i)->set_value(static_cast<UInt32*>(row->at(pos++))->
value());
481 static_cast<Float32*
>(*i)->set_value(static_cast<Float32*>(row->at(pos++))->
value());
484 static_cast<Float64*
>(*i)->set_value(static_cast<Float64*>(row->at(pos++))->
value());
493 throw InternalErr(__FILE__, __LINE__,
"Expected a scalar type when loading values for selection expression evaluation.");
554 DBG2(cerr <<
"Entering Sequence::read_row for " <<
name() << endl);
555 if (row < d_row_number)
throw InternalErr(
"Trying to back up inside a sequence!");
557 DBG2(cerr <<
"read_row: row number " << row <<
", current row " << d_row_number << endl);
558 if (row == d_row_number) {
559 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() << endl);
562 #if USE_LOCAL_TIMEOUT_SCHEME 566 while (!eof && d_row_number < row) {
579 if (d_is_synthesized) {
580 load_prototypes_with_values(d_row_number);
596 #if USE_LOCAL_TIMEOUT_SCHEME 600 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() <<
" with eof: " << eof << endl);
611 inline bool Sequence::is_end_of_rows(
int i)
613 return ((d_ending_row_number == -1) ?
false : (i > d_ending_row_number));
678 DBG2(cerr <<
"Entering Sequence::serialize for " <<
name() << endl);
683 if (is_leaf_sequence())
684 status = serialize_leaf(dds, eval, m, ce_eval);
686 status = serialize_parent_part_one(dds, eval, m);
688 #ifdef CLEAR_LOCAL_DATA 701 DBG2(cerr <<
"Entering serialize_parent_part_one for " <<
name() << endl);
703 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
709 bool status =
read_row(i, dds, eval,
false);
710 DBG2(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
712 while (status && !is_end_of_rows(i)) {
719 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
729 if ((*iter)->send_p() && (*iter)->type() == dods_sequence_c) (*iter)->serialize(eval, dds, m);
734 status =
read_row(i, dds, eval,
false);
735 DBG(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
743 if (d_top_most || d_wrote_soi) {
744 DBG(cerr <<
"Writing End of Sequence marker" << endl);
745 write_end_of_sequence(m);
764 DBG(cerr <<
"Entering serialize_parent_part_two for " <<
name() << endl);
767 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
770 DBG(cerr <<
"Writing Start of Instance marker" << endl);
772 write_start_of_instance(m);
775 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
777 DBG(cerr <<
"Sequence::serialize_parent_part_two(), serializing " 778 << (*iter)->name() << endl);
779 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
780 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
781 (*iter)->serialize(eval, dds, m,
false);
785 d_unsent_data =
false;
793 DBG(cerr <<
"Entering Sequence::serialize_leaf for " <<
name() << endl);
794 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
798 bool status =
read_row(i, dds, eval, ce_eval);
799 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
811 if (status && !is_end_of_rows(i)) {
813 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
817 while (status && !is_end_of_rows(i)) {
820 DBG(cerr <<
"Writing Start of Instance marker" << endl);
822 write_start_of_instance(m);
825 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
826 DBG(cerr <<
"Sequence::serialize_leaf(), serializing " 827 << (*iter)->name() << endl);
828 if ((*iter)->send_p()) {
829 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
830 (*iter)->serialize(eval, dds, m,
false);
836 status =
read_row(i, dds, eval, ce_eval);
837 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
842 if (d_wrote_soi || d_top_most) {
843 DBG(cerr <<
"Writing End of Sequence marker" << endl);
844 write_end_of_sequence(m);
874 DBG(cerr <<
"Sequence::intern_data - for " <<
name() << endl); DBG2(cerr <<
" intern_data, values: " << &d_values << endl);
879 sequence_values_stack_t sequence_values_stack;
881 DBG2(cerr <<
" pushing d_values of " <<
name() <<
" (" << &d_values
882 <<
") on stack; size: " << sequence_values_stack.size() << endl);
883 sequence_values_stack.push(&d_values);
885 intern_data_private(eval, dds, sequence_values_stack);
888 void Sequence::intern_data_private(
ConstraintEvaluator &eval,
DDS &dds, sequence_values_stack_t &sequence_values_stack)
890 DBG(cerr <<
"Entering intern_data_private for " <<
name() << endl);
892 if (is_leaf_sequence())
893 intern_data_for_leaf(dds, eval, sequence_values_stack);
895 intern_data_parent_part_one(dds, eval, sequence_values_stack);
899 sequence_values_stack_t & sequence_values_stack)
901 DBG(cerr <<
"Entering intern_data_parent_part_one for " <<
name() << endl);
909 bool status =
read_row(i, dds, eval,
false);
917 SequenceValues::size_type orig_stack_size = sequence_values_stack.size();
922 if ((*iter)->send_p()) {
923 switch ((*iter)->type()) {
924 case dods_sequence_c:
925 static_cast<Sequence&
>(**iter).intern_data_private(eval, dds, sequence_values_stack);
929 (*iter)->intern_data(eval, dds);
937 status =
read_row(i, dds, eval,
false);
947 if (sequence_values_stack.size() > orig_stack_size) {
948 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
949 <<
") off stack; size: " << sequence_values_stack.size() << endl);
950 sequence_values_stack.pop();
951 } DBG(cerr <<
"Leaving intern_data_parent_part_one for " <<
name() << endl);
955 sequence_values_stack_t &sequence_values_stack)
957 DBG(cerr <<
"Entering intern_data_parent_part_two for " <<
name() << endl);
960 if (btp && btp->
type() == dods_sequence_c) {
961 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
964 DBG2(cerr <<
" stack size: " << sequence_values_stack.size() << endl);
966 DBG2(cerr <<
" using values = " << (
void *)values << endl);
974 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
975 row_data->push_back((*iter)->ptr_duplicate());
977 else if ((*iter)->send_p()) {
981 throw InternalErr(__FILE__, __LINE__,
"Expected a Sequence.");
983 row_data->push_back(tmp);
984 DBG2(cerr <<
" pushing d_values of " << tmp->
name()
985 <<
" (" << &(tmp->d_values)
986 <<
") on stack; size: " << sequence_values_stack.size()
991 sequence_values_stack.push(&(tmp->d_values));
995 DBG2(cerr <<
" pushing values for " <<
name()
996 <<
" to " << values << endl);
997 values->push_back(row_data);
999 } DBG(cerr <<
"Leaving intern_data_parent_part_two for " <<
name() << endl);
1002 void Sequence::intern_data_for_leaf(
DDS &dds,
ConstraintEvaluator &eval, sequence_values_stack_t &sequence_values_stack)
1004 DBG(cerr <<
"Entering intern_data_for_leaf for " <<
name() << endl);
1008 DBG2(cerr <<
" reading row " << i << endl);
1009 bool status =
read_row(i, dds, eval,
true);
1010 DBG2(cerr <<
" status: " << status << endl); DBG2(cerr <<
" ending row number: " <<
get_ending_row_number() << endl);
1014 if (btp && btp->
type() == dods_sequence_c) {
1018 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
1025 DBG2(cerr <<
" using values = " << values << endl);
1033 if ((*iter)->send_p()) {
1034 row_data->push_back((*iter)->ptr_duplicate());
1038 DBG2(cerr <<
" pushing values for " <<
name()
1039 <<
" to " << values << endl);
1041 values->push_back(row_data);
1045 status =
read_row(i, dds, eval,
true);
1048 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
1049 <<
") off stack; size: " << sequence_values_stack.size() << endl);
1050 sequence_values_stack.pop();
1051 } DBG(cerr <<
"Leaving intern_data_for_leaf for " <<
name() << endl);
1077 if (!dd)
throw InternalErr(
"Expected argument 'dds' to be a DataDDS!");
1079 DBG2(cerr <<
"Reading from server/protocol version: " 1080 << dd->get_protocol_major() <<
"." << dd->get_protocol_minor()
1084 if (dd->get_protocol_major() < 2) {
1086 string(
"The protocl version (") + dd->get_protocol()
1087 +
") indicates that this\nis an old server which may not correctly transmit Sequence variables.\nContact the server administrator.");
1092 unsigned char marker = read_marker(um);
1093 if (is_end_of_sequence(marker))
1095 else if (is_start_of_instance(marker)) {
1097 DBG2(cerr <<
"Reading row " << d_row_number <<
" of " 1101 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1104 DBG2(cerr <<
"Deserialized " << bt_ptr->
name() <<
" (" 1105 << bt_ptr <<
") = "); DBG2(bt_ptr->
print_val(stderr,
""));
1106 bt_row_ptr->push_back(bt_ptr);
1109 d_values.push_back(bt_row_ptr);
1112 throw Error(
"I could not read the expected Sequence data stream marker!");
1133 return d_starting_row_number;
1148 return d_row_stride;
1164 return d_ending_row_number;
1177 if (stop < start)
throw Error(malformed_expr,
"Starting row number must precede the ending row number.");
1179 d_starting_row_number = start;
1180 d_row_stride = stride;
1181 d_ending_row_number = stop;
1184 void Sequence::print_one_row(FILE *out,
int row,
string space,
bool print_row_num)
1187 print_one_row(oss, row, space, print_row_num);
1188 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1191 void Sequence::print_one_row(ostream &out,
int row,
string space,
bool print_row_num)
1193 if (print_row_num) out <<
"\n" << space << row <<
": ";
1207 while (j < elements && !bt_ptr) {
1210 if (bt_ptr->
type() == dods_sequence_c)
1211 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1218 while (j < elements) {
1222 if (bt_ptr->
type() == dods_sequence_c)
1223 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1232 void Sequence::print_val_by_rows(FILE *out,
string space,
bool print_decl_p,
bool print_row_numbers)
1235 print_val_by_rows(oss, space, print_decl_p, print_row_numbers);
1236 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1239 void Sequence::print_val_by_rows(ostream &out,
string space,
bool print_decl_p,
bool print_row_numbers)
1248 int rows = number_of_rows() - 1;
1250 for (i = 0; i < rows; ++i) {
1251 print_one_row(out, i, space, print_row_numbers);
1254 print_one_row(out, i, space, print_row_numbers);
1258 if (print_decl_p) out <<
";\n";
1263 print_val_by_rows(out, space, print_decl_p,
false);
1268 print_val_by_rows(out, space, print_decl_p,
false);
1271 void Sequence::set_leaf_p(
bool state)
1273 d_leaf_sequence = state;
1276 bool Sequence::is_leaf_sequence()
1278 return d_leaf_sequence;
1307 bool has_child_sequence =
false;
1309 if (lvl == 1) d_top_most =
true;
1311 DBG2(cerr <<
"Processing sequence " <<
name() << endl);
1313 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1319 if ((*iter)->type() == dods_sequence_c && (*iter)->send_p()) {
1320 if (has_child_sequence)
1321 throw Error(
"This implementation does not support more than one nested sequence at a level. Contact the server administrator.");
1323 has_child_sequence =
true;
1326 else if ((*iter)->type() == dods_structure_c) {
1331 if (!has_child_sequence)
1336 DBG2(cerr <<
"is_leaf_sequence(): " << is_leaf_sequence() <<
" (" <<
name() <<
")" << endl);
1349 strm << DapIndent::LMarg <<
"Sequence::dump - (" << (
void *)
this <<
")" << endl;
1350 DapIndent::Indent();
1352 strm << DapIndent::LMarg <<
"# rows deserialized: " << d_row_number << endl;
1353 strm << DapIndent::LMarg <<
"bracket notation information:" << endl;
1354 DapIndent::Indent();
1355 strm << DapIndent::LMarg <<
"starting row #: " << d_starting_row_number << endl;
1356 strm << DapIndent::LMarg <<
"row stride: " << d_row_stride << endl;
1357 strm << DapIndent::LMarg <<
"ending row #: " << d_ending_row_number << endl;
1358 DapIndent::UnIndent();
1360 strm << DapIndent::LMarg <<
"data been sent? " << d_unsent_data << endl;
1361 strm << DapIndent::LMarg <<
"start of instance? " << d_wrote_soi << endl;
1362 strm << DapIndent::LMarg <<
"is leaf sequence? " << d_leaf_sequence << endl;
1363 strm << DapIndent::LMarg <<
"top most in hierarchy? " << d_top_most << endl;
1364 DapIndent::UnIndent();
virtual void clear_local_data()
Holds an Internet address (URL).
virtual bool read_p()
Has this variable been read?
abstract base class used to unmarshall/deserialize dap data objects
virtual void dump(ostream &strm) const
dumps information about this object
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()
Holds an unsigned 16-bit integer.
vector< BaseTypeRow * > SequenceValues
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.
virtual void set_row_number_constraint(int start, int stop, int stride=1)
Holds a structure (aggregate) type.
virtual Type type() const
Returns the type of the class instance.
virtual string toString()
Holds a 32-bit floating point value.
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 string dataset() const
Returns the name of the dataset used to create this instance.
virtual bool is_linear()
Check to see whether this variable can be printed simply.
Holds character string data.
virtual int length() const
bool get_unsent_data()
Get the unsent data property.
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Holds a 16-bit signed integer value.
Sequence(const string &n)
The Sequence constructor.
virtual BaseType * get_parent() const
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 bool set_value(const dods_byte value)
virtual void dump(ostream &strm) const
dumps information about this object
virtual bool set_value(const string &value)
vector< BaseType * > BaseTypeRow
virtual string name() const
Returns the name of the class instance.
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.
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.
Holds a 64-bit (double precision) floating point value.
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 ans is called ...
virtual void intern_data()
Read data into this variable.
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.
Holds a 32-bit unsigned integer.
virtual BaseType * ptr_duplicate()
virtual int get_row_stride()
Get the row stride.
virtual void set_value(SequenceValues &values)
Holds a 32-bit signed integer.
virtual void set_read_p(bool state)
Sets the value of the read_p property.