NetCDF-C++
4.3.2
Toggle main menu visibility
Loading...
Searching...
No Matches
ncType.cpp
1
#include <string>
2
#include "ncType.h"
3
#include "ncGroup.h"
4
#include "ncCheck.h"
5
using namespace
std;
6
7
8
namespace
netCDF
{
9
// Global comparator operator ==============
10
// comparator operator
11
bool
operator<
(
const
NcType
& lhs,
const
NcType
& rhs)
12
{
13
return
false
;
14
}
15
16
// comparator operator
17
bool
operator>
(
const
NcType
& lhs,
const
NcType
& rhs)
18
{
19
return
true
;
20
}
21
}
22
23
using namespace
netCDF
;
24
25
// assignment operator
26
NcType
&
NcType::operator=
(
const
NcType
& rhs)
27
{
28
nullObject = rhs.nullObject;
29
myId
= rhs.
myId
;
30
groupId
= rhs.
groupId
;
31
return
*
this
;
32
}
33
34
// The copy constructor.
35
NcType::NcType
(
const
NcType
& rhs):
36
nullObject(rhs.nullObject),
37
myId
(rhs.
myId
),
38
groupId
(rhs.
groupId
)
39
{}
40
41
42
// Constructor generates a null object.
43
NcType::NcType
() :
44
nullObject(true)
45
{}
46
47
// constructor
48
NcType::NcType
(
const
NcGroup
& grp,
const
string
& name) :
49
nullObject (false)
50
{
51
groupId
= grp.
getId
();
52
NcType
typTmp(grp.
getType
(name,
NcGroup::ParentsAndCurrent
));
53
myId
= typTmp.getId();
54
}
55
56
// constructor for a global type
57
NcType::NcType
(nc_type
id
) :
58
nullObject(false),
59
myId
(id),
60
groupId
(0)
61
{
62
}
63
64
// Constructor for a non-global type
65
NcType::NcType
(
const
netCDF::NcGroup
& grp, nc_type
id
):
66
nullObject(false),
67
myId
(id),
68
groupId
(grp.
getId
())
69
{}
70
71
72
// equivalence operator
73
bool
NcType::operator==
(
const
NcType
& rhs)
const
74
{
75
if
(nullObject)
76
return
nullObject == rhs.nullObject;
77
else
78
return
groupId
== rhs.
groupId
&&
myId
== rhs.
myId
;
79
}
80
81
// != operator
82
bool
NcType::operator!=
(
const
NcType
& rhs)
const
83
{
84
return
!(*
this
== rhs);
85
}
86
87
// Gets parent group.
88
NcGroup
NcType::getParentGroup
()
const
{
89
if
(
groupId
== 0)
return
NcGroup
();
else
return
NcGroup
(
groupId
);
90
}
91
92
// Returns the type name.
93
string
NcType::getName
()
const
{
94
char
charName[NC_MAX_NAME+1];
95
size_t
*sizep=NULL;
96
97
ncCheck
(nc_inq_type(
groupId
,
myId
,charName,sizep),__FILE__,__LINE__);
98
return
string(charName);
99
100
};
101
102
// Returns the size in bytes
103
size_t
NcType::getSize
()
const
{
104
char
* charName=NULL;
105
size_t
sizep;
106
ncCheck
(nc_inq_type(
groupId
,
myId
,charName,&sizep),__FILE__,__LINE__);
107
return
sizep;
108
};
109
110
// The type class returned as an enumeration type.
111
NcType::ncType
NcType::getTypeClass
()
const
{
112
switch
(
myId
) {
113
case
NC_BYTE :
return
nc_BYTE
;
114
case
NC_UBYTE :
return
nc_UBYTE
;
115
case
NC_CHAR :
return
nc_CHAR
;
116
case
NC_SHORT :
return
nc_SHORT
;
117
case
NC_USHORT :
return
nc_USHORT
;
118
case
NC_INT :
return
nc_INT
;
119
case
NC_UINT :
return
nc_UINT
;
120
case
NC_INT64 :
return
nc_INT64
;
121
case
NC_UINT64 :
return
nc_UINT64
;
122
case
NC_FLOAT :
return
nc_FLOAT
;
123
case
NC_DOUBLE :
return
nc_DOUBLE
;
124
case
NC_STRING :
return
nc_STRING
;
125
default
:
126
// this is a user defined type
127
// establish its type class, ie whether it is: NC_VLEN, NC_OPAQUE, NC_ENUM, or NC_COMPOUND.
128
char
* name=NULL;
129
size_t
* sizep=NULL;
130
nc_type* base_nc_typep=NULL;
131
size_t
* nfieldsp=NULL;
132
int
classp;
133
ncCheck
(nc_inq_user_type(
groupId
,
myId
,name,sizep,base_nc_typep,nfieldsp,&classp),__FILE__,__LINE__);
134
return
static_cast<
ncType
>
(classp);
135
}
136
}
137
138
// The type class returned as a string.
139
string
NcType::getTypeClassName
()
const
{
140
ncType
typeClass=
getTypeClass
();
141
switch
(typeClass) {
142
case
nc_BYTE
:
return
string(
"nc_BYTE"
);
143
case
nc_UBYTE
:
return
string(
"nc_UBYTE"
);
144
case
nc_CHAR
:
return
string(
"nc_CHAR"
);
145
case
nc_SHORT
:
return
string(
"nc_SHORT"
);
146
case
nc_USHORT
:
return
string(
"nc_USHORT"
);
147
case
nc_INT
:
return
string(
"nc_INT"
);
148
case
nc_UINT
:
return
string(
"nc_UINT"
);
149
case
nc_INT64
:
return
string(
"nc_INT64"
);
150
case
nc_UINT64
:
return
string(
"nc_UINT64"
);
151
case
nc_FLOAT
:
return
string(
"nc_FLOAT"
);
152
case
nc_DOUBLE
:
return
string(
"nc_DOUBLE"
);
153
case
nc_STRING
:
return
string(
"nc_STRING"
);
154
case
nc_VLEN
:
return
string(
"nc_VLEN"
);
155
case
nc_OPAQUE
:
return
string(
"nc_OPAQUE"
);
156
case
nc_ENUM
:
return
string(
"nc_ENUM"
);
157
case
nc_COMPOUND
:
return
string(
"nc_COMPOUND"
);
158
}
159
// we never get here!
160
return
"Dummy"
;
161
}
162
163
bool
NcType::isNonPrimitive
()
const
{
164
NcType::ncType
typeClass {
getTypeClass
()};
165
return
(
166
typeClass ==
NcType::nc_VLEN
167
|| typeClass ==
NcType::nc_OPAQUE
168
|| typeClass ==
NcType::nc_ENUM
169
|| typeClass ==
NcType::nc_COMPOUND
170
);
171
}
netCDF::NcGroup
Class represents a netCDF group.
Definition
ncGroup.h:26
netCDF::NcGroup::getId
int getId() const
Gets the group id.
Definition
ncGroup.cpp:144
netCDF::NcGroup::getType
NcType getType(const std::string &name, NcGroup::Location location=Current) const
Gets the NcType object with a given name.
Definition
ncGroup.cpp:1212
netCDF::NcGroup::NcGroup
NcGroup()
Constructor generates a null object.
Definition
ncGroup.cpp:52
netCDF::NcGroup::ParentsAndCurrent
@ ParentsAndCurrent
Select from contents of current and parents groups.
Definition
ncGroup.h:51
netCDF::NcType
Base class inherited by NcOpaque, NcVlen, NcCompound and NcEnum classes.
Definition
ncType.h:15
netCDF::NcType::groupId
int groupId
the group Id
Definition
ncType.h:155
netCDF::NcType::getTypeClass
ncType getTypeClass() const
The type class returned as enumeration type.
Definition
ncType.cpp:111
netCDF::NcType::getParentGroup
netCDF::NcGroup getParentGroup() const
Gets parent group.
Definition
ncType.cpp:88
netCDF::NcType::operator>
friend bool operator>(const NcType &lhs, const NcType &rhs)
comparator operator
Definition
ncType.cpp:17
netCDF::NcType::NcType
NcType()
Constructor generates a null object.
Definition
ncType.cpp:43
netCDF::NcType::getSize
size_t getSize() const
The size in bytes.
Definition
ncType.cpp:103
netCDF::NcType::operator<
friend bool operator<(const NcType &lhs, const NcType &rhs)
comparator operator
Definition
ncType.cpp:11
netCDF::NcType::operator==
bool operator==(const NcType &) const
equivalence operator
Definition
ncType.cpp:73
netCDF::NcType::myId
nc_type myId
the type Id
Definition
ncType.h:152
netCDF::NcType::getTypeClassName
std::string getTypeClassName() const
Return a string containing the name of the enumerated type.
Definition
ncType.cpp:139
netCDF::NcType::isNonPrimitive
bool isNonPrimitive() const
Return true if this is not a primitive type.
Definition
ncType.cpp:163
netCDF::NcType::operator=
NcType & operator=(const NcType &rhs)
assignment operator
Definition
ncType.cpp:26
netCDF::NcType::ncType
ncType
List of netCDF types that can be represented.
Definition
ncType.h:26
netCDF::NcType::nc_DOUBLE
@ nc_DOUBLE
double precision floating point number
Definition
ncType.h:32
netCDF::NcType::nc_UBYTE
@ nc_UBYTE
unsigned 1 byte int
Definition
ncType.h:33
netCDF::NcType::nc_OPAQUE
@ nc_OPAQUE
"NcOpaque type"
Definition
ncType.h:40
netCDF::NcType::nc_CHAR
@ nc_CHAR
ISO/ASCII character.
Definition
ncType.h:28
netCDF::NcType::nc_SHORT
@ nc_SHORT
signed 2 byte integer
Definition
ncType.h:29
netCDF::NcType::nc_BYTE
@ nc_BYTE
signed 1 byte integer
Definition
ncType.h:27
netCDF::NcType::nc_UINT64
@ nc_UINT64
unsigned 8-byte int
Definition
ncType.h:37
netCDF::NcType::nc_VLEN
@ nc_VLEN
"NcVlen type"
Definition
ncType.h:39
netCDF::NcType::nc_UINT
@ nc_UINT
unsigned 4-byte int
Definition
ncType.h:35
netCDF::NcType::nc_INT
@ nc_INT
signed 4 byte integer
Definition
ncType.h:30
netCDF::NcType::nc_ENUM
@ nc_ENUM
"NcEnum type"
Definition
ncType.h:41
netCDF::NcType::nc_STRING
@ nc_STRING
string
Definition
ncType.h:38
netCDF::NcType::nc_INT64
@ nc_INT64
signed 8-byte int
Definition
ncType.h:36
netCDF::NcType::nc_USHORT
@ nc_USHORT
unsigned 2-byte int
Definition
ncType.h:34
netCDF::NcType::nc_FLOAT
@ nc_FLOAT
single precision floating point number
Definition
ncType.h:31
netCDF::NcType::nc_COMPOUND
@ nc_COMPOUND
"NcCompound type"
Definition
ncType.h:42
netCDF::NcType::getName
std::string getName() const
The name of this type.
Definition
ncType.cpp:93
netCDF::NcType::operator!=
bool operator!=(const NcType &) const
!= operator
Definition
ncType.cpp:82
netCDF::NcType::getId
nc_type getId() const
The netCDF Id of this type.
Definition
ncType.h:88
netCDF
C++ API for netCDF4.
Definition
ncAtt.h:10
netCDF::ncCheck
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition
ncCheck.cpp:11
Return to the Main Unidata NetCDF page.
Generated on
for NetCDF-C++. NetCDF is a
Unidata
library.