peerplays-fc/include/fc/signals.hpp

39 lines
1.3 KiB
C++
Raw Normal View History

2014-06-21 20:12:25 +00:00
#pragma once
#include <boost/signals2/signal.hpp>
#include <fc/thread/future.hpp>
#include <fc/thread/thread.hpp>
2012-09-09 04:25:43 +00:00
2012-09-11 03:13:31 +00:00
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>;
#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() ) {
2012-09-09 04:25:43 +00:00
typename promise<T>::ptr p(new promise<T>());
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() ) {
2012-09-09 04:25:43 +00:00
promise<void>::ptr p(new promise<void>());
boost::signals2::scoped_connection c( sig.connect( [=]() { p->set_value(); } ));
2012-09-09 04:25:43 +00:00
p->wait( timeout_us );
}
}