This commit is contained in:
Jean Pierre Cimalando 2020-11-09 14:43:25 +01:00
parent 9b044a4eb9
commit 425889c949
3 changed files with 57 additions and 24 deletions

View file

@ -6,13 +6,12 @@
#pragma once #pragma once
#include "sfizz_message.h" #include "sfizz_message.h"
#include <type_traits>
namespace sfz { namespace sfz {
template <char Tag> struct OscDataTraits; template <char Tag> struct OscDataTraits;
template <char Tag> using OscType = typename OscDataTraits<Tag>::type; template <char Tag> using OscType = typename OscDataTraits<Tag>::type;
template <char Tag> using OscDecayedType = typename std::decay<OscType<Tag>>::type; template <char Tag> using OscDecayedType = typename OscDataTraits<Tag>::decayed_type;
class Client { class Client {
public: public:

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "Messaging.h" #include "Messaging.h"
#include <type_traits>
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
@ -30,30 +31,33 @@ inline void Client::receive(int delay, const char* path, OscDecayedType<Sig>...
} }
/// ///
#define OSC_SCALAR_TRAITS(tag, member) \ #define OSC_SCALAR_TRAITS(tag, member) \
template <> struct OscDataTraits<tag> { \ template <> struct OscDataTraits<tag> { \
typedef decltype(sfizz_arg_t::member) type; \ typedef decltype(sfizz_arg_t::member) type; \
static_assert(std::is_scalar<type>::value, ""); \ typedef type decayed_type; \
static inline sfizz_arg_t make_arg(type v) { \ static_assert(std::is_scalar<type>::value, ""); \
sfizz_arg_t a; a.member = v; return a; \ static inline sfizz_arg_t make_arg(decayed_type v) { \
} \ sfizz_arg_t a; a.member = v; return a; \
} \
} }
#define OSC_BYTEARRAY_TRAITS(tag, member) \ #define OSC_BYTEARRAY_TRAITS(tag, member) \
template <> struct OscDataTraits<tag> { \ template <> struct OscDataTraits<tag> { \
typedef decltype(sfizz_arg_t::member) type; \ typedef decltype(sfizz_arg_t::member) type; \
static_assert(std::is_array<type>::value, ""); \ static_assert(std::is_array<type>::value, ""); \
static inline sfizz_arg_t make_arg(type v) { \ typedef const typename std::remove_all_extents<type>::type* decayed_type; \
sfizz_arg_t a; \ static inline sfizz_arg_t make_arg(decayed_type v) { \
std::memcpy(a.member, v, sizeof(a.member)); \ sfizz_arg_t a; \
return a; \ std::memcpy(a.member, v, sizeof(a.member)); \
} \ return a; \
} \
} }
#define OSC_VOID_TRAITS(tag) \ #define OSC_VOID_TRAITS(tag) \
template <> struct OscDataTraits<tag> { \ template <> struct OscDataTraits<tag> { \
typedef struct Nothing {} type; \ typedef struct Nothing {} type; \
static inline sfizz_arg_t make_arg(type v) { \ typedef type decayed_type; \
sfizz_arg_t a; (void)v; return a; \ static inline sfizz_arg_t make_arg(decayed_type v) { \
} \ sfizz_arg_t a; (void)v; return a; \
} \
} }
OSC_SCALAR_TRAITS('i', i); OSC_SCALAR_TRAITS('i', i);

View file

@ -93,3 +93,33 @@ TEST_CASE("[Messaging] OSC message creation")
REQUIRE(args2[4].f == 5.678f); REQUIRE(args2[4].f == 5.678f);
} }
} }
TEST_CASE("[Messaging] Type-safe client API")
{
sfz::Client client(nullptr);
static const int32_t i = 777;
static const int64_t h = 0x100000000LL;
static const float f = 3.14f;
static const double d = 6.28;
static const uint8_t m[4] = {0x90, 0x40, 0xFF};
static const sfizz_blob_t b { reinterpret_cast<const uint8_t*>("MyBinaryString"), 14 };
static const char s[] = "Hello, World!";
client.setReceiveCallback(+[](void*, int, const char* path, const char* sig, const sfizz_arg_t* args) {
REQUIRE(!strcmp(path, "/test"));
REQUIRE(!strcmp(sig, "imhfdsbTFNI"));
unsigned index = 0;
REQUIRE(args[index++].i == i);
REQUIRE(!memcmp(args[index++].m, m, 4));
REQUIRE(args[index++].h == h);
REQUIRE(args[index++].f == f);
REQUIRE(args[index++].d == d);
REQUIRE(!strcmp(args[index++].s, s));
REQUIRE(args[index ].b->data == b.data);
REQUIRE(args[index++].b->size == b.size);
});
client.receive<'i', 'm', 'h', 'f', 'd', 's', 'b', 'T', 'F', 'N', 'I'>(
0, "/test", i, m, h, f, d, s, &b, {}, {}, {}, {});
}