ALPSCore reference
boost_optional.hpp
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 #ifndef ALPS_HDF5_BOOST_OPTIONAL_4ea49b8c8c814a52b0b3c6ac5bcc7839
8 #define ALPS_HDF5_BOOST_OPTIONAL_4ea49b8c8c814a52b0b3c6ac5bcc7839
9 
10 #include <boost/optional.hpp>
11 
12 namespace alps {
13  namespace hdf5 {
14 
15  template <typename T>
16  void save(alps::hdf5::archive& ar, const std::string& path,
17  const boost::optional<T>& value,
18  std::vector<std::size_t> /*size*/=std::vector<std::size_t>(),
19  std::vector<std::size_t> /*chunk*/=std::vector<std::size_t>(),
20  std::vector<std::size_t> /*offset*/=std::vector<std::size_t>())
21  {
22  if (ar.is_group(path)) ar.delete_group(path);
23  if (!value) {
24  ar.write(path,T()); // FIXME?: T must have a default constructor then.
25  ar.write(path+"/@alps_hdf5_optional_empty", true);
26  } else {
27  ar.write(path,*value);
28  ar.write(path+"/@alps_hdf5_optional_empty", false);
29  }
30  }
31 
32  template <typename T>
33  void load(alps::hdf5::archive& ar, const std::string& path,
34  boost::optional<T>& value,
35  std::vector<std::size_t> /*size*/=std::vector<std::size_t>(),
36  std::vector<std::size_t> /*chunk*/=std::vector<std::size_t>(),
37  std::vector<std::size_t> /*offset*/=std::vector<std::size_t>())
38  {
39  bool is_empty=false;
40  ar.read(path+"/@alps_hdf5_optional_empty", is_empty);
41  if (is_empty) {
42  value = boost::none;
43  } else {
44  ar.read(path, *value);
45  }
46  }
47 
48  } // hdf5::
49 } // alps::
50 
51 #endif /* ALPS_HDF5_BOOST_OPTIONAL_4ea49b8c8c814a52b0b3c6ac5bcc7839 */
void load(archive &ar, std::string const &path, T &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:309
void delete_group(std::string path) const
Definition: archive.cpp:376
auto read(std::string path, T *, std::vector< std::size_t >, std::vector< std::size_t >=std::vector< std::size_t >()) const -> typename std::enable_if<!is_native_type< T >::value, void >::type
Definition: archive.hpp:163
auto write(std::string path, T const *value, std::vector< std::size_t > size, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t > offset=std::vector< std::size_t >()) const -> typename std::enable_if<!is_native_type< T >::value, void >::type
Definition: archive.hpp:172
bool is_group(std::string path) const
Definition: archive.cpp:189
void save(archive &ar, std::string const &path, T const &value, std::vector< std::size_t >=std::vector< std::size_t >(), std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:292