Skip to content

Commit

Permalink
Merge pull request #258 from dglaeser/feature/generic-reader-direct-open
Browse files Browse the repository at this point in the history
Feature/generic-reader-immediate-open
  • Loading branch information
dglaeser authored Apr 28, 2024
2 parents b83814f + 9e3b4c1 commit 51668e7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

- __Traits__: support for writing `Dune::FieldMatrix` as tensor field data.
- __CI__: on PRs, a bunch of performance benchmarks are run with the code of the PR and the target branch, and observed runtime differences are posted as a comment to the pull request.
- __Reader__: the `Reader` class now allows for opening a file upon instantiation using `GridFormat::Reader::from(filename)` (taking further optional constructor arguments). Moreover, you can now open a file and receive the modified reader as return value using `reader.with_opened(filename)`.

## Deprecated interfaces

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main () {
const auto written_file = writer.write("my_test_file"); // extension is added by the writer

// read the data back in (here we create a generic reader, but you can also select specific ones)
GridFormat::Reader reader; reader.open(written_file);
auto reader = GridFormat::Reader::from(written_file);
std::vector<double> cell_field_values(reader.number_of_cells());
std::vector<double> point_field_values(reader.number_of_points());
reader.cell_field("cell_field")->export_to(cell_field_values);
Expand Down
18 changes: 18 additions & 0 deletions gridformat/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ class Reader : public GridReader {
: _reader{_make_unique(ReaderFactory<FileFormat>::make(f, c))}
{}

//! Construct a reader instance from the given arguments, while directly opening the given file
template<typename... ConstructorArgs>
static Reader from(const std::string& filename, ConstructorArgs&&... args) {
return Reader{std::forward<ConstructorArgs>(args)...}.with_opened(filename);
}

//! Read the data from the given file and return this reader
Reader& with_opened(const std::string& filename) & {
this->open(filename);
return *this;
}

//! Read the data from the given file and return this reader (lvalue references overload)
Reader&& with_opened(const std::string& filename) && {
this->open(filename);
return std::move(*this);
}

private:
template<typename ReaderImpl>
auto _make_unique(ReaderImpl&& reader) const {
Expand Down
16 changes: 12 additions & 4 deletions test/api/test_generic_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ std::ranges::range auto test_filenames(const std::filesystem::path& folder, cons
void test_reader(GridFormat::Reader&& reader, const std::string& filename) {
std::cout << "Testing reader with '" << GridFormat::as_highlight(filename) << "'" << std::endl;

reader.open(filename);
const auto points = reader.points()->template export_to<std::vector<std::array<double, 3>>>();
const auto [_, space_dim] = grid_and_space_dimension(filename);
const auto get_expected_value = [&] (const std::array<double, 3>& position, double t = 1.0) -> double {
Expand Down Expand Up @@ -167,12 +166,21 @@ int test_reader(const std::filesystem::path& folder,
ConstructorArgs&&... args) {
bool visited = false;
std::ranges::for_each(test_filenames(folder, extension), [&] (const std::string& filename) {
test_reader(GridFormat::Reader{std::forward<ConstructorArgs>(args)...}, filename);
{
GridFormat::Reader reader{std::forward<ConstructorArgs>(args)...};
reader.open(filename);
test_reader(std::move(reader), filename);
}
{
test_reader(
GridFormat::Reader::from(filename, std::forward<ConstructorArgs>(args)...),
filename
);
}
visited = true;

if (extension == ".vtu") { // exemplarily test that reader propagates names
GridFormat::Reader reader{GridFormat::vtu};
reader.open(filename);
auto reader = GridFormat::Reader{GridFormat::vtu}.with_opened(filename);
GridFormat::Testing::expect(reader.name() == "VTUReader");
}
});
Expand Down

0 comments on commit 51668e7

Please sign in to comment.