From 873b1feb68d145920f8c3bca91b5eabc3d346078 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 26 Apr 2020 09:10:53 +0200 Subject: [PATCH] Use lambda instead of boost::bind --- .../gnuradio_blocks/CMakeLists.txt | 15 +++++++ .../gr_complex_ip_packet_source.cc | 11 ++++- .../rtl_tcp_signal_source_c.cc | 45 ++++++++++++++----- .../acquisition/acq_performance_test.cc | 2 +- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/algorithms/signal_source/gnuradio_blocks/CMakeLists.txt b/src/algorithms/signal_source/gnuradio_blocks/CMakeLists.txt index fd88c888a..3a4b47d50 100644 --- a/src/algorithms/signal_source/gnuradio_blocks/CMakeLists.txt +++ b/src/algorithms/signal_source/gnuradio_blocks/CMakeLists.txt @@ -103,6 +103,21 @@ if(Boost_VERSION_STRING VERSION_GREATER 1.65.99) ) endif() +if(CMAKE_VERSION VERSION_GREATER 3.1) + set(has_generic_lambdas HAS_GENERIC_LAMBDA=1) + set(no_has_generic_lambdas HAS_GENERIC_LAMBDA=0) + target_compile_definitions(signal_source_gr_blocks + PRIVATE + "$<$:${has_generic_lambdas}>" + "$<$>:${no_has_generic_lambdas}>" + ) +else() + target_compile_definitions(signal_source_gr_blocks + PRIVATE + -DHAS_GENERIC_LAMBDA=0 + ) +endif() + if(ENABLE_CLANG_TIDY) if(CLANG_TIDY_EXE) set_target_properties(signal_source_gr_blocks diff --git a/src/algorithms/signal_source/gnuradio_blocks/gr_complex_ip_packet_source.cc b/src/algorithms/signal_source/gnuradio_blocks/gr_complex_ip_packet_source.cc index 70a35403b..b37350d15 100644 --- a/src/algorithms/signal_source/gnuradio_blocks/gr_complex_ip_packet_source.cc +++ b/src/algorithms/signal_source/gnuradio_blocks/gr_complex_ip_packet_source.cc @@ -24,6 +24,10 @@ #include #include #include +#if HAS_GENERIC_LAMBDA +#else +#include +#endif const int FIFO_SIZE = 1472000; @@ -148,7 +152,12 @@ bool Gr_Complex_Ip_Packet_Source::start() if (open() == true) { // start pcap capture thread - d_pcap_thread = new boost::thread(boost::bind(&Gr_Complex_Ip_Packet_Source::my_pcap_loop_thread, this, descr)); + d_pcap_thread = new boost::thread( +#if HAS_GENERIC_LAMBDA + [this] { my_pcap_loop_thread(descr); }); +#else + boost::bind(&Gr_Complex_Ip_Packet_Source::my_pcap_loop_thread, this, descr)); +#endif return true; } return false; diff --git a/src/algorithms/signal_source/gnuradio_blocks/rtl_tcp_signal_source_c.cc b/src/algorithms/signal_source/gnuradio_blocks/rtl_tcp_signal_source_c.cc index 51e84eebb..561433145 100644 --- a/src/algorithms/signal_source/gnuradio_blocks/rtl_tcp_signal_source_c.cc +++ b/src/algorithms/signal_source/gnuradio_blocks/rtl_tcp_signal_source_c.cc @@ -24,6 +24,10 @@ #include #include #include +#if HAS_GENERIC_LAMBDA +#else +#include +#endif namespace ip = boost::asio::ip; @@ -134,9 +138,18 @@ rtl_tcp_signal_source_c::rtl_tcp_signal_source_c(const std::string &address, // 6. Start reading boost::asio::async_read(socket_, boost::asio::buffer(data_), +#if HAS_GENERIC_LAMBDA + [this](auto &&PH1, auto &&PH2) { handle_read(PH1, PH2); }); +#else boost::bind(&rtl_tcp_signal_source_c::handle_read, this, _1, _2)); - boost::thread(boost::bind(&b_io_context::run, &io_context_)); +#endif + boost::thread( +#if HAS_GENERIC_LAMBDA + [ObjectPtr = &io_context_] { ObjectPtr->run(); }); +#else + boost::bind(&b_io_context::run, &io_context_)); +#endif } @@ -288,8 +301,11 @@ void rtl_tcp_signal_source_c::handle_read(const boost::system::error_code &ec, // Unpack read data boost::mutex::scoped_lock lock(mutex_); not_full_.wait(lock, - boost::bind(&rtl_tcp_signal_source_c::not_full, - this)); +#if HAS_GENERIC_LAMBDA + [this] { not_full(); }); +#else + boost::bind(&rtl_tcp_signal_source_c::not_full, this)); +#endif for (size_t i = 0; i < bytes_transferred; i++) { @@ -299,8 +315,11 @@ void rtl_tcp_signal_source_c::handle_read(const boost::system::error_code &ec, // wait until there's space for more not_empty_.notify_one(); // needed? not_full_.wait(lock, - boost::bind(&rtl_tcp_signal_source_c::not_full, - this)); +#if HAS_GENERIC_LAMBDA + [this] { not_full(); }); +#else + boost::bind(&rtl_tcp_signal_source_c::not_full, this)); +#endif } buffer_.push_front(lookup_[data_[i]]); @@ -312,8 +331,11 @@ void rtl_tcp_signal_source_c::handle_read(const boost::system::error_code &ec, // Read some more boost::asio::async_read(socket_, boost::asio::buffer(data_), - boost::bind(&rtl_tcp_signal_source_c::handle_read, - this, _1, _2)); +#if HAS_GENERIC_LAMBDA + [this](auto &&PH1, auto &&PH2) { handle_read(PH1, PH2); }); +#else + boost::bind(&rtl_tcp_signal_source_c::handle_read, this, _1, _2)); +#endif } } @@ -331,9 +353,12 @@ int rtl_tcp_signal_source_c::work(int noutput_items, { boost::mutex::scoped_lock lock(mutex_); - not_empty_.wait(lock, boost::bind(&rtl_tcp_signal_source_c::not_empty, - this)); - + not_empty_.wait(lock, +#if HAS_GENERIC_LAMBDA + [this] { not_empty(); }); +#else + boost::bind(&rtl_tcp_signal_source_c::not_empty, this)); +#endif for (; i < noutput_items && unread_ > 1; i++) { float re = buffer_[--unread_]; diff --git a/src/tests/unit-tests/signal-processing-blocks/acquisition/acq_performance_test.cc b/src/tests/unit-tests/signal-processing-blocks/acquisition/acq_performance_test.cc index 24ccc6468..f239e1742 100644 --- a/src/tests/unit-tests/signal-processing-blocks/acquisition/acq_performance_test.cc +++ b/src/tests/unit-tests/signal-processing-blocks/acquisition/acq_performance_test.cc @@ -158,7 +158,7 @@ AcqPerfTest_msg_rx::AcqPerfTest_msg_rx(Concurrent_Queue& queue) : gr::block { this->message_port_register_in(pmt::mp("events")); this->set_msg_handler(pmt::mp("events"), -#if HAS_GENNERIC_LAMBDA +#if HAS_GENERIC_LAMBDA [this](auto&& PH1) { msg_handler_events(PH1); }); #else boost::bind(&AcqPerfTest_msg_rx::msg_handler_events, this, _1));