openPMD-api
DatasetFiller.hpp
1 /* Copyright 2018-2021 Franz Poeschel
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include "openPMD/Dataset.hpp"
25 #include <memory>
26 #include <random>
27 
28 namespace openPMD
29 {
34 template <typename T>
36 {
37 protected:
38  Extent::value_type m_numberOfItems;
39 
40 public:
41  using resultType = T;
42 
43  explicit DatasetFiller(Extent::value_type numberOfItems = 0);
44 
47  virtual ~DatasetFiller() = default;
48 
55  virtual std::shared_ptr<T> produceData() = 0;
56 
61  virtual void setNumberOfItems(Extent::value_type numberOfItems) = 0;
62 };
63 
64 template <typename T>
65 DatasetFiller<T>::DatasetFiller(Extent::value_type numberOfItems)
66  : m_numberOfItems(numberOfItems)
67 {}
68 
69 template <typename DF>
71 {
72 public:
73  using resultType = typename DF::resultType;
74 
75 private:
76  std::shared_ptr<DF> m_df;
77 
78  template <typename T, typename Dummy = void>
79  struct Helper
80  {
81  std::shared_ptr<DatasetFiller<T>> operator()(std::shared_ptr<DF> &)
82  {
83  throw std::runtime_error(
84  "Can only create data of type " +
85  datatypeToString(determineDatatype<resultType>()));
86  }
87  };
88 
89  template <typename Dummy>
90  struct Helper<resultType, Dummy>
91  {
92  std::shared_ptr<DatasetFiller<resultType>>
93  operator()(std::shared_ptr<DF> &df)
94  {
95  return df;
96  }
97  };
98 
99 public:
100  explicit SimpleDatasetFillerProvider(DF df)
101  : m_df{std::make_shared<DF>(std::move(df))}
102  {}
103 
104  template <typename T>
105  std::shared_ptr<DatasetFiller<T>> operator()()
106  {
107  Helper<T> h;
108  return h(m_df);
109  }
110 };
111 
112 } // namespace openPMD
An abstract class to create one iteration of data per thread.
Definition: DatasetFiller.hpp:36
virtual std::shared_ptr< T > produceData()=0
Create a shared pointer of m_numberOfItems items of type T.
virtual ~DatasetFiller()=default
This class will be derived from.
virtual void setNumberOfItems(Extent::value_type numberOfItems)=0
Set number of items to be produced.
Definition: DatasetFiller.hpp:71
Public definitions of openPMD-api.
Definition: Date.cpp:29