33 #include <libxml/encoding.h>
46 #include "D4EnumDefs.h"
47 #include "D4Attributes.h"
52 #include "D4StreamMarshaller.h"
53 #include "D4StreamUnMarshaller.h"
55 #include "Operators.h"
56 #include "InternalErr.h"
57 #include "dods-datatypes.h"
58 #include "dods-limits.h"
61 #include "DapIndent.h"
69 void D4Enum::m_duplicate(
const D4Enum &src)
72 d_element_type = src.d_element_type;
73 d_enum_def = src.d_enum_def;
77 d_enum_def = src.d_enum_def == 0 ? 0 :
new D4EnumDef(*(src.d_enum_def));
79 d_is_signed = src.d_is_signed;
96 DBG(cerr << __func__ <<
"() - BEGIN" << endl;);
98 switch (d_element_type) {
106 my_pretty_pony =
var;
109 case dods_uint16_c: {
114 my_pretty_pony =
var;
117 case dods_uint32_c: {
122 my_pretty_pony =
var;
125 case dods_uint64_c: {
130 my_pretty_pony =
var;
138 my_pretty_pony =
var;
146 my_pretty_pony =
var;
154 my_pretty_pony =
var;
159 oss <<
"Unknown D4Enum type:" << d_element_type <<
", name: " <<
name() << endl;
164 DBG( cerr << __func__ <<
"() - Processing Enum type:"<<
165 my_pretty_pony->
type_name() <<
" name: " << my_pretty_pony->
name() << endl;);
173 this->
value(&my_value);
175 DBG(cerr << __func__ <<
"() - value: "<< my_value << endl;);
177 string my_label =
"";
181 D4EnumDef::D4EnumValueIter dIter = d_enum_def->value_begin();
182 D4EnumDef::D4EnumValueIter dEnd = d_enum_def->value_end();
183 while (dIter != dEnd) {
184 long long a_value = (*dIter).value;
185 string a_label = (*dIter).label;
188 DBG(cerr << __func__ <<
"() - a_value: "<< a_value << endl;);
190 if (a_value == my_value) {
191 my_label = (*dIter).label;
200 vector<BaseType *> *result =
new vector<BaseType *>();
201 result->push_back(my_pretty_pony);
202 DBG(cerr << __func__ <<
"() - END" << endl;);
206 void D4Enum::m_check_value(int64_t v)
const
208 switch (d_element_type) {
211 if ((uint64_t)v > DODS_UCHAR_MAX || v < 0) {
213 oss <<
"The value " << v <<
" will not fit in an unsigned byte. (" << __func__ <<
")";
214 throw Error(oss.str());
218 if ((uint64_t)v > DODS_USHRT_MAX || v < 0) {
220 oss <<
"The value " << v <<
" will not fit in an unsigned 16-bit integer. (" << __func__ <<
")";
221 throw Error(oss.str());
225 if ((uint64_t)v > DODS_UINT_MAX || v < 0) {
227 oss <<
"The value " << v <<
" will not fit in an unsigned 32-bit integer. (" << __func__ <<
")";
228 throw Error(oss.str());
236 if (v > DODS_SCHAR_MAX || v < DODS_SCHAR_MIN) {
238 oss <<
"The value " << v <<
" will not fit in an unsigned byte. (" << __func__ <<
")";
239 throw Error(oss.str());
244 if (v > DODS_SHRT_MAX || v < DODS_SHRT_MIN) {
246 oss <<
"The value " << v <<
" will not fit in an unsigned byte. (" << __func__ <<
")";
247 throw Error(oss.str());
251 if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
253 oss <<
"The value " << v <<
" will not fit in an unsigned byte. (" << __func__ <<
")";
254 throw Error(oss.str());
261 assert(!
"illegal type for D4Enum");
265 D4Enum::D4Enum(
const string &name,
const string &enum_type) :
266 BaseType(name, dods_enum_c, true ), d_buf(0), d_element_type(dods_null_c), d_enum_def(0)
268 d_element_type =
get_type(enum_type.c_str());
271 set_is_signed(d_element_type);
274 D4Enum::D4Enum(
const string &name,
Type type) :
275 BaseType(name, dods_enum_c, true ), d_buf(0), d_element_type(type), d_enum_def(0)
278 set_is_signed(d_element_type);
281 D4Enum::D4Enum(
const string &name,
const string &dataset,
Type type) :
282 BaseType(name, dataset, dods_enum_c, true ), d_buf(0), d_element_type(type), d_enum_def(0)
285 set_is_signed(d_element_type);
293 template void D4Enum::value<dods_byte>(dods_byte *v)
const;
294 template void D4Enum::value<dods_int16>(dods_int16 *v)
const;
295 template void D4Enum::value<dods_uint16>(dods_uint16 *v)
const;
296 template void D4Enum::value<dods_int32>(dods_int32 *v)
const;
297 template void D4Enum::value<dods_uint32>(dods_uint32 *v)
const;
298 template void D4Enum::value<dods_int64>(dods_int64 *v)
const;
299 template void D4Enum::value<dods_uint64>(dods_uint64 *v)
const;
301 template void D4Enum::set_value<dods_byte>(dods_byte v,
bool check_value);
302 template void D4Enum::set_value<dods_int16>(dods_int16 v,
bool check_value);
303 template void D4Enum::set_value<dods_uint16>(dods_uint16 v,
bool check_value);
304 template void D4Enum::set_value<dods_int32>(dods_int32 v,
bool check_value);
305 template void D4Enum::set_value<dods_uint32>(dods_uint32 v,
bool check_value);
306 template void D4Enum::set_value<dods_int64>(dods_int64 v,
bool check_value);
307 template void D4Enum::set_value<dods_uint64>(dods_uint64 v,
bool check_value);
310 D4Enum::set_enumeration(D4EnumDef *enum_def) {
311 d_enum_def = enum_def;
312 d_element_type = enum_def->type();
318 DBG(cerr << __func__ <<
": element type: " << ::
libdap::type_name(d_element_type) << endl);
320 switch (d_element_type) {
324 dods_byte v =
static_cast<dods_byte
>(d_buf);
325 checksum.
AddData(
reinterpret_cast<uint8_t*
>(&v),
sizeof(uint8_t));
330 dods_int16 v =
static_cast<dods_int16
>(d_buf);
331 checksum.
AddData(
reinterpret_cast<uint8_t*
>(&v),
sizeof(uint16_t));
336 dods_int32 v =
static_cast<dods_int32
>(d_buf);
337 checksum.
AddData(
reinterpret_cast<uint8_t*
>(&v),
sizeof(uint32_t));
342 checksum.
AddData(
reinterpret_cast<uint8_t*
>(&d_buf),
sizeof(uint64_t));
346 assert(!
"illegal type for D4Enum");
351 D4Enum::set_is_signed(
Type t)
373 assert(!
"illegal type for D4Enum");
375 throw InternalErr(__FILE__, __LINE__,
"Illegal type");
397 switch (d_element_type) {
425 assert(!
"illegal type for D4Enum");
432 switch (d_element_type) {
440 case dods_uint16_c: {
446 case dods_uint32_c: {
452 case dods_uint64_c: {
484 assert(!
"illegal type for D4Enum");
491 throw InternalErr(
"The incoming pointer does not contain any data.");
493 switch (d_element_type) {
496 d_buf = *(dods_byte*)val;
499 d_buf = *(dods_uint16*)val;
502 d_buf = *(dods_uint32*)val;
505 d_buf = *(dods_uint64*)val;
509 d_buf = *(dods_int8*)val;
512 d_buf = *(dods_int16*)val;
515 d_buf = *(dods_int32*)val;
518 d_buf = *(dods_int64*)val;
521 assert(!
"illegal type for D4Enum");
532 switch (d_element_type) {
535 if (!*val) *val =
new dods_byte;
536 *(dods_byte *) * val = d_buf;
539 if (!*val) *val =
new dods_uint16;
540 *(dods_uint16 *) * val = d_buf;
543 if (!*val) *val =
new dods_uint32;
544 *(dods_uint32 *) * val = d_buf;
547 if (!*val) *val =
new dods_uint64;
548 *(dods_uint64 *) * val = d_buf;
552 if (!*val) *val =
new dods_int8;
553 *(dods_int8*) * val = d_buf;
556 if (!*val) *val =
new dods_int16;
557 *(dods_int16 *) * val = d_buf;
560 if (!*val) *val =
new dods_int32;
561 *(dods_int32 *) * val = d_buf;
564 if (!*val) *val =
new dods_int64;
565 *(dods_int64 *) * val = d_buf;
568 assert(!
"illegal type for D4Enum");
581 DBG(cerr <<
"Enum union value: " << hex << d_buf << dec << endl);
607 if (constrained && !
send_p())
610 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Enum") < 0)
611 throw InternalErr(__FILE__, __LINE__,
"Could not write Enum element");
614 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)
name().c_str()) < 0)
615 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
618 string path = d_enum_def->name();
622 if (d_enum_def->parent()) {
624 path =
static_cast<D4Group*
>(d_enum_def->parent()->parent())->
FQN() + path;
626 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"enum", (
const xmlChar*)path.c_str()) < 0)
627 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for enum");
634 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
635 throw InternalErr(__FILE__, __LINE__,
"Could not end Enum element");
644 throw InternalErr(__FILE__, __LINE__,
"This value not read!");
648 throw InternalErr(__FILE__, __LINE__,
"This value not read!");
652 return Cmp<dods_int64, dods_int8>(op, d_buf,
static_cast<Int8*
>(b)->
value());
654 return SUCmp<dods_int64, dods_byte>(op, d_buf,
static_cast<Byte*
>(b)->
value());
656 return Cmp<dods_int64, dods_int16>(op, d_buf,
static_cast<Int16*
>(b)->
value());
658 return SUCmp<dods_int64, dods_uint16>(op, d_buf,
static_cast<UInt16*
>(b)->
value());
660 return Cmp<dods_int64, dods_int32>(op, d_buf,
static_cast<Int32*
>(b)->
value());
662 return SUCmp<dods_int64, dods_uint32>(op, d_buf,
static_cast<UInt32*
>(b)->
value());
666 return Cmp<dods_int64, dods_int64>(op, d_buf,
static_cast<D4Enum*
>(b)->
value());
668 return SUCmp<dods_int64, dods_uint64>(op, d_buf,
static_cast<D4Enum*
>(b)->
value());
671 return Cmp<dods_int64, dods_float32>(op, d_buf,
static_cast<Float32*
>(b)->
value());
673 return Cmp<dods_int64, dods_float64>(op, d_buf,
static_cast<Float64*
>(b)->
value());
690 strm << DapIndent::LMarg <<
"D4Enum::dump - (" << (
void *)
this <<
")" << endl;
693 strm << DapIndent::LMarg <<
"value: " << d_buf << endl;
694 DapIndent::UnIndent();