8#ifndef GRIDFORMAT_READER_HPP_
9#define GRIDFORMAT_READER_HPP_
22#include <gridformat/common/type_traits.hpp>
23#include <gridformat/parallel/concepts.hpp>
24#include <gridformat/parallel/communication.hpp>
29namespace FileFormat {
struct Any; }
31template<
typename FileFormat>
35template<Concepts::Communicator C = NullCommunicator>
40 std::unique_ptr<GridReader> make_for(
const std::string& filename)
const;
46namespace ReaderDetail {
48 template<
typename FileFormat>
49 concept SequentiallyConstructible
50 = is_complete<ReaderFactory<FileFormat>>
51 and
requires(
const FileFormat& f) {
55 template<
typename FileFormat,
typename Communicator>
56 concept ParallelConstructible
57 = is_complete<ReaderFactory<FileFormat>>
58 and Concepts::Communicator<Communicator>
59 and
requires(
const FileFormat& f,
const Communicator& comm) {
60 { ReaderFactory<FileFormat>::make(f, comm) } -> std::derived_from<GridReader>;
63 template<
typename FieldNameStorage>
64 void copy_field_names(
const GridReader& reader, FieldNameStorage& names) {
65 std::ranges::copy(cell_field_names(reader), std::back_inserter(names.cell_fields));
66 std::ranges::copy(point_field_names(reader), std::back_inserter(names.point_fields));
67 std::ranges::copy(meta_data_field_names(reader), std::back_inserter(names.meta_data_fields));
70 using ReaderFactoryFunctor = std::function<std::unique_ptr<GridReader>(
const std::string&)>;
72 template<Concepts::Communicator C = NullCommunicator>
73 ReaderFactoryFunctor default_reader_factory(
const C& c = {}) {
74 if (!std::same_as<C, NullCommunicator>)
75 return [fac = AnyReaderFactory<C>{c}] (
const std::string& f) {
return fac.make_for(f); };
76 return [fac = AnyReaderFactory<>{}] (
const std::string& f) {
return fac.make_for(f); };
96 using ReaderFactoryFunctor = ReaderDetail::ReaderFactoryFunctor;
98 Reader(ReaderFactoryFunctor f = ReaderDetail::default_reader_factory())
102 explicit Reader(
const FileFormat::Any&, ReaderFactoryFunctor f = ReaderDetail::default_reader_factory())
106 template<Concepts::Communicator C>
108 :
Reader(ReaderDetail::default_reader_factory(c))
111 template<ReaderDetail::SequentiallyConstructible FileFormat>
112 explicit Reader(
const FileFormat& f)
116 template<
typename FileFormat,
117 Concepts::Communicator Communicator>
118 requires(ReaderDetail::ParallelConstructible<FileFormat, Communicator>)
119 explicit Reader(
const FileFormat& f,
const Communicator& c)
124 template<
typename... ConstructorArgs>
138 return std::move(*
this);
142 template<
typename ReaderImpl>
143 auto _make_unique(ReaderImpl&& reader)
const {
144 return std::make_unique<std::remove_cvref_t<ReaderImpl>>(std::move(reader));
147 std::string _name()
const override {
148 if (_reader_is_set())
149 return _access_reader().
name();
153 void _open(
const std::string&
filename,
typename GridReader::FieldNames& names)
override {
155 _reader = (*_reader_factory)(
filename);
156 _access_reader().
close();
158 ReaderDetail::copy_field_names(_access_reader(), names);
161 void _close()
override {
162 _access_reader().
close();
165 std::size_t _number_of_cells()
const override {
169 std::size_t _number_of_points()
const override {
173 std::size_t _number_of_pieces()
const override {
177 FieldPtr _cell_field(std::string_view
name)
const override {
181 FieldPtr _point_field(std::string_view
name)
const override {
185 FieldPtr _meta_data_field(std::string_view
name)
const override {
189 void _visit_cells(
const CellVisitor& v)
const override {
194 return _access_reader().
points();
197 typename GridReader::PieceLocation _location()
const override {
201 std::vector<double> _ordinates(
unsigned int dir)
const override {
205 std::array<double, 3> _spacing()
const override {
206 return _access_reader().
spacing();
209 std::array<double, 3> _origin()
const override {
210 return _access_reader().
origin();
213 std::array<double, 3> _basis_vector(
unsigned int dir)
const override {
217 bool _is_sequence()
const override {
221 std::size_t _number_of_steps()
const override {
225 double _time_at_step(std::size_t step)
const override {
229 void _set_step(std::size_t step,
typename GridReader::FieldNames& field_names)
override {
232 ReaderDetail::copy_field_names(_access_reader(), field_names);
235 const GridReader& _access_reader()
const {
236 _check_reader_access();
240 GridReader& _access_reader() {
241 _check_reader_access();
245 void _check_reader_access()
const {
246 if (!_reader_is_set())
247 throw InvalidState(
"No file has been read");
250 bool _reader_is_set()
const {
251 return static_cast<bool>(_reader);
254 std::unique_ptr<GridReader> _reader;
255 std::optional<ReaderFactoryFunctor> _reader_factory;
Base class for grid data readers.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186