Skip to content

Commit

Permalink
feat(bindings/cpp): make ReaderStream manage the lifetime of Reader (#…
Browse files Browse the repository at this point in the history
…3164)

Signed-off-by: silver-ymz <[email protected]>
  • Loading branch information
silver-ymz authored Sep 23, 2023
1 parent 5d4300e commit d93c13d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
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

0 comments on commit d93c13d

Please sign in to comment.