diff --git a/examples/06_custom/client.cpp b/examples/06_custom/client.cpp index ad155cd..fbe47d4 100644 --- a/examples/06_custom/client.cpp +++ b/examples/06_custom/client.cpp @@ -14,7 +14,9 @@ int main(int argc, char** argv) { tl::endpoint server = myEngine.lookup(argv[1]); point p(1,2,3); point q(5,2,4); - double ret = dot_product.on(server).async(p,q).wait(); + double ret; + dot_product.on(server).async(p,q).wait().unpack(ret); + // or double ret = dot_product.on(server).async(p,q).wait() std::cout << "Dot product : " << ret << std::endl; return 0; diff --git a/include/thallium/packed_data.hpp b/include/thallium/packed_data.hpp index 0aea32f..5117f79 100644 --- a/include/thallium/packed_data.hpp +++ b/include/thallium/packed_data.hpp @@ -194,6 +194,32 @@ class packed_data { * @return An object of the desired type. */ template operator T() const { return as(); } + + /** + * @brief Unpack the data into a provided set of arguments. + * This function is preferable to as or to casting into + * the resulting type in cases where the resulting type + * cannot be easily moved, or the caller already has an instance + * of it that needs to be set. + * + * @tparam T Types into which to unpack. + * @param x Objects into which to unpack. + */ + template void unpack(T&... x) const { + if(m_handle == HG_HANDLE_NULL) { + throw exception( + "Cannot unpack data from handle. Are you trying to " + "unpack data from an RPC that does not return any?"); + } + auto t = std::make_tuple(std::ref(x)...); + meta_proc_fn mproc = [this, &t](hg_proc_t proc) { + return proc_object_decode(proc, t, m_engine_impl, m_context); + }; + hg_return_t ret = m_unpack_fn(m_handle, &mproc); + MARGO_ASSERT(ret, m_unpack_fn); + ret = m_free_fn(m_handle, &mproc); + MARGO_ASSERT(ret, m_free_fn); + } }; } // namespace thallium