libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4EnumDefs.cc
Go to the documentation of this file.
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of libdap, A C++ implementation of the OPeNDAP Data
4// Access Protocol.
5
6// Copyright (c) 2013 OPeNDAP, Inc.
7// Author: James Gallagher <jgallagher@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25#include "config.h"
26
27#include "D4EnumDefs.h"
28#include "D4Group.h"
29
30#include <sstream>
31
32#include "dods-limits.h"
33#include "util.h"
34
35namespace libdap {
36
43 switch (type()) {
44 case dods_int8_c:
45 return (value >= DODS_SCHAR_MIN && value <= DODS_SCHAR_MAX);
46 case dods_byte_c:
47 case dods_uint8_c:
48 return (value >= 0 && static_cast<unsigned long long>(value) <= DODS_UCHAR_MAX);
49 case dods_int16_c:
50 return (value >= DODS_SHRT_MIN && value <= DODS_SHRT_MAX);
51 case dods_uint16_c:
52 return (value >= 0 && static_cast<unsigned long long>(value) <= DODS_USHRT_MAX);
53 case dods_int32_c:
54 return (value >= DODS_INT_MIN && value <= DODS_INT_MAX);
55 case dods_uint32_c:
56 return (value >= 0 && static_cast<unsigned long long>(value) <= DODS_UINT_MAX);
57 case dods_int64_c:
58 return true; // This is always true: (value >= DODS_LLONG_MIN && value <= DODS_LLONG_MAX);
59 case dods_uint64_c:
60 return (value >= 0 /*Always true: && static_cast<unsigned long long>(value) <= DODS_ULLONG_MAX*/);
61 default:
62 return false;
63 }
64}
65
66#if 0
67// Note that in order for this to work the second argument must not be a reference.
68// jhrg 8/20/13
69static bool
70enum_def_name_eq(D4EnumDef *d, const string name)
71{
72 return d->name() == name;
73}
74#endif
75
77 auto d = find_if(d_enums.begin(), d_enums.end(), [name](const D4EnumDef *def) { return name == def->name(); });
78
79 return (d != d_enums.end()) ? *d : nullptr;
80}
81
82void D4EnumDef::print_value(XMLWriter &xml, const D4EnumDef::tuple &tuple) const {
83 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"EnumConst") < 0)
84 throw InternalErr(__FILE__, __LINE__, "Could not write EnumConst element");
85
86 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"name", (const xmlChar *)tuple.label.c_str()) <
87 0)
88 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
89
90 ostringstream oss;
91 oss << tuple.value;
92 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"value", (const xmlChar *)oss.str().c_str()) < 0)
93 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for value");
94
95 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
96 throw InternalErr(__FILE__, __LINE__, "Could not end EnumConst element");
97}
98
100 vector<D4EnumDef::tuple>::const_iterator i = d_tuples.begin();
101 while (i != d_tuples.end()) {
102 print_value(xml, *i++);
103 }
104}
105
106void D4EnumDefs::m_print_enum(XMLWriter &xml, D4EnumDef *e) const {
107 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"Enumeration") < 0)
108 throw InternalErr(__FILE__, __LINE__, "Could not write Enumeration element");
109
110 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"name", (const xmlChar *)e->name().c_str()) < 0)
111 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
112
113 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"basetype",
114 (const xmlChar *)D4type_name(e->type()).c_str()) < 0)
115 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
116
117 // print each of e.values
118 e->print_dap4(xml);
119
120 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
121 throw InternalErr(__FILE__, __LINE__, "Could not end Enumeration element");
122}
123
124void D4EnumDefs::print_dap4(XMLWriter &xml, bool constrained) const {
125 D4EnumDefCIter i = d_enums.begin();
126 while (i != d_enums.end()) {
127 if (!constrained || parent()->find_first_var_that_uses_enumeration(*i))
128 m_print_enum(xml, *i);
129 ++i;
130 }
131}
132
133} /* namespace libdap */
Type type() const
Definition D4EnumDefs.h:84
long long value(D4EnumValueIter i)
Definition D4EnumDefs.h:97
void print_dap4(XMLWriter &xml) const
Definition D4EnumDefs.cc:99
string name() const
Definition D4EnumDefs.h:81
bool is_valid_enum_value(long long value)
Definition D4EnumDefs.cc:42
void print_dap4(XMLWriter &xml, bool constrained=false) const
vector< D4EnumDef * >::const_iterator D4EnumDefCIter
Definition D4EnumDefs.h:122
D4Group * parent() const
Definition D4EnumDefs.h:143
D4EnumDef * find_enum_def(const string &name)
Definition D4EnumDefs.cc:76
A class for software fault reporting.
Definition InternalErr.h:61
xmlTextWriterPtr get_writer() const
Definition XMLWriter.h:55
STL iterator class.
#define DODS_SHRT_MIN
Definition dods-limits.h:68
#define DODS_UCHAR_MAX
Definition dods-limits.h:65
#define DODS_SCHAR_MIN
Definition dods-limits.h:63
#define DODS_SHRT_MAX
Definition dods-limits.h:69
#define DODS_UINT_MAX
Definition dods-limits.h:74
#define DODS_INT_MAX
Definition dods-limits.h:73
#define DODS_INT_MIN
Definition dods-limits.h:72
#define DODS_USHRT_MAX
Definition dods-limits.h:70
#define DODS_SCHAR_MAX
Definition dods-limits.h:64
top level DAP object to house generic methods
Definition AISConnect.cc:30
@ dods_uint32_c
Definition Type.h:100
@ dods_int16_c
Definition Type.h:97
@ dods_byte_c
Definition Type.h:96
@ dods_int32_c
Definition Type.h:99
@ dods_int8_c
Definition Type.h:115
@ dods_int64_c
Definition Type.h:118
@ dods_uint64_c
Definition Type.h:119
@ dods_uint16_c
Definition Type.h:98
@ dods_uint8_c
Definition Type.h:116
string D4type_name(Type t)
Returns the type of the class instance as a string. Supports all DAP4 types and not the DAP2-only typ...
Definition util.cc:688