libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4Enum.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2013 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#ifndef _D4Enum_h
27#define _D4Enum_h 1
28
29#include <cassert>
30
31#include "BaseType.h"
32#include "dods-datatypes.h"
33
34
35namespace libdap
36{
37
38class D4EnumDef;
39class ConstraintEvaluator;
40class Marshaller;
41class UnMarshaller;
42
55class D4Enum: public BaseType
56{
57 friend class D4EnumTest;
58
59protected:
60 // Use an unsigned 64-bit int. the value() and set_value()
61 // accessors cast to other types as needed, including signed ones.
62 uint64_t d_buf;
63
64private:
65 Type d_element_type;
66 D4EnumDef *d_enum_def; // This is a weak pointer; don't delete
67 bool d_is_signed;
68
69 void m_duplicate(const D4Enum &src);
70 void m_check_value(int64_t v) const;
71
72 unsigned int m_type_width() const {
73 switch(d_element_type) {
74 case dods_byte_c:
75 case dods_int8_c:
76 case dods_uint8_c:
77 return 1;
78 case dods_int16_c:
79 case dods_uint16_c:
80 return 2;
81 case dods_int32_c:
82 case dods_uint32_c:
83 return 4;
84 case dods_int64_c:
85 case dods_uint64_c:
86 return 8;
87 case dods_null_c:
88 default:
89 assert(!"illegal type for D4Enum");
90 return 0;
91 }
92 }
93
94 D4Enum(); // No empty constructor
95
96public:
97 D4Enum(const string &name, const string &enum_type);
98
99 D4Enum(const string &name, Type type);
100
101 D4Enum(const string &name, const string &dataset, Type type);
102
103 D4Enum(const D4Enum &src) : BaseType(src) { m_duplicate(src); }
104
105 D4Enum &operator=(const D4Enum &rhs) {
106 if (this == &rhs)
107 return *this;
108 BaseType::operator=(rhs);
109 m_duplicate(rhs);
110 return *this;
111 }
112
113 virtual ~D4Enum() { }
114
115 virtual D4EnumDef *enumeration() const { return d_enum_def; }
116 virtual void set_enumeration(D4EnumDef *enum_def);
117
118 BaseType *ptr_duplicate() override{ return new D4Enum(*this); }
119
120 Type element_type() { return d_element_type; }
121 void set_element_type(Type type) { d_element_type = type; }
122
123 bool is_signed() const { return d_is_signed; }
124 void set_is_signed(Type t);
125
134 template<typename T> void value(T *v) const {
135 *v = static_cast<T>(d_buf);
136 }
137
148 template <typename T> void set_value(T v, bool check_value = true)
149 {
150 if (check_value) m_check_value(v);
151 d_buf = static_cast<int64_t>(v);
152 }
153
169 unsigned int width(bool /* constrained */ = false) const override
170 {
171 return (int)m_type_width();
172 }
173
174 int64_t width_ll(bool /* constrained */ = false) const override
175 {
176 return (int64_t)m_type_width();
177 }
178
179 // DAP4
180 void compute_checksum(Crc32 &checksum) override;
181 void serialize(D4StreamMarshaller &m, DMR &dmr, /*ConstraintEvaluator &eval,*/ bool filter = false) override;
182 void deserialize(D4StreamUnMarshaller &um, DMR &dmr) override;
183
184 void print_val(ostream &out, string space = "", bool print_decl_p = true) override;
185
186 void print_xml_writer(XMLWriter &xml, bool constrained) override;
187
188 bool ops(BaseType *b, int op) override;
189
190 void dump(ostream &strm) const override;
191
192 unsigned int val2buf(void *, bool);
193 unsigned int buf2val(void **);
194
195 std::vector<BaseType *> *transform_to_dap2(AttrTable *parent_attr_table) override;
196
197};
198
199} // namespace libdap
200
201#endif // _D4Enum_h
202
Definition: crc.h:79
The basic data type for the DODS DAP types.
Definition: BaseType.h:120
virtual string name() const
Returns the name of the class instance.
Definition: BaseType.cc:317
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:355
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
virtual Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:362
Holds a DAP4 enumeration.
Definition: D4Enum.h:56
void print_xml_writer(XMLWriter &xml, bool constrained) override
Definition: D4Enum.cc:612
bool ops(BaseType *b, int op) override
Evaluate relational operators.
Definition: D4Enum.cc:647
std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table) override
Convert an Enum to a DAP2 int type.
Definition: D4Enum.cc:92
unsigned int buf2val(void **)
Reads the class data.
Definition: D4Enum.cc:534
BaseType * ptr_duplicate() override
Definition: D4Enum.h:118
void print_val(ostream &out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
Definition: D4Enum.cc:581
unsigned int width(bool=false) const override
Return the number of bytes in an instance of an Enum. This returns the number of bytes an instance of...
Definition: D4Enum.h:169
void compute_checksum(Crc32 &checksum) override
include the data for this variable in the checksum DAP4 includes a checksum with every data response....
Definition: D4Enum.cc:323
unsigned int val2buf(void *, bool)
Loads class data.
Definition: D4Enum.cc:495
void serialize(D4StreamMarshaller &m, DMR &dmr, bool filter=false) override
Serialize a D4Enum Use the (integer) data type associated with an Enumeration definition to serialize...
Definition: D4Enum.cc:399
void set_value(T v, bool check_value=true)
Set the value of the Enum Template member function to set the value of the Enum. The libdap library c...
Definition: D4Enum.h:148
void value(T *v) const
Get the value of an Enum Get the value of this instance. The caller is responsible for using a type T...
Definition: D4Enum.h:134
void dump(ostream &strm) const override
dumps information about this object
Definition: D4Enum.cc:695
void deserialize(D4StreamUnMarshaller &um, DMR &dmr) override
Definition: D4Enum.cc:437
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
Type
Identifies the data type.
Definition: Type.h:94