ALPSCore reference
wrapper_set.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 #pragma once
8 
9 #include <alps/config.hpp>
10 #include <alps/hdf5/archive.hpp>
11 
12 #include <memory>
13 #include <mutex>
14 
15 namespace alps {
16  namespace accumulators {
17 
18  class accumulator_wrapper;
19  class result_wrapper;
20 
21  namespace detail {
22  template<typename T> struct serializable_type;
23  }
24 
25  namespace impl {
26 
27  template <typename T> class wrapper_set {
28 
29  public:
30  typedef T value_type;
31 
32  typedef typename std::map<std::string, std::shared_ptr<T> >::iterator iterator;
33  typedef typename std::map<std::string, std::shared_ptr<T> >::const_iterator const_iterator;
34 
35  // TODO: make trait ... to disable for result_wrapper
36  template <typename U> wrapper_set(wrapper_set<U> const & arg) {
37  for (typename wrapper_set<U>::const_iterator it = arg.begin(); it != arg.end(); ++it)
38  insert(it->first, it->second->result());
39  }
40 
41  wrapper_set();
42  wrapper_set(wrapper_set const &) {} // TODO: how do we handle that?
43 
44  T & operator[](std::string const & name);
45  T const & operator[](std::string const & name) const;
46 
47  bool has(std::string const & name) const;
48 
49  void insert(std::string const & name, std::shared_ptr<T> ptr);
50 
51  std::size_t size() const {
52  return m_storage.size();
53  }
54 
55  void save(hdf5::archive & ar) const;
56  void load(hdf5::archive & ar);
57 
59  template<typename A> static void register_serializable_type_nolock();
61  template<typename A> static void register_serializable_type();
62 
63  void print(std::ostream & os) const;
64 
65  iterator begin() { return m_storage.begin(); }
66  iterator end() { return m_storage.end(); }
67 
68  const_iterator begin() const { return m_storage.begin(); }
69  const_iterator end() const { return m_storage.end(); }
70 
71  void clear() { m_storage.clear(); }
72 
73  //
74  // These methods are valid only for T = accumulator_wrapper
75  //
76 
78  template<typename U = T>
79  typename std::enable_if<std::is_same<U, accumulator_wrapper>::value>::type
80  merge(wrapper_set const &rhs) {
81  iterator it1 = this->begin();
82  const_iterator it2 = rhs.begin();
83  for(; it1 != end(); ++it1, ++it2) {
84  if (it1->first != it2 ->first) throw std::logic_error("Can't merge" + it1->first + " and " + it2->first);
85  it1->second->merge(*(it2->second));
86  }
87  }
88 
89  template<typename U = T>
90  typename std::enable_if<std::is_same<U, accumulator_wrapper>::value>::type
91  reset() {
92  for(iterator it = begin(); it != end(); ++it)
93  it->second->reset();
94  }
95 
96  private:
97  std::map<std::string, std::shared_ptr<T> > m_storage;
98  static std::vector<std::shared_ptr<detail::serializable_type<T> > > m_types;
99  static std::mutex m_types_mutex;
100  };
101  template<typename T> std::vector<std::shared_ptr<detail::serializable_type<T> > > wrapper_set<T>::m_types;
102  template<typename T> std::mutex wrapper_set<T>::m_types_mutex;
103 
104  template<typename T> inline std::ostream & operator<<(std::ostream & os, const wrapper_set<T> & arg) {
105  arg.print(os);
106  return os;
107  }
108 
109  // Will be instantiated in wrapper_set.cpp and wrapper_set_hdf5.cpp
110  extern template class wrapper_set<accumulator_wrapper>;
111  extern template class wrapper_set<result_wrapper>;
112  }
113  }
114 }
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 print(std::ostream &os) const
Definition: wrapper_set.cpp:54
wrapper_set(wrapper_set< U > const &arg)
Definition: wrapper_set.hpp:36
std::ostream & print(std::ostream &s, const dict_value &dv, bool terse)
Definition: dict_value.cpp:186
std::map< std::string, std::shared_ptr< T > >::iterator iterator
Definition: wrapper_set.hpp:32
std::enable_if< std::is_same< U, accumulator_wrapper >::value >::type merge(wrapper_set const &rhs)
Merge another accumulator/result set into this one.
Definition: wrapper_set.hpp:80
std::map< std::string, std::shared_ptr< T > >::const_iterator const_iterator
Definition: wrapper_set.hpp:33
std::enable_if< std::is_same< U, accumulator_wrapper >::value >::type reset()
Definition: wrapper_set.hpp:91
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