h5cpp icon indicating copy to clipboard operation
h5cpp copied to clipboard

Template return-type functions for reading data?

Open martukas opened this issue 7 years ago • 8 comments

Could we have instead of:

uint32_t v = 0;
attr.read(v);

this:

auto v = attr.read<uint32_t>();

Just an idea

martukas avatar Jan 27 '18 23:01 martukas

Here is another example of boilerplate that could potentially be a one-liner

      auto ds_event_time_zero
          = hdf5::node::get_dataset(root_group, group_path + "/event_time_zero");
      vector<uint64_t> event_time_zero(ds_event_time_zero.dataspace().size());
      ds_event_time_zero.read(event_time_zero);

Yes, it's not as efficient as allocating everything at the beginning of the universe, but reality dictates many such situations as, for example, the above being from some unit tests that validate small datasets.

martukas avatar Jan 28 '18 01:01 martukas

We definitely should have something like this and it is already planned. What we have to do for that purpose is to refacture the type trait framework we currently use for IO. What we would need in particular is a type trait that tells the library how to create an instance of a particular type. Something like this

template<typename T> class FactoryTrait
{
  public:
    static T create(const hdf5::dataspace::Dataspace &space,
                            const hdf5::datatype::Datatype &type); 
}

With this in our arsenal it would be rather straight forward to do things like the above.

I have a very rough design in mind which would do the job. But it still requires some rethinking. I hope I can put a draft online for discussion this week.

eugenwintersberger avatar Jan 29 '18 07:01 eugenwintersberger

I have attempted to do something like

    template<typename T>
    T read() const
    {
      T ret;
      read(ret);
      return ret;
    }

but this only works for scalar values, not vectors, as it needs to resize them before writing. Any idea how to do this so it would work for both? Would this FactoryTrait really help?

martukas avatar May 07 '18 21:05 martukas

As for the discusion with Jan on #379 the first step to make this happen is to add the following static member to the dataspace trait

namespace hdf5 {
namespace dataspace {
template<typename T> class TypeTrait<T>

  public:
    // create a new instance of T according to dataspace
    static T allocate(const Dataspace &dataspace) 
    {

    }
};
}
}

eugenwintersberger avatar Nov 09 '18 08:11 eugenwintersberger

There is a fairly stale branch for this issue: https://github.com/ess-dmsc/h5cpp/tree/issue_280 Is this worth preserving?

zjttoefs avatar Jun 02 '21 09:06 zjttoefs

I do not see this branch and the link is dead. Is this something you still have around on your machine?

eugenwintersberger avatar Jun 03 '21 08:06 eugenwintersberger

Sorry, I must have pasted the wrong link. I deleted the branch for the closed ticket #280 that had nothing that isn't in master. Nothing to do with this one.

The branch for this ticket is https://github.com/ess-dmsc/h5cpp/tree/issue_216 Just adds this: https://github.com/ess-dmsc/h5cpp/compare/issue_216

zjttoefs avatar Jun 03 '21 08:06 zjttoefs

This branch can be definitely removed. No objections on that. But we should leave this ticket open.

eugenwintersberger avatar Jun 04 '21 06:06 eugenwintersberger