Sygaldry
Loading...
Searching...
No Matches
sygbp-osc_string_constants.hpp
1#pragma once
2/*
3Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music Interaction Laboratory
4(IDMIL), Centre for Interdisciplinary Research in Music Media and Technology
5(CIRMMT), McGill University, Montréal, Canada, and Univ. Lille, Inria, CNRS,
6Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
7
8SPDX-License-Identifier: MIT
9*/
10
11#include <array>
12#include <concepts>
13#include "sygac-tuple.hpp"
14#include "sygac-components.hpp"
15#include "sygac-endpoints.hpp"
16#include "sygbp-spelling.hpp"
17
18namespace sygaldry { namespace sygbp {
23
24template<typename> struct osc_path_length : std::integral_constant<std::size_t, 0> {};
25template<template<typename...>typename L, typename ... Path>
26struct osc_path_length<L<Path...>>
27: std::integral_constant<std::size_t, (name_length<std::decay_t<Path>>() + ...) + sizeof...(Path)> {};
28template<typename T>
29constexpr std::size_t osc_type_string_length()
30{
31 if constexpr (array_like<value_t<T>>)
32 {
33 constexpr auto length = size<value_t<T>>() + 1;
34 constexpr auto remainder = length % 4;
35 constexpr auto padding = 4 - remainder;
36 return length + padding;
37 }
38 else return 4;
39}
40
41template<typename> struct osc_path;
42template<template<typename...>typename L, typename ... Path>
43struct osc_path<L<Path...>>
44{
45 static constexpr size_t N = osc_path_length<L<std::decay_t<Path>...>>() + 1; // + 1 for null terminator
46 static constexpr std::array<char, N> value = []()
47 {
48 L<std::decay_t<Path>...> path;
49 std::array<char, N> ret;
50 std::size_t write_pos = 0;
51 auto copy_one = [&]<typename T>(T)
52 {
53 ret[write_pos++] = '/';
54 for (std::size_t i = 0; i < name_length<T>(); ++i)
55 {
56 ret[write_pos++] = snake_case_v<T>[i];
57 }
58 };
59 tpl::apply([&]<typename ... Ts>(Ts... ts)
60 {
61 (copy_one(ts), ...);
62 }, path);
63 ret[write_pos] = 0;
64 return ret;
65 }();
66};
67
68template<typename T, typename Components>
69constexpr const char * osc_path_v = osc_path<path_t<T,Components>>::value.data();
70
71template<typename T>
73{
74 static constexpr size_t N = osc_type_string_length<T>();
75 static constexpr std::array<char, N> value = []()
76 {
77 std::array<char, N> ret{0};
78 ret[0] = ',';
79
80 if constexpr (Bang<T>) return ret;
81
82 char tag;
83 if constexpr (std::integral<element_t<T>>) tag = 'i';
84 else if constexpr (std::floating_point<element_t<T>>) tag = 'f';
85 else if constexpr (string_like<element_t<T>>) tag = 's';
86 else return ret; // this should never happen
87
88 if constexpr (array_like<value_t<T>>)
89 for (std::size_t i = 0; i < size<value_t<T>>(); ++i)
90 ret[1 + i] = tag;
91 else ret[1] = tag;
92 return ret;
93 }();
94};
95
96template<typename T> constexpr const char * osc_type_string_v = osc_type_string<T>::value.data();
97
98//template<typename T, typename Components>
99//constexpr bool osc_match_pattern(const char * p)
100//{
101// return osc_match_pattern(p, osc_path_v<T, Components>);
102//}
103
106} }
Definition sygac-endpoints.hpp:92
Definition sygac-endpoints.hpp:166
Definition sygac-endpoints.hpp:161
#define tag(TAG)
Helper struct for defining recognized tags. This is immediately undefed, so don't try to use it!
Definition sygah-endpoints.hpp:238
Definition sygbp-osc_string_constants.hpp:24
Definition sygbp-osc_string_constants.hpp:41
Definition sygbp-osc_string_constants.hpp:73