NetCDF-C++  4.3.1-developer
ncEnumType.cpp
1 #include "ncEnumType.h"
2 #include "ncGroup.h"
3 #include "ncCheck.h"
4 #include "ncByte.h"
5 #include "ncUbyte.h"
6 #include "ncChar.h"
7 #include "ncShort.h"
8 #include "ncUshort.h"
9 #include "ncInt.h"
10 #include "ncUint.h"
11 #include "ncInt64.h"
12 #include "ncUint64.h"
13 #include "ncFloat.h"
14 #include "ncDouble.h"
15 #include "ncString.h"
16 #include "ncException.h"
17 using namespace std;
18 using namespace netCDF;
19 using namespace netCDF::exceptions;
20 
21 // Class represents a netCDF variable.
22 
23 // assignment operator
24 NcEnumType& NcEnumType::operator=(const NcEnumType& rhs)
25 {
26  NcType::operator=(rhs); // assign base class parts
27  return *this;
28 }
29 
30 // assignment operator
31 NcEnumType& NcEnumType::operator=(const NcType& rhs)
32 {
33  if (&rhs != this) {
34  // check the rhs is the base of an Enum type
35  if(getTypeClass() != NC_ENUM) throw NcException("The NcType object must be the base of an Enum type.",__FILE__,__LINE__);
36  // assign base class parts
37  NcType::operator=(rhs);
38  }
39  return *this;
40 }
41 
42 // The copy constructor.
43 NcEnumType::NcEnumType(const NcEnumType& rhs):
44  NcType(rhs)
45 {
46 }
47 
48 
49 // Constructor generates a null object.
51  NcType() // invoke base class constructor
52 {}
53 
54 // constructor
55 NcEnumType::NcEnumType(const NcGroup& grp, const string& name):
56  NcType(grp,name)
57 {}
58 
59 
60 // constructor
62  NcType(ncType)
63 {
64  // check the nctype object is the base of an Enum type
65  if(getTypeClass() != NC_ENUM) throw NcException("The NcType object must be the base of an Enum type.",__FILE__,__LINE__);
66 }
67 
68 // Returns the base type.
70 {
71  char charName[NC_MAX_NAME+1];
72  nc_type base_nc_typep;
73  size_t *base_sizep=NULL;
74  size_t *num_membersp=NULL;
75  ncCheck(nc_inq_enum(groupId,myId,charName,&base_nc_typep,base_sizep,num_membersp),__FILE__,__LINE__);
76  switch (base_nc_typep) {
77  case NC_BYTE : return ncByte;
78  case NC_UBYTE : return ncUbyte;
79  case NC_CHAR : return ncChar;
80  case NC_SHORT : return ncShort;
81  case NC_USHORT : return ncUshort;
82  case NC_INT : return ncInt;
83  case NC_UINT : return ncUint;
84  case NC_INT64 : return ncInt64;
85  case NC_UINT64 : return ncUint64;
86  case NC_FLOAT : return ncFloat;
87  case NC_DOUBLE : return ncDouble;
88  case NC_STRING : return ncString;
89  default:
90  // this is a user defined type
91  return NcType(getParentGroup(),base_nc_typep);
92  }
93 }
94 
95 
96 // Returns number of members in this NcEnumType object.
98  char charName[NC_MAX_NAME+1];
99  nc_type* base_nc_typep=NULL;
100  size_t* base_sizep=NULL;
101  size_t num_membersp;
102  ncCheck(nc_inq_enum(groupId,myId,charName,base_nc_typep,base_sizep,&num_membersp),__FILE__,__LINE__);
103  return num_membersp;
104 };
105 
106 // Returns the member name for the given zero-based index.
107 string NcEnumType::getMemberNameFromIndex(int index) const{
108  void* value=NULL;
109  char charName[NC_MAX_NAME+1];
110  ncCheck(nc_inq_enum_member(groupId,myId,index,charName,value),__FILE__,__LINE__);
111  return static_cast<string> (charName);
112 };
Base class inherited by NcOpaque, NcVlen, NcCompound and NcEnum classes.
Definition: ncType.h:14
NcUint64 ncUint64
A global instance of the NcUint64 class within the netCDF namespace.
Definition: ncUint64.cpp:7
int groupId
the group Id
Definition: ncType.h:152
std::string getMemberNameFromIndex(int index) const
Returns the member name for the given zero-based index.
Definition: ncEnumType.cpp:107
C++ API for netCDF4.
Definition: ncAtt.h:9
NcByte ncByte
A global instance of the NcByte class within the netCDF namespace.
Definition: ncByte.cpp:7
Class represents a netCDF enum type.
Definition: ncEnumType.h:15
NcFloat ncFloat
A global instance of the NcFloat class within the netCDF namespace.
Definition: ncFloat.cpp:7
Class represents a netCDF group.
Definition: ncGroup.h:27
ncType
List of netCDF types that can be represented.
Definition: ncType.h:25
Exception classes.
Definition: ncException.h:15
NcUint ncUint
A global instance of the NcUint class within the netCDF namespace.
Definition: ncUint.cpp:7
NcChar ncChar
A global instance of the NcChar class within the netCDF namespace.
Definition: ncChar.cpp:7
nc_type myId
the type Id
Definition: ncType.h:149
NcEnumType()
Constructor generates a null object.
Definition: ncEnumType.cpp:50
NcDouble ncDouble
A global instance of the NcDouble class within the netCDF namespace.
Definition: ncDouble.cpp:7
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
NcType()
Constructor generates a null object.
Definition: ncType.cpp:44
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition: ncCheck.cpp:11
NcShort ncShort
A global instance of the NcShort class within the netCDF namespace.
Definition: ncShort.cpp:7
size_t getMemberCount() const
Returns number of members in this NcEnumType object.
Definition: ncEnumType.cpp:97
NcType getBaseType() const
Returns the base type.
Definition: ncEnumType.cpp:69
NcString ncString
A global instance of the NcString class within the netCDF namespace.
Definition: ncString.cpp:7
netCDF::NcGroup getParentGroup() const
Gets parent group.
Definition: ncType.cpp:89
NcInt ncInt
A global instance of the NcInt class within the netCDF namespace.
Definition: ncInt.cpp:7
ncType getTypeClass() const
The type class returned as enumeration type.
Definition: ncType.cpp:116
NcUbyte ncUbyte
A global instance of the NcUbyte class within the netCDF namespace.
Definition: ncUbyte.cpp:7
NcInt64 ncInt64
A global instance of the NcInt64 class within the netCDF namespace.
Definition: ncInt64.cpp:7

Return to the Main Unidata NetCDF page.
Generated on Fri Nov 11 2016 15:28:29 for NetCDF-C++. NetCDF is a Unidata library.