Utopia  2
Framework for studying models of complex & adaptive systems.
hdfbufferfactory.hh
Go to the documentation of this file.
1 
8 #ifndef UTOPIA_DATAIO_HDFBUFFERFACTORY_HH
9 #define UTOPIA_DATAIO_HDFBUFFERFACTORY_HH
10 
11 #include <vector>
12 
13 #include <hdf5.h>
14 
15 #include <boost/multi_array.hpp>
16 
17 #include "hdftype.hh"
18 #include "hdfutilities.hh"
19 
20 namespace Utopia
21 {
22 namespace DataIO
23 {
41 {
42  private:
43 
48  std::shared_ptr< spdlog::logger > _log;
49 
50  public:
61  template < typename T >
62  static auto
63  convert_source(T& source)
64  {
65  if constexpr (std::is_same_v< T, std::string >)
66  {
67  return source.c_str();
68  }
69  else
70  {
71  hvl_t value;
72  value.len = source.size();
73  value.p = &(source[0]);
74  return value;
75  }
76  }
77 
94  template < typename Iter, typename Adaptor >
95  static auto
96  buffer(Iter begin, Iter end, Adaptor&& adaptor)
97  {
98  using T = Utils::remove_qualifier_t< decltype(adaptor(*begin)) >;
99  if constexpr (Utils::is_container_v< T >)
100  {
101 
102  // set up buffer
103  if constexpr (Utils::is_array_like_v< T >)
104  {
105  std::vector< T > data_buffer(std::distance(begin, end));
106  auto buffer_begin = data_buffer.begin();
107  for (; begin != end; ++begin, ++buffer_begin)
108  {
109  *buffer_begin = adaptor(*begin);
110  }
111  return data_buffer;
112  }
113  else
114  {
115  std::vector< hvl_t > data_buffer(std::distance(begin, end));
116 
117  auto buffer_begin = data_buffer.begin();
118  for (; begin != end; ++begin, ++buffer_begin)
119  {
120  *buffer_begin = convert_source(adaptor(*begin));
121  }
122 
123  return data_buffer;
124  }
125  }
126  else if constexpr (std::is_same_v< T, std::string >)
127  {
128  // set up buffer
129 
130  std::vector< const char* > data_buffer(std::distance(begin, end));
131 
132  auto buffer_begin = data_buffer.begin();
133  for (auto it = begin; it != end; ++it, ++buffer_begin)
134  {
135  *buffer_begin = convert_source(adaptor(*it));
136  }
137 
138  return data_buffer;
139  }
140 
141  else
142  { // not a container
143  // set up buffer
144  std::vector< T > data_buffer(std::distance(begin, end));
145 
146  // make buffer
147  auto buffer_begin = data_buffer.begin();
148  for (; begin != end; ++begin, ++buffer_begin)
149  {
150  *buffer_begin = adaptor(*begin);
151  }
152  return data_buffer;
153  }
154  }
155 }; // end of group HDF5 // end of group DataIO
158 
159 } // namespace DataIO
160 } // namespace Utopia
161 
162 #endif
Class which turns non-vector or plain-array containers into vectors. If the value_types are container...
Definition: hdfbufferfactory.hh:41
static auto buffer(Iter begin, Iter end, Adaptor &&adaptor)
static function for turning an iterator range with arbitrarty datatypes into a vector of data as retu...
Definition: hdfbufferfactory.hh:96
std::shared_ptr< spdlog::logger > _log
For logging all kinds of stuff.
Definition: hdfbufferfactory.hh:48
static auto convert_source(T &source)
function for converting source data into variable length type
Definition: hdfbufferfactory.hh:63
This file provides a class which is responsible for the automatic conversion between C/C++ types and ...
This file provides metafunctions for automatically determining the nature of a C/C++ types at compile...
auto end(zip< Containers... > &zipper)
end function like std::end
Definition: zip.hh:550
auto begin(zip< Containers... > &zipper)
Begin function like std::begin.
Definition: zip.hh:537
typename remove_qualifier< T >::type remove_qualifier_t
Shorthand for 'typename remove_qualifier::value'.
Definition: type_traits.hh:97
Definition: agent.hh:11