7 #ifndef QI_TYPE_DETAIL_FUTURE_ADAPTER_HXX_
8 #define QI_TYPE_DETAIL_FUTURE_ADAPTER_HXX_
10 #include <boost/scope_exit.hpp>
23 qiLogDebug(
"qi.adapter") <<
"converting value";
29 catch(
const std::exception& e)
31 qiLogError(
"qi.adapter") <<
"future to promise forwarding error: " << e.what();
48 boost::shared_ptr<GenericObject>& ao)
51 qiLogDebug(
"qi.adapter") <<
"futureAdapter trigger";
62 BOOST_SCOPE_EXIT_TPL(&ao, &val) {
65 } BOOST_SCOPE_EXIT_END
66 if (gfut.
call<
bool>(
"hasError", 0))
68 qiLogDebug(
"qi.adapter") <<
"futureAdapter: future in error";
69 std::string s = gfut.
call<std::string>(
"error", 0);
70 qiLogDebug(
"qi.adapter") <<
"futureAdapter: got error: " << s;
74 if (gfut.
call<
bool>(
"isCanceled"))
76 qiLogDebug(
"qi.adapter") <<
"futureAdapter: future canceled";
80 qiLogDebug(
"qi.adapter") <<
"futureAdapter: future has value";
101 *kind = ft1->templateArgument()->kind();
107 *kind = ft2->templateArgument()->kind();
111 return boost::shared_ptr<GenericObject>();
112 return boost::make_shared<GenericObject>(onext, val.
rawValue());
117 template <
typename T>
124 boost::function<void()> cb =
125 boost::bind(futureAdapterGeneric<T>, val, promise, ao);
131 ao->call<
void>(
"_connect", cb);
134 boost::function<
void()>(),
136 &GenericObject::call<void>),
137 boost::weak_ptr<GenericObject>(ao),
140 catch (std::exception& e)
142 qiLogError(
"qi.object") <<
"future connect error " << e.what();
157 template <
typename T>
172 QI_ONCE(targetType = typeOf<T>());
175 std::pair<AnyReference, bool> conv = val.
convert(targetType);
176 if (!conv.first.type())
177 throw std::runtime_error(std::string(
"Unable to convert call result to target type: from ")
184 return *conv.first.ptr<T>(
false);
187 return *conv.first.ptr<T>(
false);
190 catch(
const std::exception& e)
192 throw std::runtime_error(std::string(
"Return argument conversion error: ") + e.what());
203 template <
typename T>
206 qiLogDebug(
"qi.object") <<
"futureAdapter " << qi::typeOf<T>()->infoString()<<
' ' << metaFut.
hasError();
222 QI_ONCE(targetType = typeOf<T>());
225 std::pair<AnyReference, bool> conv = val.
convert(targetType);
226 if (!conv.first.type())
227 promise.
setError(std::string(
"Unable to convert call result to target type: from ")
231 promise.
setValue(*conv.first.ptr<T>(
false));
234 conv.first.destroy();
236 catch(
const std::exception& e)
238 promise.
setError(std::string(
"Return argument conversion error: ") + e.what());
246 qiLogDebug(
"qi.object") <<
"futureAdapter void " << metaFut.hasError();
248 if (metaFut.hasError()) {
249 promise.setError(metaFut.error());
252 if (metaFut.isCanceled()) {
253 promise.setCanceled();
264 template <
typename T>
281 catch (
const std::exception& e)
283 promise.
setError(std::string(
"Return argument conversion error: ") + e.what());
std::pair< AnyReference, bool > convert(TypeInterface *targetType) const
void futureAdapter< void >(qi::Future< qi::AnyReference > metaFut, qi::Promise< void > promise)
std::string toPrettySignature() const
AutoRefDestroy(AnyReference ref)
void destroy()
Stop and flush the logging system.
void extractFuture< void >(qi::Future< qi::AnyReference > metaFut)
boost::function< RF > bind(const AF &fun,...)
void setError(const std::string &msg)
void futureAdapterGeneric(AnyReference val, qi::Promise< T > promise, boost::shared_ptr< GenericObject > &ao)
void setPromise(qi::Promise< T > &promise, AnyValue &v)
void futureAdapter(Future< FT > f, Promise< PT > p, CONV converter)
void destroy()
Deletes storage.
#define qiLogError(...)
Log in error mode.
T to() const
Convert to anything or throw trying.
qi::Signature signature(bool resolveDynamic=false) const
TypeInterface * type() const
void setOnCancel(boost::function< void(qi::Promise< T >)> cancelCallback)
#define QI_TEMPLATE_TYPE_GET(typeInst, templateName)
#define QI_ONCE(code)
Execute code once, parallel calls are blocked until code finishes.
boost::shared_ptr< GenericObject > getGenericFuture(AnyReference val, TypeKind *kind=0)
AnyReference asReference() const
const char * infoString()
void futureAdapterVal(qi::Future< qi::AnyValue > metaFut, qi::Promise< T > promise)
R call(const std::string &eventName, qi::AutoAnyReference p1=qi::AutoAnyReference(), qi::AutoAnyReference p2=qi::AutoAnyReference(), qi::AutoAnyReference p3=qi::AutoAnyReference(), qi::AutoAnyReference p4=qi::AutoAnyReference(), qi::AutoAnyReference p5=qi::AutoAnyReference(), qi::AutoAnyReference p6=qi::AutoAnyReference(), qi::AutoAnyReference p7=qi::AutoAnyReference(), qi::AutoAnyReference p8=qi::AutoAnyReference())
T extractFuture(qi::Future< qi::AnyReference > metaFut)
const std::string & error(int msecs=FutureTimeout_Infinite) const
const ValueType & value(int msecs=FutureTimeout_Infinite) const
Return the value associated to a Future.
void setValue(const ValueType &value)
bool handleFuture(AnyReference val, Promise< T > promise)
qi::Signature signature(void *storage=0, bool resolveDynamic=false)
bool hasError(int msecs=FutureTimeout_Infinite) const