forked from Maelic/libqi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyfuture.hpp
More file actions
62 lines (49 loc) · 1.41 KB
/
pyfuture.hpp
File metadata and controls
62 lines (49 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
** Copyright (C) 2020 SoftBank Robotics Europe
** See COPYING for the license
*/
#pragma once
#ifndef QIPYTHON_PYFUTURE_HPP
#define QIPYTHON_PYFUTURE_HPP
#include <qipython/common.hpp>
#include <qipython/pyguard.hpp>
#include <qi/future.hpp>
#include <qi/anyvalue.hpp>
namespace qi
{
namespace py
{
using Future = qi::Future<AnyValue>;
using Promise = qi::Promise<AnyValue>;
// Depending on whether we want an asynchronous result or not, returns the
// future or its value.
inline pybind11::object resultObject(const Future& fut, bool async)
{
GILAcquire lock;
if (async)
return castToPyObject(fut);
// Wait for the future outside of the GIL.
auto res = invokeGuarded<GILRelease>(qi::SrcFuture{}, fut);
return castToPyObject(res);
}
inline Future toFuture(qi::Future<void> f)
{
return f.andThen(FutureCallbackType_Sync,
[](void*) { return AnyValue::makeVoid(); });
}
inline Future toFuture(qi::Future<AnyValue> f) { return f; }
inline Future toFuture(qi::Future<AnyReference> f)
{
return f.andThen(FutureCallbackType_Sync,
[](const AnyReference& ref) { return AnyValue(ref); });
}
template<typename T>
inline Future toFuture(qi::Future<T> f)
{
return f.andThen(FutureCallbackType_Sync,
[](const T& val) { return AnyValue::from(val); });
}
void exportFuture(pybind11::module& module);
} // namespace py
} // namespace qi
#endif // QIPYTHON_PYFUTURE_HPP