Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings/cpp): make ReaderStream manage the lifetime of Reader #3164

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions bindings/cpp/include/opendal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ class Operator {
* @details It provides basic read and seek operations. If you want to use it
* like a stream, you can use `ReaderStream` instead.
* @code{.cpp}
* auto reader = operator.reader("path");
* opendal::ReaderStream stream(reader);
* opendal::ReaderStream stream(operator.reader("path"));
* @endcode
*/
class Reader
Expand All @@ -221,15 +220,19 @@ class Reader
/**
* @class ReaderStream
* @brief ReaderStream is a stream wrapper of Reader which can provide
* `iostream` interface.
* @note It's an undefined behavior to make multiple streams from one reader.
* `iostream` interface. It will keep a Reader inside so that you can ignore the
* lifetime of original Reader.
*/
class ReaderStream
: public boost::iostreams::stream<boost::reference_wrapper<Reader>> {
public:
ReaderStream(Reader &reader)
ReaderStream(Reader &&reader)
: boost::iostreams::stream<boost::reference_wrapper<Reader>>(
boost::ref(reader)) {}
boost::ref(reader_)),
reader_(std::move(reader)) {}

private:
Reader reader_;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion bindings/cpp/tests/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ TEST_F(OpendalTest, ReaderTest) {
reader.seek(0, std::ios::beg);

// stream
opendal::ReaderStream stream(reader);
opendal::ReaderStream stream(std::move(reader));

auto read_fn = [&](std::size_t to_read, std::streampos expected_tellg) {
std::vector<char> v(to_read);
Expand Down