libdap++  Updated for version 3.14.0
D4Enum.h
Go to the documentation of this file.
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 
33 #include "InternalErr.h"
34 #include "dods-datatypes.h"
35 #include "util.h"
36 
37 namespace libdap
38 {
39 
40 class D4EnumDef;
41 class ConstraintEvaluator;
42 class Marshaller;
43 class UnMarshaller;
44 
55 class D4Enum: public BaseType
56 {
57  friend class D4EnumTest;
58 
59 public:
60  union enum_value {
61  int8_t i8;
62  uint8_t ui8;
63  int16_t i16;
64  uint16_t ui16;
65  int32_t i32;
66  uint32_t ui32;
67  int64_t i64;
68  uint64_t ui64;
69 
70  enum_value() : ui64(0) { }
71 
72  enum_value(int8_t i) : i8(i) {}
73  enum_value(uint8_t i) : ui8(i) {}
74  enum_value(int16_t i) : i16(i) {}
75  enum_value(uint16_t i) : ui16(i) {}
76  enum_value(int32_t i) : i32(i) {}
77  enum_value(uint32_t i) : ui32(i) {}
78  enum_value(int64_t i) : i64(i) {}
79  enum_value(uint64_t i) : ui64(i) {}
80 
81  // cast operators; use by set_value()
82  operator int8_t() const { return i8; }
83  operator uint8_t() const { return ui8; }
84  operator int16_t() const { return i16; }
85  operator uint16_t() const { return ui16; }
86  operator int32_t() const { return i32; }
87  operator uint32_t() const { return ui32; }
88  operator int64_t() const { return i64; }
89  operator uint64_t() const { return ui64; }
90  };
91 
92 private:
93  enum_value d_buf;
94 
95  Type d_element_type;
96  D4EnumDef *d_enum_def; // The enumeration defined in the DMR, not an integer type
97  bool d_is_signed;
98 
99  void m_duplicate(const D4Enum &src) {
100  d_buf = src.d_buf;
101  d_element_type = src.d_element_type;
102  }
103 
104  unsigned int m_type_width() const {
105  switch(d_element_type) {
106  case dods_byte_c:
107  case dods_int8_c:
108  case dods_uint8_c:
109  return 1;
110  case dods_int16_c:
111  case dods_uint16_c:
112  return 2;
113  case dods_int32_c:
114  case dods_uint32_c:
115  return 4;
116  case dods_int64_c:
117  case dods_uint64_c:
118  return 8;
119  case dods_null_c:
120  default:
121  assert(!"illegal type for D4Enum");
122  return 0;
123  }
124  }
125 
126  D4Enum(); // No empty constructor
127 
128 public:
129  // TODO add a way to set the EnumDef to these
130  D4Enum(const string &name, const string &enum_type) : BaseType(name, dods_enum_c, true /*is_dap4*/),
131  d_buf((uint64_t)0), d_element_type(dods_null_c) {
132  d_element_type = get_type(enum_type.c_str());
133  // assert(is_integer_type(d_element_type));
134  if (!is_integer_type(d_element_type)) d_element_type = dods_uint64_c;
135  set_is_signed(d_element_type);
136  }
137 
138  D4Enum(const string &name, Type type) : BaseType(name, dods_enum_c, true /*is_dap4*/),
139  d_buf((uint64_t)0), d_element_type(type) {
140  //assert(is_integer_type(d_element_type));
141  if (!is_integer_type(d_element_type)) d_element_type = dods_uint64_c;
142  set_is_signed(d_element_type);
143  }
144 
145  D4Enum(const string &name, const string &dataset, Type type) : BaseType(name, dataset, dods_enum_c, true /*is_dap4*/),
146  d_buf((uint64_t)0), d_element_type(type) {
147  //assert(is_integer_type(d_element_type));
148  if (!is_integer_type(d_element_type)) d_element_type = dods_uint64_c;
149  set_is_signed(d_element_type);
150  }
151 
152  D4Enum(const D4Enum &src) : BaseType(src) { m_duplicate(src); }
153 
154  D4Enum &operator=(const D4Enum &rhs) {
155  if (this == &rhs)
156  return *this;
157  static_cast<BaseType &>(*this) = rhs;
158  m_duplicate(rhs);
159  return *this;
160  }
161 
162  virtual ~D4Enum() { }
163 
164  virtual D4EnumDef *enumeration() const { return d_enum_def; }
165  virtual void set_enumeration(D4EnumDef *enum_def);
166 
167  virtual BaseType *ptr_duplicate() { return new D4Enum(*this); }
168 
169  Type element_type() { return d_element_type; }
170  void set_element_type(Type type) { d_element_type = type; }
171 
172  bool is_signed() const { return d_is_signed; }
173  void set_is_signed(Type t) {
174  switch (t) {
175  case dods_byte_c:
176  case dods_uint8_c:
177  case dods_uint16_c:
178  case dods_uint32_c:
179  case dods_uint64_c:
180  d_is_signed = false;
181  break;
182 
183  case dods_int8_c:
184  case dods_int16_c:
185  case dods_int32_c:
186  case dods_int64_c:
187  d_is_signed = true;
188  break;
189 
190  default:
191  assert(!"illegal type for D4Enum");
192  throw InternalErr(__FILE__, __LINE__, "Illegal type");
193  }
194  }
195 
206  template<typename T> void value(T *v) const
207  {
208  switch (d_element_type) {
209  case dods_byte_c:
210  case dods_uint8_c:
211  *v = static_cast<T>(d_buf.ui8);
212  break;
213  case dods_uint16_c:
214  *v = static_cast<T>(d_buf.ui16);
215  break;
216  case dods_uint32_c:
217  *v = static_cast<T>(d_buf.ui32);
218  break;
219  case dods_uint64_c:
220  *v = static_cast<T>(d_buf.ui64);
221  break;
222 
223  case dods_int8_c:
224  *v = static_cast<T>(d_buf.i8);
225  break;
226  case dods_int16_c:
227  *v = static_cast<T>(d_buf.i16);
228  break;
229  case dods_int32_c:
230  *v = static_cast<T>(d_buf.i32);
231  break;
232  case dods_int64_c:
233  *v = static_cast<T>(d_buf.i64);
234  break;
235  default:
236  assert(!"illegal type for D4Enum");
237  }
238  }
239 
248  template <typename T> void set_value(T v) { d_buf = v; }
249 
259  virtual unsigned int width(bool /* constrained */ = false) const { return m_type_width(); }
260 
261  // DAP4
262  virtual void compute_checksum(Crc32 &checksum);
263  virtual void serialize(D4StreamMarshaller &m, DMR &dmr, /*ConstraintEvaluator &eval,*/ bool filter = false);
264  virtual void deserialize(D4StreamUnMarshaller &um, DMR &dmr);
265 
266  virtual void print_val(ostream &out, string space = "", bool print_decl_p = true);
267 
268  virtual void print_xml_writer(XMLWriter &xml, bool constrained);
269 
270  virtual bool ops(BaseType *b, int op);
271 
272  virtual void dump(ostream &strm) const;
273 
274  unsigned int val2buf(void *, bool);
275  unsigned int buf2val(void **);
276 };
277 
278 } // namespace libdap
279 
280 #endif // _D4Enum_h
281 
virtual void set_enumeration(D4EnumDef *enum_def)
Definition: D4Enum.cc:83
D4Enum(const string &name, const string &enum_type)
Definition: D4Enum.h:130
void set_value(T v)
Set the value of the Enum Template member function to set the value of the Enum. The libdap library c...
Definition: D4Enum.h:248
unsigned int val2buf(void *, bool)
Loads class data.
Definition: D4Enum.cc:201
friend class D4EnumTest
Definition: D4Enum.h:57
bool is_signed() const
Definition: D4Enum.h:172
Read data from the stream made by D4StreamMarshaller.
virtual ~D4Enum()
Definition: D4Enum.h:162
Definition: crc.h:76
enum_value(int16_t i)
Definition: D4Enum.h:74
D4Enum(const D4Enum &src)
Definition: D4Enum.h:152
virtual void print_val(ostream &out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: D4Enum.cc:287
enum_value(uint64_t i)
Definition: D4Enum.h:79
Type
Identifies the data type.
Definition: Type.h:94
Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:306
enum_value(uint32_t i)
Definition: D4Enum.h:77
A class for software fault reporting.
Definition: InternalErr.h:64
string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:299
D4Enum(const string &name, Type type)
Definition: D4Enum.h:138
Holds a DAP4 enumeration.
Definition: D4Enum.h:55
virtual void serialize(D4StreamMarshaller &m, DMR &dmr, bool filter=false)
Serialize a D4Enum Use the (integer) data type associated with an Enumeration definition to serialize...
Definition: D4Enum.cc:129
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
D4Enum & operator=(const D4Enum &rhs)
Definition: D4Enum.h:154
void set_element_type(Type type)
Definition: D4Enum.h:170
enum_value(uint8_t i)
Definition: D4Enum.h:73
ObjectType get_type(const string &value)
Definition: mime_util.cc:326
virtual void compute_checksum(Crc32 &checksum)
include the data for this variable in the checksum DAP4 includes a checksum with every data response...
Definition: D4Enum.cc:89
Type element_type()
Definition: D4Enum.h:169
void set_is_signed(Type t)
Definition: D4Enum.h:173
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:261
virtual void deserialize(D4StreamUnMarshaller &um, DMR &dmr)
Definition: D4Enum.cc:167
enum_value(uint16_t i)
Definition: D4Enum.h:75
virtual void print_xml_writer(XMLWriter &xml, bool constrained)
Definition: D4Enum.cc:316
virtual BaseType * ptr_duplicate()
Definition: D4Enum.h:167
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
virtual unsigned int width(bool=false) const
Return the number of bytes in an instance of an Enum. This returns the number of bytes an instance of...
Definition: D4Enum.h:259
virtual D4EnumDef * enumeration() const
Definition: D4Enum.h:164
void value(T *v) const
Copy the value of this Enum into v. Template member function that can be used to read the value of th...
Definition: D4Enum.h:206
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: D4Enum.cc:351
D4Enum(const string &name, const string &dataset, Type type)
Definition: D4Enum.h:145
enum_value(int64_t i)
Definition: D4Enum.h:78
enum_value(int32_t i)
Definition: D4Enum.h:76
unsigned int buf2val(void **)
Reads the class data.
Definition: D4Enum.cc:240
virtual void dump(ostream &strm) const
dumps information about this object
Definition: D4Enum.cc:401
bool is_integer_type(Type t)
Definition: util.cc:948