-
Notifications
You must be signed in to change notification settings - Fork 13
/
D4Enum.cc
687 lines (615 loc) · 20.1 KB
/
D4Enum.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#include "config.h"
// #define DODS_DEBUG
#include <cassert>
#include <sstream>
#include <libxml/encoding.h>
#include "Byte.h" // synonymous with UInt8 and Char
#include "Int16.h"
#include "Int32.h"
#include "Int64.h"
#include "Int8.h"
#include "UInt16.h"
#include "UInt32.h"
#include "UInt64.h"
#include "D4Attributes.h"
#include "D4Enum.h"
#include "D4EnumDefs.h"
#include "D4Group.h"
#include "Float32.h"
#include "Float64.h"
#include "D4StreamMarshaller.h"
#include "D4StreamUnMarshaller.h"
#include "DapIndent.h"
#include "InternalErr.h"
#include "Operators.h"
#include "debug.h"
#include "dods-datatypes.h"
#include "dods-limits.h"
#include "util.h"
using std::cerr;
using std::endl;
namespace libdap {
// Private
void D4Enum::m_duplicate(const D4Enum &src) {
d_buf = src.d_buf;
d_element_type = src.d_element_type;
d_enum_def = src.d_enum_def;
#if 0
// The enum_def is a weak pointer managed by D4Group. We just copy it
// and do not delete it. jhrg 10/19/15
d_enum_def = src.d_enum_def == 0 ? 0 : new D4EnumDef(*(src.d_enum_def));
#endif
d_is_signed = src.d_is_signed;
}
/**
* @brief Convert an Enum to a DAP2 int type
*
* Build a DAP2 integer type that matches the D4Enum's internal type and then
* add an attribute table to that new variable that holds the D4Enum's symbolic
* name and value mapping.
*
* @param attr AttrTable pointer, ignored by this method.
*/
vector<BaseType *> *D4Enum::transform_to_dap2(AttrTable *) {
BaseType *btp;
DBG(cerr << __func__ << "() - BEGIN" << endl;);
switch (d_element_type) {
case dods_byte_c:
case dods_int8_c:
case dods_uint8_c: {
Byte *var = new Byte(name());
dods_byte val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
case dods_uint16_c: {
UInt16 *var = new UInt16(name());
dods_uint16 val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
case dods_uint32_c: {
UInt32 *var = new UInt32(name());
dods_uint32 val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
case dods_uint64_c: {
UInt64 *var = new UInt64(name());
dods_uint64 val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
case dods_int16_c: {
Int16 *var = new Int16(name());
dods_int16 val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
case dods_int32_c: {
Int32 *var = new Int32(name());
dods_int32 val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
case dods_int64_c: {
Int64 *var = new Int64(name());
dods_int64 val;
this->value(&val);
var->set_value(val);
btp = var;
break;
}
default: {
ostringstream oss;
oss << "Unknown D4Enum type:" << d_element_type << ", name: " << name() << endl;
throw InternalErr(__FILE__, __LINE__, oss.str());
}
}
DBG(cerr << __func__ << "() - Processing Enum type:" << btp->type_name() << " name: " << btp->name() << endl;);
// Grab the attributes!
#if 0
AttrTable d2_attrs = *(this->attributes()->get_AttrTable(name()));
btp->set_attr_table(d2_attrs);
#else
if (btp->get_attr_table().get_size() == 0) {
attributes()->transform_attrs_to_dap2(&btp->get_attr_table());
btp->get_attr_table().set_name(name());
}
#endif
// make the Enum label, and the enum's definition into DAP2 attributes for our returned item.
long long my_value;
this->value(&my_value);
DBG(cerr << __func__ << "() - value: " << my_value << endl;);
string my_label = "";
AttrTable *enum_def = new AttrTable();
enum_def->set_name("d4:enum_def");
D4EnumDef::D4EnumValueIter dIter = d_enum_def->value_begin();
D4EnumDef::D4EnumValueIter dEnd = d_enum_def->value_end();
while (dIter != dEnd) {
long long a_value = (*dIter).value;
string a_label = (*dIter).label;
ostringstream oss;
oss << a_value;
DBG(cerr << __func__ << "() - a_value: " << a_value << endl;);
enum_def->append_attr(a_label, btp->type_name(), oss.str());
if (a_value == my_value) {
my_label = (*dIter).label;
}
dIter++;
}
if (!my_label.empty())
btp->get_attr_table().append_attr("d4:enum_label", "String", my_label);
btp->get_attr_table().append_container(enum_def, enum_def->get_name());
vector<BaseType *> *result = new vector<BaseType *>();
result->push_back(btp);
DBG(cerr << __func__ << "() - END" << endl;);
return result;
}
void D4Enum::m_check_value(int64_t v) const {
switch (d_element_type) {
case dods_byte_c:
case dods_uint8_c:
if ((uint64_t)v > DODS_UCHAR_MAX || v < 0) {
ostringstream oss;
oss << "The value " << v << " will not fit in an unsigned byte. (" << __func__ << ")";
throw Error(oss.str());
}
break;
case dods_uint16_c:
if ((uint64_t)v > DODS_USHRT_MAX || v < 0) {
ostringstream oss;
oss << "The value " << v << " will not fit in an unsigned 16-bit integer. (" << __func__ << ")";
throw Error(oss.str());
}
break;
case dods_uint32_c:
if ((uint64_t)v > DODS_UINT_MAX || v < 0) {
ostringstream oss;
oss << "The value " << v << " will not fit in an unsigned 32-bit integer. (" << __func__ << ")";
throw Error(oss.str());
}
break;
case dods_uint64_c:
// If 'v' can never be bigger than ULLONG_MAX
break;
case dods_int8_c:
if (v > DODS_SCHAR_MAX || v < DODS_SCHAR_MIN) {
ostringstream oss;
oss << "The value " << v << " will not fit in an unsigned byte. (" << __func__ << ")";
throw Error(oss.str());
}
break;
case dods_int16_c:
if (v > DODS_SHRT_MAX || v < DODS_SHRT_MIN) {
ostringstream oss;
oss << "The value " << v << " will not fit in an unsigned byte. (" << __func__ << ")";
throw Error(oss.str());
}
break;
case dods_int32_c:
if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
ostringstream oss;
oss << "The value " << v << " will not fit in an unsigned byte. (" << __func__ << ")";
throw Error(oss.str());
}
break;
case dods_int64_c:
// There's no value 'v' can have that won't fit into a 64-bit int.
break;
default:
assert(!"illegal type for D4Enum");
}
}
D4Enum::D4Enum(const string &name, const string &enum_type)
: BaseType(name, dods_enum_c, true /*is_dap4*/), d_buf(0), d_element_type(dods_null_c), d_enum_def(0) {
d_element_type = get_type(enum_type.c_str());
if (!is_integer_type(d_element_type))
d_element_type = dods_uint64_c;
set_is_signed(d_element_type);
}
D4Enum::D4Enum(const string &name, Type type)
: BaseType(name, dods_enum_c, true /*is_dap4*/), d_buf(0), d_element_type(type), d_enum_def(0) {
if (!is_integer_type(d_element_type))
d_element_type = dods_uint64_c;
set_is_signed(d_element_type);
}
D4Enum::D4Enum(const string &name, const string &dataset, Type type)
: BaseType(name, dataset, dods_enum_c, true /*is_dap4*/), d_buf(0), d_element_type(type), d_enum_def(0) {
if (!is_integer_type(d_element_type))
d_element_type = dods_uint64_c;
set_is_signed(d_element_type);
}
// Explicit instantiation of the template member function 'value(T *)'.
// This is required in order to have the library contain these member
// functions when its own code does not use them. Normally, C++ instantiates
// templates when they are used, and this forces that process so the
// library file contains the various versions of the member function.
template void D4Enum::value<dods_byte>(dods_byte *v) const;
template void D4Enum::value<dods_int16>(dods_int16 *v) const;
template void D4Enum::value<dods_uint16>(dods_uint16 *v) const;
template void D4Enum::value<dods_int32>(dods_int32 *v) const;
template void D4Enum::value<dods_uint32>(dods_uint32 *v) const;
template void D4Enum::value<dods_int64>(dods_int64 *v) const;
template void D4Enum::value<dods_uint64>(dods_uint64 *v) const;
template void D4Enum::set_value<dods_byte>(dods_byte v, bool check_value);
template void D4Enum::set_value<dods_int16>(dods_int16 v, bool check_value);
template void D4Enum::set_value<dods_uint16>(dods_uint16 v, bool check_value);
template void D4Enum::set_value<dods_int32>(dods_int32 v, bool check_value);
template void D4Enum::set_value<dods_uint32>(dods_uint32 v, bool check_value);
template void D4Enum::set_value<dods_int64>(dods_int64 v, bool check_value);
template void D4Enum::set_value<dods_uint64>(dods_uint64 v, bool check_value);
void D4Enum::set_enumeration(D4EnumDef *enum_def) {
d_enum_def = enum_def;
d_element_type = enum_def->type();
}
void D4Enum::compute_checksum(Crc32 &checksum) {
DBG(cerr << __func__ << ": element type: " << ::libdap::type_name(d_element_type) << endl);
switch (d_element_type) {
case dods_byte_c:
case dods_uint8_c:
case dods_int8_c: {
dods_byte v = static_cast<dods_byte>(d_buf);
checksum.AddData(reinterpret_cast<uint8_t *>(&v), sizeof(uint8_t));
break;
}
case dods_uint16_c:
case dods_int16_c: {
dods_int16 v = static_cast<dods_int16>(d_buf);
checksum.AddData(reinterpret_cast<uint8_t *>(&v), sizeof(uint16_t));
break;
}
case dods_uint32_c:
case dods_int32_c: {
dods_int32 v = static_cast<dods_int32>(d_buf);
checksum.AddData(reinterpret_cast<uint8_t *>(&v), sizeof(uint32_t));
break;
}
case dods_uint64_c:
case dods_int64_c:
checksum.AddData(reinterpret_cast<uint8_t *>(&d_buf), sizeof(uint64_t));
break;
default:
assert(!"illegal type for D4Enum");
}
}
void D4Enum::set_is_signed(Type t) {
switch (t) {
case dods_byte_c:
case dods_uint8_c:
case dods_uint16_c:
case dods_uint32_c:
case dods_uint64_c:
d_is_signed = false;
break;
case dods_int8_c:
case dods_int16_c:
case dods_int32_c:
case dods_int64_c:
d_is_signed = true;
break;
default:
#if 0
// I removed this because it hinders testing and is no better
// than throwing an exception. jhrg 3/29/18
assert(!"illegal type for D4Enum");
#endif
throw InternalErr(__FILE__, __LINE__, "Illegal type");
}
}
/**
* @brief Serialize a D4Enum
* Use the (integer) data type associated with an Enumeration definition to
* serialize the value of a D4Enum variable. This send just the bits that
* correspond to the declared type, not all 64-bits of storage used by a
* scalar D4Enum.
* @param m
* @param dmr Unused
* @param eval Unused
* @param filter Unused
* @exception Error is thrown if the value needs to be read and that operation fails.
*/
void D4Enum::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool) {
if (!read_p())
read(); // read() throws Error
switch (d_element_type) {
case dods_byte_c:
case dods_uint8_c:
m.put_byte(d_buf);
break;
case dods_uint16_c:
m.put_uint16(d_buf);
break;
case dods_uint32_c:
m.put_uint32(d_buf);
break;
case dods_uint64_c:
m.put_uint64(d_buf);
break;
case dods_int8_c:
m.put_int8(d_buf);
break;
case dods_int16_c:
m.put_int16(d_buf);
break;
case dods_int32_c:
m.put_int32(d_buf);
break;
case dods_int64_c:
m.put_int64(d_buf);
break;
default:
assert(!"illegal type for D4Enum");
}
}
void D4Enum::deserialize(D4StreamUnMarshaller &um, DMR &) {
switch (d_element_type) {
case dods_byte_c:
case dods_uint8_c: {
dods_byte v;
um.get_byte(v);
d_buf = v;
break;
}
case dods_uint16_c: {
dods_uint16 v;
um.get_uint16(v);
d_buf = v;
break;
}
case dods_uint32_c: {
dods_uint32 v;
um.get_uint32(v);
d_buf = v;
break;
}
case dods_uint64_c: {
dods_uint64 v;
um.get_uint64(v);
d_buf = v;
break;
}
case dods_int8_c: {
dods_int8 v;
um.get_int8(v);
d_buf = v;
break;
}
case dods_int16_c: {
dods_int16 v;
um.get_int16(v);
d_buf = v;
break;
}
case dods_int32_c: {
dods_int32 v;
um.get_int32(v);
d_buf = v;
break;
}
case dods_int64_c: {
dods_int64 v;
um.get_int64(v);
d_buf = v;
break;
}
default:
assert(!"illegal type for D4Enum");
}
}
unsigned int D4Enum::val2buf(void *val, bool) {
if (!val)
throw InternalErr("The incoming pointer does not contain any data.");
switch (d_element_type) {
case dods_byte_c:
case dods_uint8_c:
d_buf = *(dods_byte *)val;
break;
case dods_uint16_c:
d_buf = *(dods_uint16 *)val;
break;
case dods_uint32_c:
d_buf = *(dods_uint32 *)val;
break;
case dods_uint64_c:
d_buf = *(dods_uint64 *)val;
break;
case dods_int8_c:
d_buf = *(dods_int8 *)val;
break;
case dods_int16_c:
d_buf = *(dods_int16 *)val;
break;
case dods_int32_c:
d_buf = *(dods_int32 *)val;
break;
case dods_int64_c:
d_buf = *(dods_int64 *)val;
break;
default:
assert(!"illegal type for D4Enum");
}
return width();
}
unsigned int D4Enum::buf2val(void **val) {
if (!val)
throw InternalErr("NULL pointer");
switch (d_element_type) {
case dods_byte_c:
case dods_uint8_c:
if (!*val)
*val = new dods_byte;
*(dods_byte *)*val = d_buf;
break;
case dods_uint16_c:
if (!*val)
*val = new dods_uint16;
*(dods_uint16 *)*val = d_buf;
break;
case dods_uint32_c:
if (!*val)
*val = new dods_uint32;
*(dods_uint32 *)*val = d_buf;
break;
case dods_uint64_c:
if (!*val)
*val = new dods_uint64;
*(dods_uint64 *)*val = d_buf;
break;
case dods_int8_c:
if (!*val)
*val = new dods_int8;
*(dods_int8 *)*val = d_buf;
break;
case dods_int16_c:
if (!*val)
*val = new dods_int16;
*(dods_int16 *)*val = d_buf;
break;
case dods_int32_c:
if (!*val)
*val = new dods_int32;
*(dods_int32 *)*val = d_buf;
break;
case dods_int64_c:
if (!*val)
*val = new dods_int64;
*(dods_int64 *)*val = d_buf;
break;
default:
assert(!"illegal type for D4Enum");
}
return width();
}
void D4Enum::print_val(ostream &out, string space, bool print_decl_p) {
if (print_decl_p) {
print_decl(out, space, false);
out << " = ";
}
DBG(cerr << "Enum union value: " << hex << d_buf << dec << endl);
if (is_signed()) {
int64_t v;
value(&v);
out << v;
} else {
uint64_t v;
value(&v);
out << v;
}
if (print_decl_p)
out << ";" << endl;
}
/** Write the XML representation of this variable. This method is used to
build the DDX XML response.
@param out Destination output stream
@param space Use this to indent child declarations. Default is "".
@param constrained If true, only print this if it's part part of the
current projection. Default is False. */
void D4Enum::print_xml_writer(XMLWriter &xml, bool constrained) {
if (constrained && !send_p())
return;
if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"Enum") < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write Enum element");
if (!name().empty())
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"name", (const xmlChar *)name().c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
string path = d_enum_def->name();
// Not every D4EnumDef is a member of an instance of D4EnumDefs - the D4EnumDefs instance
// holds a reference to the D4Group that holds the Enum definitions.
// TODO Should this be changed - so the EnumDef holds a reference to its parent Group?
if (d_enum_def->parent()) {
// print the FQN for the enum def; D4Group::FQN() includes the trailing '/'
path = static_cast<D4Group *>(d_enum_def->parent()->parent())->FQN() + path;
}
if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"enum", (const xmlChar *)path.c_str()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not write attribute for enum");
attributes()->print_dap4(xml);
if (get_attr_table().get_size() > 0)
get_attr_table().print_xml_writer(xml);
if (xmlTextWriterEndElement(xml.get_writer()) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not end Enum element");
}
bool D4Enum::ops(BaseType *b, int op) {
// Get the arg's value.
if (!read_p() && !read())
throw InternalErr(__FILE__, __LINE__, "This value not read!");
// Get the second arg's value.
if (!b->read_p() && !b->read())
throw InternalErr(__FILE__, __LINE__, "This value not read!");
switch (b->type()) {
case dods_int8_c:
return Cmp<dods_int64, dods_int8>(op, d_buf, static_cast<Int8 *>(b)->value());
case dods_byte_c:
return Cmp<dods_int64, dods_byte>(op, d_buf, static_cast<Byte *>(b)->value());
case dods_int16_c:
return Cmp<dods_int64, dods_int16>(op, d_buf, static_cast<Int16 *>(b)->value());
case dods_uint16_c:
return Cmp<dods_int64, dods_uint16>(op, d_buf, static_cast<UInt16 *>(b)->value());
case dods_int32_c:
return Cmp<dods_int64, dods_int32>(op, d_buf, static_cast<Int32 *>(b)->value());
case dods_uint32_c:
return Cmp<dods_int64, dods_uint32>(op, d_buf, static_cast<UInt32 *>(b)->value());
#if 0
// FIXME
case dods_int64_c:
return Cmp<dods_int64, dods_int64>(op, d_buf, static_cast<D4Enum*>(b)->value());
case dods_uint64_c:
return Cmp<dods_int64, dods_uint64>(op, d_buf, static_cast<D4Enum*>(b)->value());
#endif
case dods_float32_c:
return Cmp<dods_int64, dods_float32>(op, d_buf, static_cast<Float32 *>(b)->value());
case dods_float64_c:
return Cmp<dods_int64, dods_float64>(op, d_buf, static_cast<Float64 *>(b)->value());
default:
return false;
}
}
/** @brief dumps information about this object
*
* Displays the pointer value of this instance and information about this
* instance.
*
* @param strm C++ i/o stream to dump the information to
* @return void
*/
void D4Enum::dump(ostream &strm) const {
strm << DapIndent::LMarg << "D4Enum::dump - (" << (void *)this << ")" << endl;
DapIndent::Indent();
BaseType::dump(strm);
strm << DapIndent::LMarg << "value: " << d_buf << endl;
DapIndent::UnIndent();
}
} // namespace libdap