ALPSCore reference
archive_free.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 <string>
9 #include <vector>
10 
11 #include <alps/hdf5/archive.hpp>
12 #include <alps/utilities/cast.hpp>
13 
14 #include "common.hpp"
15 #include "archivecontext.hpp"
16 
17 namespace alps {
18  namespace hdf5 {
19 
20  namespace detail {
21 
22  hid_t open_attribute(archive const & ar, hid_t file_id, std::string path) {
23  if ((path = ar.complete_path(path)).find_last_of('@') == std::string::npos)
24  throw invalid_path("no attribute path: " + path + ALPS_STACKTRACE);
25  return H5Aopen_by_name(file_id, path.substr(0, path.find_last_of('@')).c_str(), path.substr(path.find_last_of('@') + 1).c_str(), H5P_DEFAULT, H5P_DEFAULT);
26  }
27 
28  herr_t list_children_visitor(hid_t, char const * n, const H5L_info_t *, void * d) {
29  reinterpret_cast<std::vector<std::string> *>(d)->push_back(n);
30  return 0;
31  }
32 
33  herr_t list_attributes_visitor(hid_t, char const * n, const H5A_info_t *, void * d) {
34  reinterpret_cast<std::vector<std::string> *>(d)->push_back(n);
35  return 0;
36  }
37  }
38 
39  #define ALPS_HDF5_IMPLEMENT_FREE_FUNCTIONS(T) \
40  namespace detail { \
41  alps::hdf5::scalar_type< T >::type * get_pointer< T >::apply( T & value) { \
42  return &value; \
43  } \
44  \
45  alps::hdf5::scalar_type< T >::type const * get_pointer< T const >::apply( T const & value) { \
46  return &value; \
47  } \
48  \
49  bool is_vectorizable< T >::apply(T const &) { \
50  return true; \
51  } \
52  bool is_vectorizable< T const >::apply(T &) { \
53  return true; \
54  } \
55  } \
56  \
57  void save( \
58  archive & ar \
59  , std::string const & path \
60  , T const & value \
61  , std::vector<std::size_t> size \
62  , std::vector<std::size_t> chunk \
63  , std::vector<std::size_t> offset \
64  ) { \
65  if (!size.size()) \
66  ar.write(path, value); \
67  else \
68  ar.write(path, get_pointer(value), size, chunk, offset); \
69  } \
70  \
71  void load( \
72  archive & ar \
73  , std::string const & path \
74  , T & value \
75  , std::vector<std::size_t> chunk \
76  , std::vector<std::size_t> offset \
77  ) { \
78  if (!chunk.size()) \
79  ar.read(path, value); \
80  else \
81  ar.read(path, get_pointer(value), chunk, offset); \
82  }
83 
85  #undef ALPS_HDF5_IMPLEMENT_FREE_FUNCTIONS
86 
87  namespace detail {
88  template<typename T> struct is_datatype_impl_compare {
89  static bool apply(type_type const & native_id) {
90  return check_error(
91  H5Tequal(type_type(H5Tcopy(native_id)), type_type(get_native_type(typename alps::detail::type_wrapper<T>::type())))
92  ) > 0;
93  }
94  };
95  template<> struct is_datatype_impl_compare<std::string> {
96  static bool apply(type_type const & native_id) {
97  return H5Tget_class(native_id) == H5T_STRING;
98  }
99  };
100  }
101 
102  template<typename T>
103  auto archive::is_datatype_impl(std::string path, T) const -> ONLY_NATIVE(T, bool) {
105  hid_t type_id;
106  path = complete_path(path);
107  if (context_ == NULL)
108  throw archive_closed("the archive is closed" + ALPS_STACKTRACE);
109  if (path.find_last_of('@') != std::string::npos && is_attribute(path)) {
110  detail::attribute_type attr_id(detail::open_attribute(*this, context_->file_id_, path));
111  type_id = H5Aget_type(attr_id);
112  } else if (path.find_last_of('@') == std::string::npos && is_data(path)) {
113  detail::data_type data_id(H5Dopen2(context_->file_id_, path.c_str(), H5P_DEFAULT));
114  type_id = H5Dget_type(data_id);
115  } else
116  throw path_not_found("no valid path: " + path + ALPS_STACKTRACE);
117  detail::type_type native_id(H5Tget_native_type(type_id, H5T_DIR_ASCEND));
118  detail::check_type(type_id);
119  {
121  return detail::is_datatype_impl_compare< T >::apply(native_id);
122  }
123  }
124  #define ALPS_HDF5_IS_DATATYPE_IMPL_IMPL(T) template bool archive::is_datatype_impl<T>(std::string, T) const;
126  }
127 }
auto is_datatype_impl(std::string path, T) const -> typename std::enable_if< is_native_type< T >::value, bool >::type
#define ALPS_HDF5_LOCK_MUTEX
Definition: common.hpp:19
ALPS_FOREACH_NATIVE_HDF5_TYPE(ALPS_HDF5_READ_SCALAR)
#define ONLY_NATIVE(T, R)
Definition: archive.hpp:56
#define ALPS_HDF5_IS_DATATYPE_IMPL_IMPL(T)
#define ALPS_HDF5_FAKE_THREADSAFETY
Definition: common.hpp:25
#define ALPS_HDF5_IMPLEMENT_FREE_FUNCTIONS(T)
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37