Skip to content

Commit 1444cf8

Browse files
committed
improved reference api call
1 parent fa1eeca commit 1444cf8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

h5cpp/H5Rall.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ namespace h5::exp {
9999
template <class T, class... args_t>
100100
inline h5::ds_t write( const h5::ds_t& ds, h5::reference_t& reference,
101101
const T* ptr, args_t&&... args ) try {
102+
102103
h5::dt_t<T> mem_type;
103104

104105
const h5::dxpl_t& dxpl = arg::get(h5::default_dxpl, args...);
@@ -124,6 +125,38 @@ namespace h5::exp {
124125
throw h5::error::io::dataset::write( err.what() );
125126
}
126127

128+
template <class T, class... args_t>
129+
inline h5::ds_t write( const h5::fd_t& fd, const std::string& dataset_path, h5::reference_t& reference,
130+
const T* ptr, args_t&&... args ) try {
131+
132+
using tcount = typename arg::tpos<const h5::count_t&, const args_t&...>;
133+
//static_assert( tcount::present, "h5::count_t{ ... } must be provided to describe T* memory region" );
134+
h5::ds_t ds; // initialized to H5I_UNINIT
135+
136+
h5::mute(); // find out if we have to create the dataset
137+
bool is_dataset_present = H5Lexists(fd, dataset_path.c_str(), H5P_DEFAULT) > 0;
138+
h5::unmute(); // <- make sure not to mute error handling longer than needed
139+
if (is_dataset_present) {
140+
const h5::dapl_t& dapl = arg::get(h5::default_dapl, args...);
141+
ds = h5::open(fd, dataset_path, dapl);
142+
} else {
143+
// dataset doesn't exist, or some error happened, since h5::create doesn't know of the
144+
// memory space size as `T& ref` never passed along we have to compute the `h5::current_dims_t{}` upfront
145+
using tcurrent_dims = typename arg::tpos<const h5::current_dims_t&, const args_t&...>;
146+
if constexpr (tcurrent_dims::present) // user knows what he is doing, specified h5::current_dims{} explicitly
147+
ds = h5::create<h5::reference_t>(fd, dataset_path, args...);
148+
else {
149+
h5::current_dims_t current_dims{1}; // count must be present, we checked
150+
ds = h5::create<h5::reference_t>(fd, dataset_path, current_dims, args...); // and use it to create dataset
151+
}
152+
}
153+
h5::exp::write(ds, reference, ptr, args...);
154+
155+
return ds;
156+
} catch ( const std::exception& err ){
157+
throw h5::error::io::dataset::write( err.what() );
158+
}
159+
127160

128161
}
129162
#endif

0 commit comments

Comments
 (0)