Skip to content

Commit

Permalink
Merge pull request #33 from basiliscos/ivan/issue-32
Browse files Browse the repository at this point in the history
relax requirements: use c++11 instead c++14
  • Loading branch information
basiliscos authored Aug 10, 2019
2 parents 9efae77 + 30aecfd commit 15b0202
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 133 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
cmake_minimum_required(VERSION 3.0)
project (bredis)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include (CTest)
enable_testing()



add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)
add_definitions(-DBOOST_COROUTINES_NO_DEPRECATION_WARNING)
#add_definitions(-DBREDIS_DEBUG)
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Boost::ASIO low-level redis client (connector)

## Changelog

### 0.08
- relaxed c++ compiler requirements: c++11 can be used instead of c++14

### 0.07
- minor parsing speed improvements (upto 10% in synthetic tests)
- fix compilation issues on boost::asio 1.70
Expand Down Expand Up @@ -213,6 +216,7 @@ in case you don't want the throw-exception behaviour.
...
namespace r = bredis;
namespace asio = boost::asio;
namespace sys = boost::system;
...
using socket_t = asio::ip::tcp::socket;
using Buffer = boost::asio::streambuf;
Expand All @@ -229,10 +233,10 @@ socket.connect(end_point);
...
Buffer tx_buff, rx_buff;
c.async_write(
tx_buff, r::single_command_t{"llen", "my-queue"}, [&](const auto &error_code, auto bytes_transferred) {
tx_buff, r::single_command_t{"llen", "my-queue"}, [&](const sys::error_code &ec, std::size_t bytes_transferred) {
/* tx_buff must be consumed when it is no longer needed */
tx_buff.consume(bytes_transferred);
c.async_read(rx_buff, [&](const auto &error_code, result_t &&r) {
c.async_read(rx_buff, [&](const sys::error_code &ec, result_t &&r) {
/* see above how to work with the result */
auto extract = boost::apply_visitor(r::extractor<Iterator>(), r.result);
auto &queue_size = boost::get<r::extracts::int_t>(extract);
Expand Down Expand Up @@ -350,7 +354,7 @@ in the transaction (i.e. results for `INCR` and `GET` above)
```cpp
Buffer rx_buff;
c.async_read(rx_buff, [&](const auto& error_code, auto&& r){
c.async_read(rx_buff, [&](const sys::error_code &ec, result_t&& r){
auto &replies = boost::get<r::markers::array_holder_t<Iterator>>(r.result);
/* scan stream for OK, QUEUED, QUEUED */
...
Expand Down
18 changes: 12 additions & 6 deletions examples/multi-threads-1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
namespace r = bredis;
namespace asio = boost::asio;
namespace po = boost::program_options;
namespace sys = boost::system;

using socket_t = asio::ip::tcp::socket;
using next_layer_t = socket_t;
//using next_layer_t = r::test::SocketWithLogging<asio::ip::tcp::socket>;
using Buffer = boost::asio::streambuf;
using Iterator = typename r::to_iterator<Buffer>::iterator_t;
using Policy = r::parsing_policy::keep_result;
using result_t = r::positive_parse_result_t<Iterator, Policy>;
using Connection = r::Connection<next_layer_t>;

struct redis_accessor_t {
Expand All @@ -64,8 +67,9 @@ struct producer_t {
redis.conn.async_write(
redis.tx_buff,
cmd_ping,
asio::bind_executor(redis.strand, [self = this](const auto &error_code, size_t bytes_transferred){
if (!error_code){
asio::bind_executor(redis.strand, [this](const sys::error_code &ec, std::size_t bytes_transferred){
if (!ec){
auto self = this;
self->redis.ping_count++;
self->redis.tx_buff.consume(bytes_transferred);
self->produce();
Expand All @@ -83,8 +87,9 @@ struct consumer_t {
void consume(){
redis.conn.async_read(
redis.rx_buff,
asio::bind_executor(redis.strand, [self = this](const auto &error_code, auto &&r){
if(!error_code){
asio::bind_executor(redis.strand, [this](const sys::error_code &ec, result_t &&r){
if(!ec){
auto self = this;
self->redis.pong_count++;
self->redis.rx_buff.consume(r.consumed);
self->consume();
Expand All @@ -103,8 +108,9 @@ struct watcher_t {
void watch() {
timer.expires_after(asio::chrono::seconds(1));
timer.async_wait(
asio::bind_executor(redis.strand, [self = this](const auto &error_code){
if (!error_code) {
asio::bind_executor(redis.strand, [this](const sys::error_code &ec){
if (!ec) {
auto self = this;
std::cout << "pings: " << self->redis.ping_count << ", pongs: " << self->redis.pong_count << "\n";
self->watch();
}
Expand Down
8 changes: 5 additions & 3 deletions examples/speed_test_async_multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ double time_s() {
// alias namespaces
namespace r = bredis;
namespace asio = boost::asio;
namespace sys = boost::system;
using boost::get;

int main(int argc, char **argv) {
Expand All @@ -62,6 +63,7 @@ int main(int argc, char **argv) {
using Iterator = typename r::to_iterator<Buffer>::iterator_t;
//using policy_t = r::parsing_policy::drop_result;
using policy_t = r::parsing_policy::keep_result;
using result_t = r::positive_parse_result_t<Iterator, policy_t>;

if (argc < 2) {
std::cout << "Usage : " << argv[0] << " ip:port \n";
Expand Down Expand Up @@ -111,7 +113,7 @@ int main(int argc, char **argv) {

c.async_read(
rx_buff,
[&](const boost::system::error_code &ec, auto &&r) {
[&](const sys::error_code &ec, result_t &&r) {
assert(!ec);
(void)ec;
rx_buff.consume(r.consumed);
Expand All @@ -124,8 +126,8 @@ int main(int argc, char **argv) {
},
cmds_count, policy_t{});

c.async_write(tx_buff, cmd_wpapper, [&](const boost::system::error_code &ec,
auto bytes_transferred) {
c.async_write(tx_buff, cmd_wpapper, [&](const sys::error_code &ec,
std::size_t bytes_transferred) {
(void)ec;
assert(!ec);
tx_buff.consume(bytes_transferred);
Expand Down
6 changes: 3 additions & 3 deletions include/bredis/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//
#pragma once

#include <boost/core/enable_if.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/variant.hpp>
#include <type_traits>
#include <vector>

#include "Result.hpp"
Expand All @@ -31,14 +31,14 @@ struct single_command_t {
args_container_t arguments;

template <typename... Args,
typename = std::enable_if_t<detail::are_all_constructible<
typename = boost::enable_if_t<detail::are_all_constructible<
boost::string_ref, Args...>::value>>
single_command_t(Args &&... args) : arguments{std::forward<Args>(args)...} {
static_assert(sizeof...(Args) >= 1, "Empty command is not allowed");
}

template <typename InputIterator,
typename = std::enable_if_t<std::is_constructible<
typename = boost::enable_if_t<std::is_constructible<
boost::string_ref, typename std::iterator_traits<
InputIterator>::value_type>::value>>
single_command_t(InputIterator first, InputIterator last)
Expand Down
18 changes: 10 additions & 8 deletions include/bredis/MarkerHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <algorithm>
#include <boost/algorithm/cxx14/equal.hpp>
#include <boost/convert.hpp>
#include <boost/convert/lexical_cast.hpp>
#include <cctype>
Expand Down Expand Up @@ -81,21 +82,22 @@ class equality : public boost::static_visitor<bool> {
}

bool operator()(const markers::string_t<Iterator> &value) const {
auto helper = stringizer<Iterator>();
auto str = helper(value);
return std::equal(begin_, end_, value.from, value.to);
return boost::algorithm::equal(begin_, end_, value.from, value.to);
}

bool operator()(const markers::int_t<Iterator> &value) const {
return std::equal(begin_, end_, value.string.from, value.string.to);
return boost::algorithm::equal(begin_, end_, value.string.from,
value.string.to);
}

bool operator()(const markers::error_t<Iterator> &value) const {
return std::equal(begin_, end_, value.string.from, value.string.to);
return boost::algorithm::equal(begin_, end_, value.string.from,
value.string.to);
}

bool operator()(const markers::nil_t<Iterator> &value) const {
return std::equal(begin_, end_, value.string.from, value.string.to);
return boost::algorithm::equal(begin_, end_, value.string.from,
value.string.to);
}
};

Expand Down Expand Up @@ -172,8 +174,8 @@ class check_subscription : public boost::static_visitor<bool> {
}

const auto &channel_ = cmd_.arguments[idx];
return std::equal(channel_.cbegin(), channel_.cend(), channel->from,
channel->to);
return boost::algorithm::equal(channel_.cbegin(), channel_.cend(),
channel->from, channel->to);
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions include/bredis/impl/connection.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

#include "common.ipp"
#include <algorithm>
#include <boost/type_traits/decay.hpp>
#include <cassert>
#include <ostream>
#include <type_traits>

#include "async_op.ipp"

Expand All @@ -29,7 +29,7 @@ Connection<NextLayer>::async_write(DynamicBuffer &tx_buff,

using boost::asio::async_write;
using Signature = void(boost::system::error_code, std::size_t);
using Callback = std::decay_t<WriteCallback>;
using Callback = boost::decay_t<WriteCallback>;
using AsyncResult = asio::async_result<Callback, Signature>;
using CompletionHandler = typename AsyncResult::completion_handler_type;
using serializer_t = command_serializer_visitor<DynamicBuffer>;
Expand Down Expand Up @@ -58,7 +58,7 @@ Connection<NextLayer>::async_read(DynamicBuffer &rx_buff,
using Iterator = typename to_iterator<DynamicBuffer>::iterator_t;
using ParseResult = BREDIS_PARSE_RESULT(DynamicBuffer, Policy);
using Signature = void(boost::system::error_code, ParseResult);
using Callback = std::decay_t<ReadCallback>;
using Callback = boost::decay_t<ReadCallback>;
using AsyncResult = asio::async_result<Callback, Signature>;
using CompletionHandler = typename AsyncResult::completion_handler_type;
using ReadOp =
Expand Down
6 changes: 4 additions & 2 deletions t/10-ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace r = bredis;
namespace asio = boost::asio;
namespace ep = empty_port;
namespace ts = test_server;
namespace sys = boost::system;

TEST_CASE("ping", "[connection]") {
using socket_t = asio::ip::tcp::socket;
Expand Down Expand Up @@ -48,10 +49,11 @@ TEST_CASE("ping", "[connection]") {
Buffer tx_buff, rx_buff;

c.async_write(
tx_buff, "ping", [&](const auto &error_code, auto bytes_transferred) {
tx_buff, "ping",
[&](const sys::error_code &error_code, std::size_t bytes_transferred) {
REQUIRE(!error_code);
tx_buff.consume(bytes_transferred);
c.async_read(rx_buff, [&](const auto&, auto &&r) {
c.async_read(rx_buff, [&](const sys::error_code &, result_t &&r) {
completion_promise.set_value(r);
rx_buff.consume(r.consumed);
});
Expand Down
33 changes: 17 additions & 16 deletions t/11-multi-ping.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <boost/asio.hpp>
#include <future>
#include <vector>
#include <string>
#include <vector>

#include "EmptyPort.hpp"
#include "SocketWithLogging.hpp"
Expand All @@ -14,6 +14,7 @@ namespace r = bredis;
namespace asio = boost::asio;
namespace ep = empty_port;
namespace ts = test_server;
namespace sys = boost::system;

TEST_CASE("ping", "[connection]") {
using socket_t = asio::ip::tcp::socket;
Expand Down Expand Up @@ -59,23 +60,23 @@ TEST_CASE("ping", "[connection]") {
std::future<result_t> completion_future = completion_promise.get_future();

Buffer tx_buff, rx_buff;
read_callback_t read_callback =
[&](const boost::system::error_code &error_code, ParseResult &&r) {
if (error_code) {
BREDIS_LOG_DEBUG("error: " << error_code.message());
REQUIRE(!error_code);
}
read_callback_t read_callback = [&](const sys::error_code &error_code,
ParseResult &&r) {
if (error_code) {
BREDIS_LOG_DEBUG("error: " << error_code.message());
REQUIRE(!error_code);
auto &replies =
boost::get<r::markers::array_holder_t<Iterator>>(r.result);
BREDIS_LOG_DEBUG("callback, size: " << replies.elements.size());
REQUIRE(replies.elements.size() == count);
completion_promise.set_value();
rx_buff.consume(r.consumed);
};
}
REQUIRE(!error_code);
auto &replies =
boost::get<r::markers::array_holder_t<Iterator>>(r.result);
BREDIS_LOG_DEBUG("callback, size: " << replies.elements.size());
REQUIRE(replies.elements.size() == count);
completion_promise.set_value();
rx_buff.consume(r.consumed);
};

write_callback_t write_callback = [&](
const boost::system::error_code &error_code, auto bytes_transferred) {
write_callback_t write_callback = [&](const sys::error_code &error_code,
std::size_t bytes_transferred) {
(void)bytes_transferred;
BREDIS_LOG_DEBUG("write_callback");
if (error_code) {
Expand Down
16 changes: 9 additions & 7 deletions t/12-basic-types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace r = bredis;
namespace asio = boost::asio;
namespace ep = empty_port;
namespace ts = test_server;
namespace sys = boost::system;

TEST_CASE("ping", "[connection]") {
using socket_t = asio::ip::tcp::socket;
Expand Down Expand Up @@ -64,7 +65,7 @@ TEST_CASE("ping", "[connection]") {
r::single_command_t("time"),
};
std::vector<read_callback_t> callbacks{
[&](const boost::system::error_code&error_code, ParseResult &&r) {
[&](const boost::system::error_code &error_code, ParseResult &&r) {
auto extract = boost::apply_visitor(Extractor(), r.result);
REQUIRE(boost::get<r::extracts::int_t>(extract) == 0);
REQUIRE(order == 0);
Expand Down Expand Up @@ -120,12 +121,13 @@ TEST_CASE("ping", "[connection]") {
c.async_read(rx_buff, generic_callback);
};

c.async_write(tx_buff, r::command_wrapper_t(cmds_container),
[&](const auto &error_code, auto bytes_transferred) {
REQUIRE(!error_code);
tx_buff.consume(bytes_transferred);
c.async_read(rx_buff, generic_callback);
});
c.async_write(
tx_buff, r::command_wrapper_t(cmds_container),
[&](const sys::error_code &ec, std::size_t bytes_transferred) {
REQUIRE(!ec);
tx_buff.consume(bytes_transferred);
c.async_read(rx_buff, generic_callback);
});

while (completion_future.wait_for(sleep_delay) !=
std::future_status::ready) {
Expand Down
Loading

0 comments on commit 15b0202

Please sign in to comment.