peerplays-fc/include/fc/signals.hpp

50 lines
1.6 KiB
C++
Raw Normal View History

2014-06-21 20:12:25 +00:00
#pragma once
2014-09-07 22:02:39 +00:00
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(push)
#pragma warning(disable:4996)
#endif
#include <boost/signals2/signal.hpp>
2014-09-07 22:02:39 +00:00
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(pop)
#endif
#include <fc/thread/future.hpp>
#include <fc/thread/thread.hpp>
2012-09-09 04:25:43 +00:00
namespace fc {
#if !defined(BOOST_NO_TEMPLATE_ALIASES)
2012-09-09 04:25:43 +00:00
template<typename T>
using signal = boost::signals2::signal<T>;
2014-06-26 15:58:57 +00:00
using scoped_connection = boost::signals2::scoped_connection;
#else
/** Workaround for missing Template Aliases feature in the VS 2012.
\warning Class defined below cannot have defined constructor (even base class has it)
since it is impossible to reference directly template class arguments outside this class.
This code will work until someone will use non-default constructor as it is defined in
boost::signals2::signal.
*/
template <class T>
class signal : public boost::signals2::signal<T>
{
public:
};
#endif
2012-09-09 04:25:43 +00:00
template<typename T>
inline T wait( boost::signals2::signal<void(T)>& sig, const microseconds& timeout_us=microseconds::maximum() ) {
typename promise<T>::ptr p(new promise<T>("fc::signal::wait"));
boost::signals2::scoped_connection c( sig.connect( [=]( T t ) { p->set_value(t); } ));
2012-09-09 04:25:43 +00:00
return p->wait( timeout_us );
}
inline void wait( boost::signals2::signal<void()>& sig, const microseconds& timeout_us=microseconds::maximum() ) {
promise<void>::ptr p(new promise<void>("fc::signal::wait"));
boost::signals2::scoped_connection c( sig.connect( [=]() { p->set_value(); } ));
2012-09-09 04:25:43 +00:00
p->wait( timeout_us );
}
}