-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathHDF5IterateGroups.h
199 lines (176 loc) · 7.03 KB
/
HDF5IterateGroups.h
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
/************************************************************************
* Copyright(c) 2009, One Unified. All rights reserved. *
* email: [email protected] *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
#pragma once
#include <string>
#include <iostream>
#include <boost/function.hpp>
#include <OUCommon/FastDelegate.h>
using namespace fastdelegate;
#include "HDF5DataManager.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
// called from IterateCallback (which is called as HDF5 iterates the directory
// this class is called recursively as the group hierarchy is traversed
// class T needs to supply method: Process( sObjectName, sObjectPath );
// example usage in SymbolSelectionFilter
/*
string sBaseGroup = "/bar/86400/";
HDF5IterateGroups<CSymbolSelectionFilter> control;
int result = control.Start( sBaseGroup, this );
*/
// FastDelegate flavoured version
class HDF5IterateGroups {
public:
typedef FastDelegate2<const std::string&,const std::string&> OnObjectHandler_t; // objectpath, objectname
HDF5IterateGroups() {};
void SetOnHandleObject( OnObjectHandler_t handler ) {
HandleObject = handler;;
}
void SetOnHandleGroup( OnObjectHandler_t handler ) {
HandleGroup = handler;
}
int Start( const std::string& sBaseGroup ) {
HDF5DataManager dm( HDF5DataManager::RO );
m_sBaseGroup = sBaseGroup;
int idx = 0; // starting location for interrupted queries
int result = dm.GetH5File()->iterateElems( sBaseGroup, &idx, &HDF5IterateCallback, this );
return result;
}
protected:
std::string m_sBaseGroup;
private:
OnObjectHandler_t HandleObject;
OnObjectHandler_t HandleGroup;
void Process( const std::string &sObjectName ) {
HDF5DataManager dm( HDF5DataManager::RO );
std::string sObjectPath;
if ( '/' == m_sBaseGroup[ m_sBaseGroup.size() - 1 ] ) {
sObjectPath = m_sBaseGroup + sObjectName;
}
else {
sObjectPath = m_sBaseGroup + '/' + sObjectName;
}
H5G_stat_t stats;
try {
dm.GetH5File()->getObjinfo( sObjectPath, stats );
switch ( stats.type ) {
case H5G_DATASET:
try {
if ( NULL != HandleObject ) HandleObject( sObjectPath, sObjectName );
}
catch ( std::exception e ) {
std::cout << "CFilterSelectionIteratorControl::Process Object " << sObjectName << " problem: " << e.what() << std::endl;
}
catch (...) {
std::cout << "CFilterSelectionIteratorControl::Process Object " << sObjectName << " unknown problems" << std::endl;
}
break;
case H5G_GROUP: {
int idx = 0; // starting location for interrupted queries
sObjectPath.append( "/" );
if ( NULL != HandleGroup ) HandleGroup( sObjectPath, sObjectName );
HDF5IterateGroups control; // recursive call
control.SetOnHandleObject( HandleObject );
control.SetOnHandleGroup( HandleGroup );
int result = control.Start( sObjectPath );
}
break;
default:
break;
}
}
catch ( H5::Exception e ) {
std::cout << "CFilterSelectionIteratorControl::Process H5::Exception " << e.getDetailMsg() << std::endl;
e.walkErrorStack( H5E_WALK_DOWNWARD, (H5E_walk2_t) &HDF5DataManager::PrintH5ErrorStackItem, 0 );
}
}
static herr_t HDF5IterateCallback( hid_t group, const char *name, void *op_data ) {
HDF5IterateGroups* pControl = ( HDF5IterateGroups *) op_data;
pControl->Process( name );
return 0;
}
};
// boost lambda flavoured version
namespace hdf5 {
/*
IterateGroups Call example:
#include <boost/phoenix/bind/bind_member_function.hpp>
namespace args = boost::phoenix::placeholders;
ou::tf::hdf5::IterateGroups ig(
"/bar/86400",
boost::phoenix::bind( &AppScanner::HandleHdf5Group, this, args::arg1, args::arg2 ),
boost::phoenix::bind( &AppScanner::HandleHdf5Object, this, args::arg1, args::arg2 )
);
*/
class IterateGroups {
public:
typedef boost::function<void (const std::string&, const std::string& )> callback_t;
IterateGroups( HDF5DataManager& dm, const std::string& sBaseGroup, callback_t group, callback_t object )
: m_dm( dm ), m_sBaseGroup( sBaseGroup ), m_g( group ), m_o( object )
{
int idx {}; // starting location for interrupted queries
int result = m_dm.GetH5File()->iterateElems( m_sBaseGroup, &idx, &IterateGroups::IterateCallback, this );
}
~IterateGroups() {};
protected:
private:
const std::string m_sBaseGroup;
HDF5DataManager& m_dm;
callback_t m_g;
callback_t m_o;
static herr_t IterateCallback( hid_t group, const char *name, void *op_data ) {
IterateGroups& ig( *reinterpret_cast<IterateGroups*>( op_data ) );
std::string sObjectPath;
const std::string sObjectName( name );
if ( '/' == ig.m_sBaseGroup[ ig.m_sBaseGroup.size() - 1 ] ) {
sObjectPath = ig.m_sBaseGroup + sObjectName;
}
else {
sObjectPath = ig.m_sBaseGroup + '/' + sObjectName;
}
H5G_stat_t stats;
try {
ig.m_dm.GetH5File()->getObjinfo( sObjectPath, stats );
switch ( stats.type ) {
case H5G_DATASET:
try {
ig.m_o( sObjectPath, name );
}
catch ( std::exception e ) {
std::cout << "hdf5::IterateGroups::IterateCallback Object " << sObjectPath << "," << sObjectName << " problem: " << e.what() << std::endl;
}
catch (...) {
std::cout << "hdf5::IterateGroups::IterateCallback Object " << sObjectName << " unknown problems" << std::endl;
}
break;
case H5G_GROUP: {
sObjectPath.append( "/" );
ig.m_g( sObjectPath, sObjectName );
IterateGroups igRecursive( ig.m_dm, sObjectPath, ig.m_g, ig.m_o ); // recursive call
}
break;
default:
break;
}
}
catch ( H5::Exception& e ) {
std::cerr << "hdf5::IterateGroups::IterateCallback H5::Exception " << e.getDetailMsg() << std::endl;
e.walkErrorStack( H5E_WALK_DOWNWARD, (H5E_walk2_t) &HDF5DataManager::PrintH5ErrorStackItem, 0 );
}
return 0;
}
};
}
} // namespace tf
} // namespace ou