From b2a4f32264b9bec4fb2b63cfc4cd0346eea6533c Mon Sep 17 00:00:00 2001 From: Owen Green Date: Fri, 16 Aug 2019 10:14:00 +0100 Subject: [PATCH] SC Wrapper: Handling for heterogenous return types from messages --- include/FluidSCWrapper.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/FluidSCWrapper.hpp b/include/FluidSCWrapper.hpp index e469cee..07b8d49 100644 --- a/include/FluidSCWrapper.hpp +++ b/include/FluidSCWrapper.hpp @@ -512,6 +512,20 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase template static size_t allocSize(FluidTensor s) { return s.size() ; } + template + static std::tuple,size_t> allocSize(std::tuple&& t) + { + return allocSizeImpl(std::forward(t), std::index_sequence_for()); + }; + + template + static std::tuple,size_t> allocSizeImpl(std::tuple&& t,std::index_sequence) + { + size_t size{0}; + std::array res; + (void)std::initializer_list{(res[Is] = size,size += ToFloatArray::allocSize(std::get(t)),0)...}; + return std::make_tuple(res,size); //array of offsets into allocated buffer & total number of floats to alloc + }; static void convert(float* f, typename BufferT::type buf) { f[0] = static_cast(buf.get())->bufnum(); } @@ -539,6 +553,12 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase static_assert(std::is_convertible::value,"Can't convert this to float output"); std::copy(s.begin(), s.end(), f); } + + template + static void convert(float* f,std::tuple&& t, std::array offsets, std::index_sequence) + { + (void)std::initializer_list{(convert(f + offsets[Is],std::get(t)),0)...}; + } }; @@ -645,6 +665,19 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase ft->fSendNodeReply(&x->mParent->mNode, -1, s.c_str(), 0, nullptr); } + template + static void messageOutput(FluidSCWrapper* x, const std::string& s, MessageResult>& result) + { + auto ft = getInterfaceTable(); + std::array offsets; + size_t numArgs; + std::tie(offsets,numArgs) = ToFloatArray::allocSize(static_cast>(result)); + float* values = static_cast(ft->fRTAlloc(x->mWorld,numArgs * sizeof(float))); + ToFloatArray::convert(values,std::tuple(result),offsets,std::index_sequence_for()); + ft->fSendNodeReply(&x->mParent->mNode, -1, s.c_str(), static_cast(numArgs), values); + } + + public: using Client = C;