-
Notifications
You must be signed in to change notification settings - Fork 14
/
D4Enum.h
189 lines (152 loc) · 6.1 KB
/
D4Enum.h
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
// -*- 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.
#ifndef _D4Enum_h
#define _D4Enum_h 1
#include <cassert>
#include "BaseType.h"
#include "dods-datatypes.h"
namespace libdap {
class D4EnumDef;
class ConstraintEvaluator;
class Marshaller;
class UnMarshaller;
/**
* @brief Holds a DAP4 enumeration.
*
* @note When constructed a type for the Enum must be specified. If
* it is not an integer type, the Enum will use unsigned int 64. This
* is not the same as the enumeration type that is defined using the
* Enumeration XML element in the DMR - that information is stored
* in additional fields and used for checking values and printing the
* variable's declaration, but not for the internal storage of values.
*
* @todo Note the hack to remove the union...
*/
class D4Enum : public BaseType {
friend class D4EnumTest;
protected:
// Use an unsigned 64-bit int. the value() and set_value()
// accessors cast to other types as needed, including signed ones.
uint64_t d_buf;
private:
Type d_element_type;
D4EnumDef *d_enum_def; // This is a weak pointer; don't delete
bool d_is_signed;
void m_duplicate(const D4Enum &src);
void m_check_value(int64_t v) const;
unsigned int m_type_width() const {
switch (d_element_type) {
case dods_byte_c:
case dods_int8_c:
case dods_uint8_c:
return 1;
case dods_int16_c:
case dods_uint16_c:
return 2;
case dods_int32_c:
case dods_uint32_c:
return 4;
case dods_int64_c:
case dods_uint64_c:
return 8;
case dods_null_c:
default:
assert(!"illegal type for D4Enum");
return 0;
}
}
D4Enum(); // No empty constructor
public:
D4Enum(const string &name, const string &enum_type);
D4Enum(const string &name, Type type);
D4Enum(const string &name, const string &dataset, Type type);
D4Enum(const D4Enum &src) : BaseType(src) { m_duplicate(src); }
D4Enum &operator=(const D4Enum &rhs) {
if (this == &rhs)
return *this;
BaseType::operator=(rhs);
m_duplicate(rhs);
return *this;
}
virtual ~D4Enum() {}
virtual D4EnumDef *enumeration() const { return d_enum_def; }
virtual void set_enumeration(D4EnumDef *enum_def);
BaseType *ptr_duplicate() override { return new D4Enum(*this); }
Type element_type() { return d_element_type; }
void set_element_type(Type type) { d_element_type = type; }
bool is_signed() const { return d_is_signed; }
void set_is_signed(Type t);
/**
* @brief Get the value of an Enum
* Get the value of this instance. The caller is responsible
* for using a type T than can hold the value.
*
* @param v Value-result parameter; return the value of the Enum
* in this variable.
*/
template <typename T> void value(T *v) const { *v = static_cast<T>(d_buf); }
/**
* @brief Set the value of the Enum
* Template member function to set the value of the Enum. The libdap library
* contains versions of this member function for dods_byte, ..., dods_uint64
* types for the parameter \c v.
*
* @param v Set the Enum to this value.
* @param check_value If true test the value 'v' against the type of the
* Enum. Defaults to true.
*/
template <typename T> void set_value(T v, bool check_value = true) {
if (check_value)
m_check_value(v);
d_buf = static_cast<int64_t>(v);
}
/**
* @brief Return the number of bytes in an instance of an Enum.
* This returns the number of bytes an instance of Enum will use
* in memory or on the wire (i.e., in a serialization of
* the type). On the wire this type uses the minimum number of
* bytes for the given Enum type - an Enum with type Byte uses
* one byte, Int16 uses two, and so on. In memory, a single instance
* uses 64-bits but a vector of these will use the same number of
* bytes per value as the on-the-wire representation.
*
* @note The private method m_type_width() returns the byte width
* used for the on-the-wire representation of values.
*
* @return The number of bytes used by a value.
*/
unsigned int width(bool /* constrained */ = false) const override { return (int)m_type_width(); }
int64_t width_ll(bool /* constrained */ = false) const override { return (int64_t)m_type_width(); }
// DAP4
void compute_checksum(Crc32 &checksum) override;
void serialize(D4StreamMarshaller &m, DMR &dmr, /*ConstraintEvaluator &eval,*/ bool filter = false) override;
void deserialize(D4StreamUnMarshaller &um, DMR &dmr) override;
void print_val(ostream &out, string space = "", bool print_decl_p = true) override;
void print_xml_writer(XMLWriter &xml, bool constrained) override;
bool ops(BaseType *b, int op) override;
void dump(ostream &strm) const override;
unsigned int val2buf(void *, bool) override;
unsigned int buf2val(void **) override;
std::vector<BaseType *> *transform_to_dap2(AttrTable *parent_attr_table) override;
};
} // namespace libdap
#endif // _D4Enum_h