libdap  Updated for version 3.17.0
D4Enum.h
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2013 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 #ifndef _D4Enum_h
00027 #define _D4Enum_h 1
00028 
00029 #include <cassert>
00030 
00031 #include "BaseType.h"
00032 #include "dods-datatypes.h"
00033 
00034 #if 0
00035 #include "InternalErr.h"
00036 #include "dods-datatypes.h"
00037 #include "dods-limits.h"
00038 #include "util.h"
00039 #endif
00040 
00041 namespace libdap
00042 {
00043 
00044 class D4EnumDef;
00045 class ConstraintEvaluator;
00046 class Marshaller;
00047 class UnMarshaller;
00048 
00061 class D4Enum: public BaseType
00062 {
00063         friend class D4EnumTest;
00064 
00065 protected:
00066     // Use an unsigned 64-bit int. the value() and set_value()
00067     // accessors cast to other types as needed, including signed ones.
00068     uint64_t d_buf;
00069 
00070 private:
00071     Type d_element_type;
00072     D4EnumDef *d_enum_def;      // This is a weak pointer; don't delete
00073     bool d_is_signed;
00074 
00075     void m_duplicate(const D4Enum &src);
00076     void m_check_value(int64_t v) const;
00077 
00078     unsigned int m_type_width() const {
00079         switch(d_element_type) {
00080             case dods_byte_c:
00081             case dods_int8_c:
00082             case dods_uint8_c:
00083                 return 1;
00084             case dods_int16_c:
00085             case dods_uint16_c:
00086                 return 2;
00087             case dods_int32_c:
00088             case dods_uint32_c:
00089                 return 4;
00090             case dods_int64_c:
00091             case dods_uint64_c:
00092                 return 8;
00093             case dods_null_c:
00094             default:
00095                 assert(!"illegal type for D4Enum");
00096                 return 0;
00097         }
00098     }
00099 
00100     D4Enum();   // No empty constructor
00101 
00102 public:
00103     D4Enum(const string &name, const string &enum_type);
00104 
00105     D4Enum(const string &name, Type type);
00106 
00107     D4Enum(const string &name, const string &dataset, Type type);
00108 
00109     D4Enum(const D4Enum &src) : BaseType(src) { m_duplicate(src); }
00110 
00111     D4Enum &operator=(const D4Enum &rhs) {
00112         if (this == &rhs)
00113             return *this;
00114         static_cast<BaseType &>(*this) = rhs;
00115         m_duplicate(rhs);
00116         return *this;
00117     }
00118 
00119     virtual ~D4Enum() { }
00120 
00121     virtual D4EnumDef *enumeration() const { return d_enum_def; }
00122     virtual void set_enumeration(D4EnumDef *enum_def);
00123 
00124     virtual BaseType *ptr_duplicate() { return new D4Enum(*this); }
00125 
00126     Type element_type() { return d_element_type; }
00127     void set_element_type(Type type) { d_element_type = type; }
00128 
00129     bool is_signed() const { return d_is_signed; }
00130     void set_is_signed(Type t);
00131 
00140         template<typename T> void value(T *v) const {
00141                 *v = static_cast<T>(d_buf);
00142         }
00143 
00154     template <typename T> void set_value(T v, bool check_value = true)
00155     {
00156         if (check_value) m_check_value(v);
00157         d_buf = static_cast<int64_t>(v);
00158     }
00159 
00175     virtual unsigned int width(bool /* constrained */ = false) const { return /*sizeof(int64_t);*/ m_type_width();}
00176 
00177     // DAP4
00178     virtual void compute_checksum(Crc32 &checksum);
00179     virtual void serialize(D4StreamMarshaller &m, DMR &dmr, /*ConstraintEvaluator &eval,*/ bool filter = false);
00180     virtual void deserialize(D4StreamUnMarshaller &um, DMR &dmr);
00181 
00182     virtual void print_val(ostream &out, string space = "", bool print_decl_p = true);
00183 
00184     virtual void print_xml_writer(XMLWriter &xml, bool constrained);
00185 
00186     virtual bool ops(BaseType *b, int op);
00187 
00188     virtual void dump(ostream &strm) const;
00189 
00190     unsigned int val2buf(void *, bool);
00191     unsigned int buf2val(void **);
00192 };
00193 
00194 } // namespace libdap
00195 
00196 #endif // _D4Enum_h
00197