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 
25 #include <memory>
26 #include "openPMD/Dataset.hpp"
27 #include <random>
28 
29 
30 namespace openPMD
31 {
36  template< typename T >
38  {
39  protected:
40  Extent::value_type m_numberOfItems;
41  public:
42  using resultType = T;
43 
44  explicit DatasetFiller( Extent::value_type numberOfItems = 0 );
45 
48  virtual ~DatasetFiller() = default;
49 
56  virtual std::shared_ptr< T > produceData( ) = 0;
57 
62  virtual void setNumberOfItems( Extent::value_type numberOfItems ) = 0;
63  };
64 
65 
66  template< typename T >
67  DatasetFiller< T >::DatasetFiller( Extent::value_type numberOfItems ) :
68  m_numberOfItems( numberOfItems )
69  {}
70 
71 
72  template< typename DF >
74  {
75  public:
76  using resultType = typename DF::resultType;
77  private:
78  std::shared_ptr< DF > m_df;
79 
80 
81  template<
82  typename T,
83  typename Dummy=void
84  >
85  struct Helper
86  {
87  std::shared_ptr< DatasetFiller< T>> operator()( std::shared_ptr<DF> & )
88  {
89  throw std::runtime_error(
90  "Can only create data of type " +
91  datatypeToString( determineDatatype< resultType >( ) )
92  );
93  }
94  };
95 
96  template< typename Dummy >
97  struct Helper<
98  resultType,
99  Dummy
100  >
101  {
102  std::shared_ptr< DatasetFiller< resultType>> operator()(std::shared_ptr<DF> &df )
103  {
104  return df;
105  }
106  };
107 
108  public:
109 
110 
111  explicit SimpleDatasetFillerProvider( DF df ) :
112  m_df { std::make_shared< DF >( std::move( df ) ) }
113  {}
114 
115 
116  template< typename T >
117  std::shared_ptr< DatasetFiller< T >> operator()( )
118  {
119  Helper< T > h;
120  return h( m_df );
121  }
122  };
123 
124 
125 }
openPMD::DatasetFiller::produceData
virtual std::shared_ptr< T > produceData()=0
Create a shared pointer of m_numberOfItems items of type T.
openPMD::DatasetFiller
An abstract class to create one iteration of data per thread.
Definition: DatasetFiller.hpp:37
openPMD::UnitDimension::T
@ T
time
openPMD::DatasetFiller::~DatasetFiller
virtual ~DatasetFiller()=default
This class will be derived from.
openPMD
Public definitions of openPMD-api.
Definition: Date.cpp:29
openPMD::DatasetFiller::setNumberOfItems
virtual void setNumberOfItems(Extent::value_type numberOfItems)=0
Set number of items to be produced.
openPMD::SimpleDatasetFillerProvider
Definition: DatasetFiller.hpp:73