From 70c8cfebda8384ffe8662d4242f3f946aa773d53 Mon Sep 17 00:00:00 2001 From: Owen Green Date: Fri, 16 Aug 2019 10:08:48 +0100 Subject: [PATCH] =?UTF-8?q?SC=20Wrapper:=20incoming=20/=20outgoing=20data?= =?UTF-8?q?=20=E2=80=93=C2=A0deal=20more=20liberally=20with=20int=20/=20fp?= =?UTF-8?q?=20types=20(for=20message=20returns);=20deal=20with=20goddam=20?= =?UTF-8?q?strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/FluidSCWrapper.hpp | 74 +++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/include/FluidSCWrapper.hpp b/include/FluidSCWrapper.hpp index 6beebe5..059d39f 100644 --- a/include/FluidSCWrapper.hpp +++ b/include/FluidSCWrapper.hpp @@ -399,18 +399,50 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase static auto fromArgs(World *, sc_msg_iter* args, std::string, int) { - return std::string(args->gets("")); + const char* recv = args->gets(""); + + return std::string(recv?recv:""); } + static auto fromArgs(World *w, FloatControlsIter& args, std::string, int) + { + //first is string size, then chars + int size = static_cast(args.next()); + char* chunk = static_cast(FluidSCWrapper::getInterfaceTable()->fRTAlloc(w,size + 1)); + + if (!chunk) { + std::cout << "ERROR: " << FluidSCWrapper::getName() << ": RT memory allocation failed\n"; + return std::string{""}; + } + + for(int i = 0; i < size; ++i) + chunk[i] = static_cast(args.next()); + + chunk[size] = 0; //terminate string + + return std::string{chunk}; + } + + + template + static std::enable_if_t::value,T> + fromArgs(World *, FloatControlsIter& args, T, int) { return args.next(); } + + template + static std::enable_if_t::value,T> + fromArgs(World *, FloatControlsIter& args, T, int) { return args.next(); } + + template + static std::enable_if_t::value,T> + fromArgs(World *, sc_msg_iter* args, T, int defVal) { return args->geti(defVal); } - static auto fromArgs(World *, FloatControlsIter& args, LongT::type, int) { return args.next(); } - static auto fromArgs(World *, FloatControlsIter& args, FloatT::type, int) { return args.next(); } - static auto fromArgs(World *, sc_msg_iter* args, LongT::type, int defVal) { return args->geti(defVal); } - static auto fromArgs(World *, sc_msg_iter* args, FloatT::type, int) { return args->getf(); } + template + static std::enable_if_t::value,T> + fromArgs(World *, sc_msg_iter* args, T, int) { return args->getf(); } static auto fromArgs(World *w, ArgType args, BufferT::type&, int) { - typename LongT::type bufnum = static_cast(fromArgs(w, args, LongT::type(), -1)); + typename LongT::type bufnum = static_cast(ParamReader::fromArgs(w, args, typename LongT::type(), -1)); return BufferT::type(bufnum >= 0 ? new SCBufferAdaptor(bufnum, w) : nullptr); } @@ -456,11 +488,14 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase struct ToFloatArray { - static size_t allocSize(SCBufferAdaptor*){ return 1; } - static size_t allocSize(double){ return 1; } - static size_t allocSize(float){ return 1; } - static size_t allocSize(intptr_t){ return 1; } - static size_t allocSize(std::string s){ return s.size(); } + static size_t allocSize(typename BufferT::type){ return 1; } + + template + static std::enable_if_t::value||std::is_floating_point::value,size_t> + allocSize(T){ return 1; } + + static size_t allocSize(std::string s){ return s.size() + 1; } //put null char at end when we send + static size_t allocSize(FluidTensor s) { size_t count = 0; @@ -470,11 +505,18 @@ class FluidSCWrapper : public impl::FluidSCWrapperBase template static size_t allocSize(FluidTensor s) { return s.size() ; } - - static void convert(float* f, SCBufferAdaptor* buf) { f[0] = buf->bufnum(); } - static void convert(float* f, double d) { f[0] = static_cast(d); } - static void convert(float* f, intptr_t i) { f[0] = i; } - static void convert(float* f, std::string s) { std::copy(s.begin(), s.end(), f); } + + static void convert(float* f, typename BufferT::type buf) { f[0] = static_cast(buf.get())->bufnum(); } + + template + static std::enable_if_t::value||std::is_floating_point::value> + convert(float* f, T x) { f[0] = static_cast(x); } + + static void convert(float* f, std::string s) + { + std::copy(s.begin(), s.end(), f); + f[s.size()] = 0; //terminate + } static void convert(float* f, FluidTensor s) { for(auto& str: s)