Commit bb9f2c0c authored by SR_team's avatar SR_team 💬

Add ABI-compatible for projects with boost and without boost

parent b03b984f
#ifndef SRSignal_H
#define SRSignal_H
#include <algorithm>
#include <chrono>
#include <deque>
#include <list>
#include <functional>
#include <tuple>
#include <algorithm>
#include <list>
#include <mutex>
#include <tuple>
#include <variant>
#ifdef WITH_BOOST_CORO
#include <variant>
#include <chrono>
#include <boost/coroutine2/all.hpp>
# include <boost/coroutine2/all.hpp>
using wait_t = boost::coroutines2::coroutine<size_t>::push_type &;
#endif
......@@ -22,7 +22,9 @@ template<typename... Args> class SRSignal {
boost::coroutines2::coroutine<size_t>::pull_type *coro = nullptr;
std::chrono::system_clock::time_point wait_to;
#else
std::function<void( Args... )> func;
std::variant<std::function<void( Args... )>, std::function<void( void *, Args... )>> func;
void *coro___compability_pad = nullptr;
std::chrono::system_clock::time_point wait_to___compability_pad;
#endif
bool use_coro = false;
};
......@@ -45,7 +47,7 @@ public:
_slots.push_back( slot );
return _slots.size() - 1;
}
template <class C> size_t connect( C *obj, void ( C::*func )( Args... ) ) {
template<class C> size_t connect( C *obj, void ( C::*func )( Args... ) ) {
auto lambda = [obj, func]( Args... args ) { ( obj->*func )( args... ); };
return connect( lambda );
}
......@@ -73,8 +75,7 @@ public:
void disconnect( size_t id ) {
std::lock_guard lock( mtx );
if ( std::find( _empty.begin(), _empty.end(), id ) != _empty.end() )
return;
if ( std::find( _empty.begin(), _empty.end(), id ) != _empty.end() ) return;
if ( id == _slots.size() - 1 ) {
_slots.pop_back();
return;
......@@ -84,17 +85,13 @@ public:
_empty.push_back( id );
}
size_t operator+=( const std::function<void( Args... )> &func ) {
return connect( func );
}
size_t operator+=( const std::function<void( Args... )> &func ) { return connect( func ); }
template<class C> size_t operator+=( const std::tuple<C *, void ( C::* )( Args... )> &method ) {
auto [obj, func] = method;
return connect( obj, func );
}
#ifdef WITH_BOOST_CORO
size_t operator+=( const std::function<void( wait_t, Args... )> &func ) {
return connect( func );
}
size_t operator+=( const std::function<void( wait_t, Args... )> &func ) { return connect( func ); }
template<class C> size_t operator+=( const std::tuple<C *, void ( C::* )( wait_t, Args... )> &method ) {
auto [obj, func] = method;
return connect( obj, func );
......@@ -114,12 +111,12 @@ public:
} else
slot.coro->operator()();
if ( slot.coro->operator bool() )
slot.wait_to = std::chrono::system_clock::now() +
std::chrono::milliseconds( slot.coro->get() );
slot.wait_to =
std::chrono::system_clock::now() + std::chrono::milliseconds( slot.coro->get() );
} else
std::get<std::function<void( Args... )>>( slot.func )( args... );
#else
slot.func( args... );
std::get<std::function<void( Args... )>>( slot.func )( args... );
#endif
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment