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" 76 #include "DapIndent.h" 78 #undef CLEAR_LOCAL_DATA 84 static const unsigned char end_of_sequence = 0xA5;
85 static const unsigned char start_of_instance = 0x5A;
89 void Sequence::m_duplicate(
const Sequence &s)
91 DBG(cerr <<
"In Sequence::m_duplicate" << endl);
93 d_row_number = s.d_row_number;
94 d_starting_row_number = s.d_starting_row_number;
95 d_ending_row_number = s.d_ending_row_number;
96 d_row_stride = s.d_row_stride;
97 d_leaf_sequence = s.d_leaf_sequence;
98 d_unsent_data = s.d_unsent_data;
99 d_wrote_soi = s.d_wrote_soi;
100 d_top_most = s.d_top_most;
102 Sequence &cs =
const_cast<Sequence &
>(s);
105 for (vector<BaseTypeRow *>::iterator rows_iter = cs.d_values.begin(); rows_iter != cs.d_values.end(); rows_iter++) {
112 for (BaseTypeRow::iterator bt_row_iter = src_bt_row_ptr->begin(); bt_row_iter != src_bt_row_ptr->end();
114 BaseType *src_bt_ptr = *bt_row_iter;
115 BaseType *dest_bt_ptr = src_bt_ptr->ptr_duplicate();
116 dest_bt_row_ptr->push_back(dest_bt_ptr);
119 d_values.push_back(dest_bt_row_ptr);
123 static void write_end_of_sequence(Marshaller &m)
125 m.put_opaque((
char *) &end_of_sequence, 1);
128 static void write_start_of_instance(Marshaller &m)
130 m.put_opaque((
char *) &start_of_instance, 1);
133 static unsigned char read_marker(UnMarshaller &um)
135 unsigned char marker;
136 um.get_opaque((
char *) &marker, 1);
141 static bool is_start_of_instance(
unsigned char marker)
143 return (marker == start_of_instance);
146 static bool is_end_of_sequence(
unsigned char marker)
148 return (marker == end_of_sequence);
161 Sequence::Sequence(
const string &n) :
162 Constructor(n, dods_sequence_c), d_row_number(-1), d_starting_row_number(-1), d_row_stride(1), d_ending_row_number(
163 -1), d_unsent_data(false), d_wrote_soi(false), d_leaf_sequence(false), d_top_most(false)
178 Constructor(n, d, dods_sequence_c), d_row_number(-1), d_starting_row_number(-1),
179 d_row_stride(1), d_ending_row_number(-1), d_unsent_data(false),
180 d_wrote_soi(false), d_leaf_sequence(false), d_top_most(false)
228 static inline void delete_bt(
BaseType *bt_ptr)
234 static inline void delete_rows(
BaseTypeRow *bt_row_ptr)
236 for_each(bt_row_ptr->begin(), bt_row_ptr->end(), delete_bt);
242 Sequence::~Sequence()
249 if (!d_values.empty()) {
250 for_each(d_values.begin(), d_values.end(), delete_rows);
258 Sequence::operator=(
const Sequence &rhs)
260 if (
this == &rhs)
return *
this;
283 for (Vars_iter i = d_vars.begin(); i != d_vars.end(); i++) {
284 oss << (*i)->toString();
295 bool seq_found =
false;
296 for (Vars_iter iter = d_vars.begin(); linear && iter != d_vars.end(); iter++) {
297 if ((*iter)->type() == dods_sequence_c) {
308 else if ((*iter)->type() == dods_structure_c) {
313 linear = (*iter)->is_simple_type();
327 if (row >= d_values.size())
return 0;
328 return d_values[row];
366 if (!bt_row_ptr)
return 0;
368 BaseTypeRow::iterator bt_row_iter = bt_row_ptr->begin();
369 BaseTypeRow::iterator bt_row_end = bt_row_ptr->end();
370 while (bt_row_iter != bt_row_end && (*bt_row_iter)->name() !=
name)
373 if (bt_row_iter == bt_row_end)
388 if (!bt_row_ptr)
return 0;
390 if (i >= bt_row_ptr->size())
return 0;
392 return (*bt_row_ptr)[i];
419 int Sequence::number_of_rows()
const 421 return d_values.size();
443 if ((*i)->type() == dods_sequence_c)
501 DBG2(cerr <<
"Entering Sequence::read_row for " <<
name() <<
", row number " << row <<
", current row " << d_row_number << endl);
502 if (row < d_row_number)
throw InternalErr(
"Trying to back up inside a sequence!");
504 if (row == d_row_number) {
505 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() << endl);
510 while (!eof && d_row_number < row) {
531 DBG2(cerr <<
"Leaving Sequence::read_row for " <<
name() <<
" with eof: " << eof << endl);
542 inline bool Sequence::is_end_of_rows(
int i)
544 return ((d_ending_row_number == -1) ?
false : (i > d_ending_row_number));
612 if (is_leaf_sequence())
613 status = serialize_leaf(dds, eval, m, ce_eval);
615 status = serialize_parent_part_one(dds, eval, m);
626 DBG2(cerr <<
"Entering serialize_parent_part_one for " <<
name() << endl);
628 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
634 bool status =
read_row(i, dds, eval,
false);
635 DBG2(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
637 while (status && !is_end_of_rows(i)) {
644 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
654 if ((*iter)->send_p() && (*iter)->type() == dods_sequence_c) (*iter)->serialize(eval, dds, m);
659 status =
read_row(i, dds, eval,
false);
660 DBG(cerr <<
"Sequence::serialize_parent_part_one::read_row() status: " << status << endl);
668 if (d_top_most || d_wrote_soi) {
669 DBG(cerr <<
"Writing End of Sequence marker" << endl);
670 write_end_of_sequence(m);
689 DBG(cerr <<
"Entering serialize_parent_part_two for " <<
name() << endl);
692 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
695 DBG(cerr <<
"Writing Start of Instance marker" << endl);
697 write_start_of_instance(m);
700 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
702 DBG(cerr <<
"Sequence::serialize_parent_part_two(), serializing " 703 << (*iter)->name() << endl);
704 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
705 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
706 (*iter)->serialize(eval, dds, m,
false);
710 d_unsent_data =
false;
718 DBG(cerr <<
"Entering Sequence::serialize_leaf for " <<
name() << endl);
719 int i = (d_starting_row_number != -1) ? d_starting_row_number : 0;
723 bool status =
read_row(i, dds, eval, ce_eval);
724 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
736 if (status && !is_end_of_rows(i)) {
738 if (btp && btp->
type() == dods_sequence_c) static_cast<Sequence&>(*btp).serialize_parent_part_two(dds, eval, m);
742 while (status && !is_end_of_rows(i)) {
745 DBG(cerr <<
"Writing Start of Instance marker" << endl);
747 write_start_of_instance(m);
750 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
751 DBG(cerr <<
"Sequence::serialize_leaf(), serializing " 752 << (*iter)->name() << endl);
753 if ((*iter)->send_p()) {
754 DBG(cerr <<
"Send P is true, sending " << (*iter)->name() << endl);
755 (*iter)->serialize(eval, dds, m,
false);
761 status =
read_row(i, dds, eval, ce_eval);
762 DBG(cerr <<
"Sequence::serialize_leaf::read_row() status: " << status << endl);
767 if (d_wrote_soi || d_top_most) {
768 DBG(cerr <<
"Writing End of Sequence marker" << endl);
769 write_end_of_sequence(m);
799 DBG(cerr <<
"Sequence::intern_data - for " <<
name() << endl); DBG2(cerr <<
" intern_data, values: " << &d_values << endl);
804 sequence_values_stack_t sequence_values_stack;
806 sequence_values_stack.push(&d_values);
808 intern_data_private(eval, dds, sequence_values_stack);
811 void Sequence::intern_data_private(
ConstraintEvaluator &eval,
DDS &dds, sequence_values_stack_t &sequence_values_stack)
813 DBG(cerr <<
"Entering intern_data_private for " <<
name() << endl);
815 if (is_leaf_sequence())
816 intern_data_for_leaf(dds, eval, sequence_values_stack);
818 intern_data_parent_part_one(dds, eval, sequence_values_stack);
822 sequence_values_stack_t & sequence_values_stack)
824 DBG(cerr <<
"Entering intern_data_parent_part_one for " <<
name() << endl);
832 bool status =
read_row(i, dds, eval,
false);
840 SequenceValues::size_type orig_stack_size = sequence_values_stack.size();
845 if ((*iter)->send_p()) {
846 switch ((*iter)->type()) {
847 case dods_sequence_c:
848 static_cast<Sequence&
>(**iter).intern_data_private(eval, dds, sequence_values_stack);
852 (*iter)->intern_data(eval, dds);
860 status =
read_row(i, dds, eval,
false);
870 if (sequence_values_stack.size() > orig_stack_size) {
871 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
872 <<
") off stack; size: " << sequence_values_stack.size() << endl);
873 sequence_values_stack.pop();
876 DBG(cerr <<
"Leaving intern_data_parent_part_one for " <<
name() << endl);
880 sequence_values_stack_t &sequence_values_stack)
882 DBG(cerr <<
"Entering intern_data_parent_part_two for " <<
name() << endl);
885 if (btp && btp->
type() == dods_sequence_c) {
886 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
889 DBG2(cerr <<
" stack size: " << sequence_values_stack.size() << endl);
891 DBG2(cerr <<
" using values = " << (
void *)values << endl);
899 if ((*iter)->send_p() && (*iter)->type() != dods_sequence_c) {
900 row_data->push_back((*iter)->ptr_duplicate());
902 else if ((*iter)->send_p()) {
906 throw InternalErr(__FILE__, __LINE__,
"Expected a Sequence.");
908 row_data->push_back(tmp);
909 DBG2(cerr <<
" pushing d_values of " << tmp->
name()
910 <<
" (" << &(tmp->d_values)
911 <<
") on stack; size: " << sequence_values_stack.size()
916 sequence_values_stack.push(&(tmp->d_values));
920 DBG2(cerr <<
" pushing values for " <<
name()
921 <<
" to " << values << endl);
922 values->push_back(row_data);
926 DBG(cerr <<
"Leaving intern_data_parent_part_two for " <<
name() << endl);
929 void Sequence::intern_data_for_leaf(
DDS &dds,
ConstraintEvaluator &eval, sequence_values_stack_t &sequence_values_stack)
931 DBG(cerr <<
"Entering intern_data_for_leaf for " <<
name() << endl);
935 DBG2(cerr <<
" reading row " << i << endl);
936 bool status =
read_row(i, dds, eval,
true);
937 DBG2(cerr <<
" status: " << status << endl); DBG2(cerr <<
" ending row number: " <<
get_ending_row_number() << endl);
941 if (btp && btp->
type() == dods_sequence_c) {
945 static_cast<Sequence&
>(*btp).intern_data_parent_part_two(dds, eval, sequence_values_stack);
952 DBG2(cerr <<
" using values = " << values << endl);
960 if ((*iter)->send_p()) {
961 row_data->push_back((*iter)->ptr_duplicate());
965 DBG2(cerr <<
" pushing values for " <<
name()
966 <<
" to " << values << endl);
968 values->push_back(row_data);
972 status =
read_row(i, dds, eval,
true);
975 DBG2(cerr <<
" popping d_values (" << sequence_values_stack.top()
976 <<
") off stack; size: " << sequence_values_stack.size() << endl);
977 sequence_values_stack.pop();
980 DBG(cerr <<
"Leaving intern_data_for_leaf for " <<
name() << endl);
1008 if (!dd)
throw InternalErr(
"Expected argument 'dds' to be a DataDDS!");
1010 DBG2(cerr <<
"Reading from server/protocol version: " 1011 << dd->get_protocol_major() <<
"." << dd->get_protocol_minor()
1015 if (dd->get_protocol_major() < 2) {
1017 string(
"The protocl version (") + dd->get_protocol()
1018 +
") indicates that this\nis an old server which may not correctly transmit Sequence variables.\nContact the server administrator.");
1023 unsigned char marker = read_marker(um);
1024 if (is_end_of_sequence(marker))
1026 else if (is_start_of_instance(marker)) {
1028 DBG2(cerr <<
"Reading row " << d_row_number <<
" of " 1032 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1035 DBG2(cerr <<
"Deserialized " << bt_ptr->
name() <<
" (" 1036 << bt_ptr <<
") = "); DBG2(bt_ptr->
print_val(stderr,
""));
1037 bt_row_ptr->push_back(bt_ptr);
1040 d_values.push_back(bt_row_ptr);
1043 throw Error(
"I could not read the expected Sequence data stream marker!");
1064 return d_starting_row_number;
1079 return d_row_stride;
1095 return d_ending_row_number;
1108 if (stop < start)
throw Error(malformed_expr,
"Starting row number must precede the ending row number.");
1110 d_starting_row_number = start;
1111 d_row_stride = stride;
1112 d_ending_row_number = stop;
1115 void Sequence::print_one_row(FILE *out,
int row,
string space,
bool print_row_num)
1118 print_one_row(oss, row, space, print_row_num);
1119 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1122 void Sequence::print_one_row(ostream &out,
int row,
string space,
bool print_row_num)
1124 if (print_row_num) out <<
"\n" << space << row <<
": ";
1138 while (j < elements && !bt_ptr) {
1141 if (bt_ptr->
type() == dods_sequence_c)
1142 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1149 while (j < elements) {
1153 if (bt_ptr->
type() == dods_sequence_c)
1154 static_cast<Sequence*>(bt_ptr)->print_val_by_rows(out, space +
" ",
false, print_row_num);
1163 void Sequence::print_val_by_rows(FILE *out,
string space,
bool print_decl_p,
bool print_row_numbers)
1166 print_val_by_rows(oss, space, print_decl_p, print_row_numbers);
1167 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1170 void Sequence::print_val_by_rows(ostream &out,
string space,
bool print_decl_p,
bool print_row_numbers)
1179 int rows = number_of_rows() - 1;
1181 for (i = 0; i < rows; ++i) {
1182 print_one_row(out, i, space, print_row_numbers);
1185 print_one_row(out, i, space, print_row_numbers);
1189 if (print_decl_p) out <<
";\n";
1194 print_val_by_rows(out, space, print_decl_p,
false);
1199 print_val_by_rows(out, space, print_decl_p,
false);
1202 void Sequence::set_leaf_p(
bool state)
1204 d_leaf_sequence = state;
1207 bool Sequence::is_leaf_sequence()
1209 return d_leaf_sequence;
1238 bool has_child_sequence =
false;
1240 if (lvl == 1) d_top_most =
true;
1242 DBG2(cerr <<
"Processing sequence " <<
name() << endl);
1244 for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
1250 if ((*iter)->type() == dods_sequence_c && (*iter)->send_p()) {
1251 if (has_child_sequence)
1252 throw Error(
"This implementation does not support more than one nested sequence at a level. Contact the server administrator.");
1254 has_child_sequence =
true;
1257 else if ((*iter)->type() == dods_structure_c) {
1262 if (!has_child_sequence)
1267 DBG2(cerr <<
"is_leaf_sequence(): " << is_leaf_sequence() <<
" (" <<
name() <<
")" << endl);
1280 strm << DapIndent::LMarg <<
"Sequence::dump - (" << (
void *)
this <<
")" << endl;
1281 DapIndent::Indent();
1283 strm << DapIndent::LMarg <<
"# rows deserialized: " << d_row_number << endl;
1284 strm << DapIndent::LMarg <<
"bracket notation information:" << endl;
1285 DapIndent::Indent();
1286 strm << DapIndent::LMarg <<
"starting row #: " << d_starting_row_number << endl;
1287 strm << DapIndent::LMarg <<
"row stride: " << d_row_stride << endl;
1288 strm << DapIndent::LMarg <<
"ending row #: " << d_ending_row_number << endl;
1289 DapIndent::UnIndent();
1291 strm << DapIndent::LMarg <<
"data been sent? " << d_unsent_data << endl;
1292 strm << DapIndent::LMarg <<
"start of instance? " << d_wrote_soi << endl;
1293 strm << DapIndent::LMarg <<
"is leaf sequence? " << d_leaf_sequence << endl;
1294 strm << DapIndent::LMarg <<
"top most in hierarchy? " << d_top_most << endl;
1295 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.
top level DAP object to house generic methods
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