ALPSCore reference
archive_read_vector_attribute_helper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #include <iostream>
8 #include <vector>
9 
10 #include <memory>
11 
12 #include <alps/hdf5/archive.hpp>
14 #include <alps/utilities/cast.hpp>
15 
16 #include "common.hpp"
17 
18 namespace alps {
19  namespace hdf5 {
20  namespace detail {
21 
22  template<typename T, typename U, typename... UTail>
23  inline bool hdf5_read_vector_attribute_helper_impl(std::string const &path, T * value, attribute_type const &attribute_id, type_type const &native_id,
24  std::vector<std::size_t> const &chunk,
25  std::vector<std::size_t> const &data_size,
26  std::true_type) {
27  if (check_error(
28  H5Tequal(type_type(H5Tcopy(native_id)), type_type(get_native_type(U())))
29  ) > 0) {
30  std::size_t len = std::accumulate(chunk.begin(), chunk.end(), std::size_t(1), std::multiplies<std::size_t>());
31  std::unique_ptr<U[]> raw(
32  new U[len]
33  );
34  if (std::equal(chunk.begin(), chunk.end(), data_size.begin())) {
35  check_error(H5Aread(attribute_id, native_id, raw.get()));
36  cast(raw.get(), raw.get() + len, value);
37  } else
38  throw std::logic_error("Not Implemented, path: " + path + ALPS_STACKTRACE);
39  return true;
40  } else
41  return hdf5_read_vector_attribute_helper_impl<T, UTail...>(path,
42  value,
43  attribute_id,
44  native_id,
45  chunk,
46  data_size,
47  std::integral_constant<bool, sizeof...(UTail) != 0>());
48  }
49  template<typename T, typename... UTail>
50  inline bool hdf5_read_vector_attribute_helper_impl(std::string const &, T *, attribute_type const &, type_type const &,
51  std::vector<std::size_t> const &,
52  std::vector<std::size_t> const &,
53  std::false_type)
54  { return false; }
55 
56  template<typename T>
57  bool hdf5_read_vector_attribute_helper(std::string const &path, T * value, attribute_type const &attribute_id, type_type const &native_id,
58  std::vector<std::size_t> const &chunk,
59  std::vector<std::size_t> const &data_size) {
60  return hdf5_read_vector_attribute_helper_impl<T, ALPS_HDF5_NATIVE_INTEGRAL_TYPES>(path,
61  value,
62  attribute_id,
63  native_id,
64  chunk,
65  data_size,
66  std::true_type());
67  }
68 
69  #define ALPS_HDF5_READ_VECTOR_ATTRIBUTE_HELPER(T) \
70  template bool hdf5_read_vector_attribute_helper(std::string const &, T *, attribute_type const &, type_type const &, \
71  std::vector<std::size_t> const &, \
72  std::vector<std::size_t> const &);
74  }
75  }
76 }
#define ALPS_HDF5_READ_VECTOR_ATTRIBUTE_HELPER(T)
ALPS_FOREACH_NATIVE_HDF5_TYPE(ALPS_HDF5_READ_SCALAR)
U cast(T const &)
Definition: cast.hpp:151
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37