echelon  0.8.0
sink_adaptors.hpp
1 // Copyright (c) 2014 Christopher Hinz
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef ECHELON_SINK_ADAPTORS_HPP
7 #define ECHELON_SINK_ADAPTORS_HPP
8 
9 #include <echelon/hdf5/container_adaption.hpp>
10 
11 namespace echelon
12 {
13 
14 template <typename ResizeableSink>
15 class auto_reshaper
16 {
17 public:
18  static_assert(hdf5::has_reshape_member<ResizeableSink>(),
19  "ResizeableSink does not fulfill the ResizeableSink requirements");
20 
21  explicit auto_reshaper(ResizeableSink& underlying_sink_) : underlying_sink_{&underlying_sink_}
22  {
23  }
24 
25  template <typename Source>
26  friend void operator<<=(auto_reshaper<ResizeableSink> sink, const Source& source)
27  {
28  auto shape = source.shape();
29 
30  std::vector<std::size_t> shape_;
31  shape_.reserve(shape.size());
32 
33  for(auto value : shape)
34  {
35  shape_.push_back(value);
36  }
37 
38  hdf5::reshape_adl(*sink.underlying_sink_, shape_);
39 
40  *sink.underlying_sink_ <<= source;
41  }
42 
43 private:
44  ResizeableSink* underlying_sink_;
45 };
46 
66 template <typename ResizeableSink>
67 auto_reshaper<ResizeableSink> auto_reshape(ResizeableSink& sink)
68 {
69  return auto_reshaper<ResizeableSink>(sink);
70 }
71 }
72 
73 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
auto_reshaper< ResizeableSink > auto_reshape(ResizeableSink &sink)
Add an adaptor to the sink which automatically reshapes the sink if needed.
Definition: sink_adaptors.hpp:67