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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*********************************************************************
* Copyright 1992, University Corporation for Atmospheric Research
* See netcdf/README file for copying and redistribution conditions.
*
* Purpose: interface for classes of typed arrays for netCDF
*
* $Header: /hdf/src/master/dist/mfhdf/c++/ncvalues.hh,v 1.2 1993/04/30 20:29:42 koziol Exp $
*********************************************************************/
#ifndef Ncvalues_def
#define Ncvalues_def
#include <generic.h>
#include <iostream.h>
#include <strstream.h>
#include <limits.h>
#include <string.h>
#include "netcdf.h"
typedef unsigned char ncbyte;
enum NcType
{
ncNoType = NC_UNSPECIFIED,
ncByte = NC_BYTE,
ncChar = NC_CHAR,
ncShort = NC_SHORT,
ncLong = NC_LONG,
ncFloat = NC_FLOAT,
ncDouble = NC_DOUBLE,
ncInt // Note: needed for int* overloading
};
static const ncbyte ncBad_byte = FILL_BYTE;
static const char ncBad_char = FILL_CHAR;
static const short ncBad_short = FILL_SHORT;
static const long ncBad_long = FILL_LONG;
static const float ncBad_float = FILL_FLOAT;
static const double ncBad_double = FILL_DOUBLE;
#define NcVal(TYPE) name2(NcValues_,TYPE)
#define NcValuesdeclare(TYPE) \
class NcVal(TYPE) : public NcValues \
{ \
public: \
NcVal(TYPE)( void ); \
NcVal(TYPE)(long num); \
NcVal(TYPE)(long num, const TYPE* vals); \
NcVal(TYPE)(const NcVal(TYPE)&); \
virtual NcVal(TYPE)& operator=(const NcVal(TYPE)&); \
virtual ~NcVal(TYPE)( void ); \
virtual void* base( void ) const; \
virtual int bytes_for_one( void ) const; \
virtual ncbyte as_ncbyte( int n ) const; \
virtual char as_char( int n ) const; \
virtual short as_short( int n ) const; \
virtual long as_long( int n ) const; \
virtual float as_float( int n ) const; \
virtual double as_double( int n ) const; \
virtual char* as_string( int n ) const; \
private: \
TYPE* the_values; \
ostream& print(ostream&) const; \
};
#define NcTypeEnum(TYPE) name2(_nc__,TYPE)
#define _nc__ncbyte ncByte
#define _nc__char ncChar
#define _nc__short ncShort
#define _nc__long ncLong
#define _nc__int ncInt
#define _nc__float ncFloat
#define _nc__double ncDouble
#define NcValuesimplement(TYPE) \
NcVal(TYPE)::NcVal(TYPE)( void ) \
: NcValues(NcTypeEnum(TYPE), 0), the_values(0) \
{} \
\
NcVal(TYPE)::NcVal(TYPE)(long num, const TYPE* vals) \
: NcValues(NcTypeEnum(TYPE), num) \
{ \
the_values = new TYPE[num]; \
for(int i = 0; i < num; i++) \
the_values[i] = vals[i]; \
} \
\
NcVal(TYPE)::NcVal(TYPE)(long num) \
: NcValues(NcTypeEnum(TYPE), num), the_values(new TYPE[num]) \
{} \
\
NcVal(TYPE)::NcVal(TYPE)(const NcVal(TYPE)& v) \
{ \
delete[] the_values; \
the_values = new TYPE[v.the_number]; \
for(int i = 0; i < v.the_number; i++) \
the_values[i] = v.the_values[i]; \
} \
\
NcVal(TYPE)& NcVal(TYPE)::operator=(const NcVal(TYPE)& v) \
{ \
delete[] the_values; \
the_values = new TYPE[v.the_number]; \
for(int i = 0; i < v.the_number; i++) \
the_values[i] = v.the_values[i]; \
return *this; \
} \
\
void* NcVal(TYPE)::base( void ) const \
{ \
return the_values; \
} \
\
NcVal(TYPE)::~NcVal(TYPE)( void ) \
{ \
delete[] the_values; \
}
#define Ncbytes_for_one_implement(TYPE) \
int NcVal(TYPE)::bytes_for_one( void ) const \
{ \
return nctypelen((nc_type) NcTypeEnum(TYPE)); \
}
#define as_ncbyte_implement(TYPE) \
ncbyte NcVal(TYPE)::as_ncbyte( int n ) const \
{ \
if (the_values[n] < 0 || the_values[n] > UCHAR_MAX) \
return ncBad_byte; \
return (ncbyte) the_values[n]; \
}
#define as_char_implement(TYPE) \
char NcVal(TYPE)::as_char( int n ) const \
{ \
if (the_values[n] < CHAR_MIN || the_values[n] > CHAR_MAX) \
return ncBad_char; \
return (char) the_values[n]; \
}
#define as_short_implement(TYPE) \
short NcVal(TYPE)::as_short( int n ) const \
{ \
if (the_values[n] < SHRT_MIN || the_values[n] > SHRT_MAX) \
return ncBad_short; \
return (short) the_values[n]; \
}
#define as_long_implement(TYPE) \
long NcVal(TYPE)::as_long( int n ) const \
{ \
if (the_values[n] < LONG_MIN || the_values[n] > LONG_MAX) \
return ncBad_long; \
return (long) the_values[n]; \
}
#define as_float_implement(TYPE) \
inline float NcVal(TYPE)::as_float( int n ) const \
{ \
return (float) the_values[n]; \
}
#define as_double_implement(TYPE) \
inline double NcVal(TYPE)::as_double( int n ) const \
{ \
return (double) the_values[n]; \
}
#define as_string_implement(TYPE) \
char* NcVal(TYPE)::as_string( int n ) const \
{ \
char* s = new char[32]; \
ostrstream(s, sizeof(s)) << the_values[n] << ends ; \
return s; \
}
class NcValues // ABC for value blocks
{
public:
NcValues( void );
NcValues(NcType, long);
virtual ~NcValues( void );
virtual long num( void );
virtual ostream& print(ostream&) const = 0;
virtual void* base( void ) const = 0;
virtual int bytes_for_one( void ) const = 0;
// The following member functions provide conversions from the value
// type to a desired basic type. If the value is out of range, the
// default "fill-value" for the appropriate type is returned.
virtual ncbyte as_ncbyte( int n ) const = 0; // nth value as a byte
virtual char as_char( int n ) const = 0; // nth value as char
virtual short as_short( int n ) const = 0; // nth value as short
virtual long as_long( int n ) const = 0; // nth value as long
virtual float as_float( int n ) const = 0; // nth value as floating-point
virtual double as_double( int n ) const = 0; // nth value as double
virtual char* as_string( int n ) const = 0; // value as string
protected:
NcType the_type;
long the_number;
friend ostream& operator<< (ostream&, const NcValues&);
};
declare(NcValues,ncbyte)
declare(NcValues,char)
declare(NcValues,short)
declare(NcValues,long)
declare(NcValues,int)
declare(NcValues,float)
declare(NcValues,double)
#endif
|