Skip to content

Commit

Permalink
algos use dynamic_buffer_view
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Aug 1, 2023
1 parent 4acc9c8 commit abe9d85
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 80 deletions.
11 changes: 2 additions & 9 deletions include/boost/async/io/copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <boost/async/io/stream.hpp>
#include <boost/async/promise.hpp>
#include <boost/async/io/buffers/any_dynamic_buffer.hpp>
#include <boost/async/io/buffers/dynamic_buffer_view.hpp>

namespace boost::async::io
{
Expand All @@ -19,16 +19,9 @@ BOOST_ASYNC_DECL promise<std::pair<transfer_result, transfer_result>>
copy(stream & source, stream & sink);

BOOST_ASYNC_DECL promise<std::pair<transfer_result, transfer_result>>
copy(stream & source, stream & sink, buffers::any_dynamic_buffer & buffer, std::size_t chunk_size = 4096);
copy(stream & source, stream & sink, buffers::dynamic_buffer_view buffer, std::size_t chunk_size = 4096);


template<buffers::dynamic_buffer DynamicBuffer>
promise<transfer_result> read(stream & source, stream & sink, DynamicBuffer & buffer, std::size_t chunk_size = 4096)
{
auto any = buffers::make_any(buffer);
buffers::any_dynamic_buffer & ab = any;
co_return co_await copy(source, sink, ab, chunk_size);
}

}

Expand Down
14 changes: 2 additions & 12 deletions include/boost/async/io/copy_n.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <boost/async/io/stream.hpp>
#include <boost/async/promise.hpp>
#include <boost/async/io/buffers/any_dynamic_buffer.hpp>
#include <boost/async/io/buffers/dynamic_buffer_view.hpp>

namespace boost::async::io
{
Expand All @@ -19,19 +19,9 @@ BOOST_ASYNC_DECL promise<std::pair<transfer_result, transfer_result>>
copy_n(stream & source, stream & sink, std::size_t n);

BOOST_ASYNC_DECL promise<std::pair<transfer_result, transfer_result>>
copy_n(stream & source, stream & sink, buffers::any_dynamic_buffer & buffer,
copy_n(stream & source, stream & sink, buffers::dynamic_buffer_view buffer,
std::size_t n, std::size_t chunk_size = 4096);


template<buffers::dynamic_buffer DynamicBuffer>
promise<transfer_result> copy_n(stream & source, stream & sink, DynamicBuffer & buffer,
std::size_t n, std::size_t chunk_size = 4096)
{
auto any = buffers::make_any(buffer);
buffers::any_dynamic_buffer & ab = any;
co_return co_await copy_n(source, sink, ab, n, chunk_size);
}

}

#endif //BOOST_ASYNC_IO_COPY_N_HPP
4 changes: 1 addition & 3 deletions include/boost/async/io/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include <boost/async/promise.hpp>
#include <boost/async/io/stream.hpp>
#include <boost/async/io/buffers/any_dynamic_buffer.hpp>
#include <boost/async/io/buffers/mutable_buffer_span.hpp>
#include <boost/async/io/buffers/range.hpp>
#include <boost/container/pmr/vector.hpp>

namespace boost::async::io
Expand All @@ -20,8 +20,6 @@ namespace boost::async::io
BOOST_ASYNC_DECL promise<transfer_result> read(stream & source, buffers::mutable_buffer buffer);
BOOST_ASYNC_DECL promise<transfer_result> read(stream & source, buffers::mutable_buffer_span buffer);
BOOST_ASYNC_DECL promise<transfer_result> read(stream & source, buffers::mutable_buffer_subspan buffer);
BOOST_ASYNC_DECL promise<transfer_result> read(stream & source, buffers::any_dynamic_buffer & buffer,
std::size_t chunk_size = 4096);

template<buffers::mutable_buffer_sequence MutableBufferSequence>
requires (!std::convertible_to<buffers::mutable_buffer_span, MutableBufferSequence>)
Expand Down
13 changes: 2 additions & 11 deletions include/boost/async/io/read_all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,16 @@

#include <boost/async/promise.hpp>
#include <boost/async/io/stream.hpp>
#include <boost/async/io/buffers/any_dynamic_buffer.hpp>
#include <boost/async/io/buffers/dynamic_buffer_view.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/container/pmr/vector.hpp>

namespace boost::async::io
{

BOOST_ASYNC_DECL promise<transfer_result> read_all(stream & source, buffers::any_dynamic_buffer & buffer,
BOOST_ASYNC_DECL promise<transfer_result> read_all(stream & source, buffers::dynamic_buffer_view buffer,
std::size_t chunk_size = 4096);

template<buffers::dynamic_buffer DynamicBuffer>
promise<transfer_result> read_all(stream & source, DynamicBuffer &&buffer, std::size_t chunk_size = 4096)
{
auto any = buffers::make_any(std::forward<DynamicBuffer>(buffer));
buffers::any_dynamic_buffer & ab = any;
co_return co_await read_all(source, ab, chunk_size);
}


}

#endif //BOOST_ASYNC_IO_READ_ALL_HPP
25 changes: 3 additions & 22 deletions include/boost/async/io/read_until.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,18 @@

#include <boost/async/promise.hpp>
#include <boost/async/io/stream.hpp>
#include <boost/async/io/buffers/any_dynamic_buffer.hpp>
#include <boost/async/io/buffers/dynamic_buffer_view.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/container/pmr/vector.hpp>

namespace boost::async::io
{

BOOST_ASYNC_DECL promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer & buffer,
BOOST_ASYNC_DECL promise<transfer_result> read_until(stream & source, buffers::dynamic_buffer_view buffer,
char delim, std::size_t chunk_size = 4096);
BOOST_ASYNC_DECL promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer & buffer,
BOOST_ASYNC_DECL promise<transfer_result> read_until(stream & source, buffers::dynamic_buffer_view buffer,
core::string_view delim, std::size_t chunk_size = 4096);


template<buffers::dynamic_buffer DynamicBuffer>
promise<transfer_result> read_until(stream & source, DynamicBuffer &&buffer,
char delim, std::size_t chunk_size = 4096)
{
auto any = buffers::make_any(std::forward<DynamicBuffer>(buffer));
buffers::any_dynamic_buffer & ab = any;
co_return co_await read_until(source, ab, delim, chunk_size);
}

template<buffers::dynamic_buffer DynamicBuffer>
promise<transfer_result> read_until(stream & source, DynamicBuffer &&buffer,
core::string_view delim, std::size_t chunk_size = 4096)
{
auto any = buffers::make_any(std::forward<DynamicBuffer>(buffer));
buffers::any_dynamic_buffer & ab = any;
co_return co_await read_until(source, ab, delim, chunk_size);
}

}

#endif //BOOST_ASYNC_IO_READ_UNTIL_HPP
2 changes: 1 addition & 1 deletion include/boost/async/io/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include <boost/async/promise.hpp>
#include <boost/async/io/stream.hpp>
#include <boost/async/io/buffers/any_dynamic_buffer.hpp>
#include <boost/async/io/buffers/const_buffer_span.hpp>
#include <boost/async/io/buffers/range.hpp>
#include <boost/container/pmr/vector.hpp>

namespace boost::async::io
Expand Down
8 changes: 4 additions & 4 deletions src/io/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ copy(stream & source, stream & sink)
w = {};

buf.commit(r.transferred);
printf("Copy 1 %ld/%ld ==> \n", r.transferred, w.transferred, 1024 * 1024);

while (!r.has_error() && !w.has_error())
{
Expand All @@ -40,18 +41,17 @@ copy(stream & source, stream & sink)
// remaining readable stuff
while (r.has_error() && !w.has_error() && buffers::buffer_size(buf.data()) > 0u)
{
assert(false);
auto w2 = co_await sink.write_some(buffers::const_buffer_span(buf.data()));
buf.consume(w2.transferred);
w.transferred = w2.transferred;
w.error = w2.error;
}

printf("Copy %ld/%ld ==> \n", r.transferred, w.transferred, 1024 * 1024);
co_return {r, w};
}

promise<std::pair<transfer_result, transfer_result>>
copy(stream & source, stream & sink, buffers::any_dynamic_buffer & buf, std::size_t chunk_size)
copy(stream & source, stream & sink, buffers::dynamic_buffer_view buf, std::size_t chunk_size)
{
transfer_result r = co_await source.read_some(buffers::mutable_buffer_span(buf.prepare(chunk_size))),
w = {};
Expand All @@ -73,12 +73,12 @@ copy(stream & source, stream & sink, buffers::any_dynamic_buffer & buf, std::siz
// remaining readable stuff
while (r.has_error() && !w.has_error() && buffers::buffer_size(buf.data()) > 0u)
{
assert(false);
auto w2 = co_await sink.write_some(buffers::const_buffer_span(buf.data()));
buf.consume(w2.transferred);
w.transferred = w2.transferred;
w.error = w2.error;
}
printf("Copy %ld/%ld\n", r.transferred, w.transferred);
co_return {r, w};
}

Expand Down
2 changes: 1 addition & 1 deletion src/io/copy_n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ copy_n(stream & source, stream & sink, std::size_t n)
}

promise<std::pair<transfer_result, transfer_result>>
copy_n(stream & source, stream & sink, buffers::any_dynamic_buffer & buf,
copy_n(stream & source, stream & sink, buffers::dynamic_buffer_view buf,
std::size_t n, std::size_t chunk_size)
{
transfer_result r = co_await source.read_some(buffers::mutable_buffer_span(buf.prepare(chunk_size))),
Expand Down
6 changes: 3 additions & 3 deletions src/io/read_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
namespace boost::async::io
{

promise<transfer_result> read_all(stream & source, buffers::any_dynamic_buffer & buffer, std::size_t chunk_size)
promise<transfer_result> read_all(stream & source, buffers::dynamic_buffer_view buffer, std::size_t chunk_size)
{
transfer_result tr;

do
{
auto rd = co_await source.read_some(buffer.prepare(chunk_size));
auto rd = co_await source.read_some(buffer.prepare((std::min)(chunk_size, buffer.max_size() - buffer.size())));
tr.transferred += rd.transferred;
tr.error = rd.error;
buffer.commit(rd.transferred);
}
while (buffer.size() >= 0 && !tr.has_error());
while (buffer.max_size() > buffer.size() && !tr.has_error());
co_return tr;
}

Expand Down
10 changes: 5 additions & 5 deletions src/io/read_until.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
namespace boost::async::io
{

promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer & buffer,
promise<transfer_result> read_until(stream & source, buffers::dynamic_buffer_view buffer,
char delim, std::size_t chunk_size)
{
transfer_result tr{};

bool matched = false;
do
{
auto buf = buffer.prepare(chunk_size);
auto buf = buffer.prepare((std::min)(chunk_size, buffer.max_size() - buffer.size()));
auto rd = co_await source.read_some(buf);

auto begin = asio::buffers_begin(buf);
Expand All @@ -39,7 +39,7 @@ promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer
co_return tr;
}

promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer & buffer,
promise<transfer_result> read_until(stream & source, buffers::dynamic_buffer_view buffer,
core::string_view delim, std::size_t chunk_size)
{
transfer_result tr;
Expand All @@ -51,7 +51,7 @@ promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer
bool matched = false;
do
{
auto buf = buffer.prepare(chunk_size);
auto buf = buffer.prepare((std::min)(chunk_size, buffer.max_size() - buffer.size()));
auto rd = co_await source.read_some(buf);
tr.transferred += rd.transferred;
tr.error = rd.error;
Expand All @@ -63,7 +63,7 @@ promise<transfer_result> read_until(stream & source, buffers::any_dynamic_buffer
auto itr = std::search(begin, end, delim.begin(), delim.end());
matched = (itr != end);
}
while (buffer.size() > 0 && !tr.has_error() && !matched);
while (buffer.max_size() >= buffer.size() && !tr.has_error() && !matched);
co_return tr;
}

Expand Down
5 changes: 2 additions & 3 deletions src/io/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ namespace boost::async::io

promise<transfer_result> write(stream & source, buffers::const_buffer buffer)
{
transfer_result tr;
transfer_result tr{};

do
{
auto rd = co_await source.write_some(buffer);
tr.transferred += rd.transferred;
tr.error = rd.error;
tr += rd;
buffer += rd.transferred;
}
while (buffer.size() > 0 && !tr.has_error());
Expand Down
9 changes: 5 additions & 4 deletions test/io/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

using namespace boost::async;

promise<void> do_write__(io::stream & str, std::string & input )
promise<void> do_write__(io::stream & str, const std::string & input )
{
boost::ignore_unused(co_await io::write(str, {input.data(), input.size()}));
auto w = co_await io::write(str, input);
CHECK(w.transferred == input.size());
str.close().value();
};

Expand Down Expand Up @@ -53,12 +54,12 @@ CO_TEST_CASE("copy")
auto p = do_write__(w, input);
auto c = do_copy(ri, wi);
std::string output;
auto res = co_await io::read_all(r, io::buffers::string_buffer(&output));
auto res = co_await io::read_all(r, output);

CHECK(res.transferred == output.size());
CHECK(res.transferred == input.size());

CHECK(std::equal(output.begin(), output.end(), input.begin()));
CHECK(std::equal(output.begin(), std::next(output.begin(), res.transferred), input.begin()));

CHECK(!r.close().has_error());
co_await p;
Expand Down
4 changes: 2 additions & 2 deletions test/io/read_until.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CO_TEST_CASE("read_until char")


std::string output;
auto res = co_await io::read_until(r, io::buffers::string_buffer(&output), '\n');
auto res = co_await io::read_until(r, output, '\n');

CHECK(res.transferred >= 4242);
CHECK(res.transferred < 10000);
Expand Down Expand Up @@ -75,7 +75,7 @@ CO_TEST_CASE("read_until string")


std::string output;
auto res = co_await io::read_until(r, io::buffers::string_buffer(&output), "\r\n");
auto res = co_await io::read_until(r, output, "\r\n");

CHECK(res.transferred >= 8001);
CHECK(res.transferred < 16000);
Expand Down

0 comments on commit abe9d85

Please sign in to comment.