File: HDFAtom.cpp

package info (click to toggle)
pbseqlib 5.3.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,020 kB
  • sloc: cpp: 77,246; python: 331; sh: 103; makefile: 42
file content (183 lines) | stat: -rw-r--r-- 5,781 bytes parent folder | download | duplicates (4)
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
#include <hdf/HDFAtom.hpp>

template <>
void HDFAtom<std::string>::Create(H5::H5Location &object, const std::string &atomName)
{
    H5::StrType strType(0, H5T_VARIABLE);
    hsize_t defaultDims[] = {1};
    H5::DataSpace defaultDataSpace(1, defaultDims);
    attribute = object.createAttribute(atomName.c_str(), strType, H5::DataSpace(H5S_SCALAR));
}

#define DEFINE_TYPED_CREATE_ATOM(T, predtype)                                               \
    template <>                                                                             \
    void HDFAtom<T>::TypedCreate(H5::H5Location &object, const std::string &atomName,         \
                                 H5::DataSpace &defaultDataSpace)                           \
    {                                                                                       \
        attribute = object.createAttribute(atomName.c_str(), (predtype), defaultDataSpace); \
    }

DEFINE_TYPED_CREATE_ATOM(int, H5::PredType::NATIVE_INT)
DEFINE_TYPED_CREATE_ATOM(unsigned int, H5::PredType::NATIVE_UINT)
DEFINE_TYPED_CREATE_ATOM(unsigned char, H5::PredType::NATIVE_UINT8)
DEFINE_TYPED_CREATE_ATOM(char, H5::PredType::NATIVE_INT8)
DEFINE_TYPED_CREATE_ATOM(float, H5::PredType::NATIVE_FLOAT)
DEFINE_TYPED_CREATE_ATOM(uint16_t, H5::PredType::NATIVE_UINT16)
DEFINE_TYPED_CREATE_ATOM(uint64_t, H5::PredType::STD_I64LE)

template <>
void HDFAtom<std::vector<int> >::Write(const std::vector<int> vect)
{
    hsize_t length = vect.size();
    H5::DataType baseType = H5::PredType::NATIVE_INT;
    H5::ArrayType arrayDataType(baseType, 1, &length);
    attribute.write(arrayDataType, &((vect)[0]));
}

template <>
void HDFAtom<std::string>::Write(std::string value)
{
    //	H5::StrType strType(0, value.size());
    H5::StrType strType(0, H5T_VARIABLE);
    attribute.write(strType, std::string(value.c_str()));
}

template <>
void HDFAtom<uint64_t>::Write(uint64_t value)
{
    attribute.write(H5::PredType::STD_I64LE, &value);
}

template <>
void HDFAtom<int>::Write(int value)
{
    attribute.write(H5::PredType::NATIVE_INT, &value);
}

template <>
void HDFAtom<unsigned int>::Write(unsigned int value)
{
    attribute.write(H5::PredType::NATIVE_INT, &value);
}

template <>
void HDFAtom<unsigned char>::Write(unsigned char value)
{
    attribute.write(H5::PredType::NATIVE_UINT8, &value);
}

template <>
void HDFAtom<uint16_t>::Write(uint16_t value)
{
    attribute.write(H5::PredType::NATIVE_UINT16, &value);
}

template <>
void HDFAtom<char>::Write(char value)
{
    attribute.write(H5::PredType::NATIVE_INT8, &value);
}

template <>
void HDFAtom<float>::Write(float value)
{
    attribute.write(H5::PredType::NATIVE_FLOAT, &value);
}

template <>
void HDFAtom<std::string>::Read(std::string &value)
{
    /*
	 * Read in a std::string that has been stored either as an array or a
	 * variable length std::string.  To decide which, query the
	 * isVariableStr() option.
	 */
    H5::StrType stringType = attribute.getStrType();
    bool stringIsVariableLength = stringType.isVariableStr();
    if (stringIsVariableLength)
        attribute.read(stringType, value);
    else {
        hsize_t stsize = attribute.getStorageSize();
        value.resize(stsize);
        attribute.read(stringType, &value[0]);
        if (stsize > 0 and value[stsize - 1] == '\0') {
            value.resize(stsize - 1);
        }
    }
}

template <>
void HDFAtom<int>::Read(int &value)
{
    H5::DataType intType(H5::PredType::NATIVE_INT);
    attribute.read(intType, &value);
}

template <>
void HDFAtom<uint16_t>::Read(uint16_t &value)
{
    H5::DataType intType(H5::PredType::NATIVE_UINT16);
    attribute.read(intType, &value);
}

template <>
void HDFAtom<uint64_t>::Read(uint64_t &value)
{
    H5::DataType intType(H5::PredType::STD_I64LE);
    attribute.read(intType, &value);
}

template <>
void HDFAtom<unsigned int>::Read(unsigned int &value)
{
    H5::DataType uintType(H5::PredType::NATIVE_UINT);
    attribute.read(uintType, &value);
}

template <>
void HDFAtom<float>::Read(float &value)
{
    H5::DataType type(H5::PredType::NATIVE_FLOAT);
    attribute.read(type, &value);
}

template <>
void HDFAtom<std::vector<std::string> >::Read(std::vector<std::string> &values)
{
    std::string value;
    /*
	 * This attribute is an array of std::strings. They are read in by
	 * storing pointers to std::strings in memory controlled by HDF.  To read
	 * the std::strings, read the pointers into a temporary array, then copy
	 * those std::strings to the values array. This way when the values array
	 * is destroyed, it will not try and get rid of space that is under
	 * HDF control.
	 */
    H5::DataSpace attributeSpace = attribute.getSpace();
    hsize_t nPoints;
    nPoints = attributeSpace.getSelectNpoints();
    H5::DataType attrType = attribute.getDataType();  // necessary for attr.read()
    // Declare and initialize std::vector of pointers to std::string attribute list.
    std::vector<char *> ptrsToHDFControlledMemory;
    ptrsToHDFControlledMemory.resize(nPoints);
    // Copy the pointers.
    attribute.read(attrType, &ptrsToHDFControlledMemory[0]);
    // Copy the std::strings into memory the main program has control over.
    unsigned int i;
    for (i = 0; i < ptrsToHDFControlledMemory.size(); i++) {
        values.push_back(ptrsToHDFControlledMemory[i]);
        free(ptrsToHDFControlledMemory[i]);
    }
}

// Explict instantiate classes
template class HDFAtom<int>;
template class HDFAtom<unsigned int>;
template class HDFAtom<char>;
template class HDFAtom<unsigned char>;
template class HDFAtom<uint64_t>;
template class HDFAtom<float>;
template class HDFAtom<std::string>;
template class HDFAtom<std::vector<int> >;
template class HDFAtom<std::vector<unsigned int> >;
template class HDFAtom<std::vector<std::string> >;