LMMS
Loading...
Searching...
No Matches
typed-message.h
Go to the documentation of this file.
1#ifndef RTOSC_TYPED_MESSAGE_H
2#define RTOSC_TYPED_MESSAGE_H
3#include <rtosc/typestring.hh>
4#include <rtosc/rtosc.h>
5#include <type_traits>
6#include <stdexcept>
7
8namespace rtosc
9{
10struct match_exact{};
12
13template<class... Types> class rtMsg;
14
15// empty tuple
16template<> class rtMsg<>
17{
18 public:
19 rtMsg(const char *arg = NULL, const char *spec=NULL, bool _=false)
20 :msg(arg)
21 {
22 if(arg && spec && !rtosc_match_path(spec, arg, NULL))
23 msg = NULL;
24 (void)_;
25 }
26
27 operator bool(void){return this->msg;}
28
29 const char *msg;
30};
31
32template<class T>
34{
35 static std::true_type size;
36};
37
38template<char ... C>
39struct advance_size<irqus::typestring<C...>>
40{
41 static std::false_type size;
42};
43
44template<class T>
45bool valid_char(char) { return false;}
46
47template<>
48bool valid_char<const char*>(char c) { return c=='s' || c=='S'; };
49
50template<>
51bool valid_char<int32_t>(char c) { return c=='i'; };
52
53template<>
54bool valid_char<float>(char c) { return c=='f'; };
55
56template<int i>
57bool validate(const char *arg)
58{
59 return rtosc_narguments(arg) == i;
60}
61
62template<class T>
63bool match_path(std::false_type, const char *arg)
64{
65 return rtosc_match_path(T::data(), arg, NULL);
66}
67
68template<class T>
69bool match_path(std::true_type, const char *)
70{
71 return true;
72}
73
74template<int i, class This, class... Rest>
75bool validate(const char *arg)
76{
79 return false;
80 else if(!size && !match_path<This>(size, arg))
81 return false;
82 else
84}
85
86//Tuple Like Template Class Definition
87template<class This, class... Rest>
88class rtMsg<This, Rest...>:public rtMsg<Rest...>
89{
90 public:
91 typedef This This_;
92 typedef rtMsg<Rest...> T;
93 rtMsg(const char *arg = NULL, const char *spec=NULL)
94 :T(arg, spec, false)
95 {
96 if(this->msg && !validate<0,This,Rest...>(this->msg))
97 this->msg = NULL;
98 }
99
100 rtMsg(const char *arg, const char *spec, bool)
101 :T(arg, spec, false)
102 {}
103
104};
105
106
107// tuple_element
108template<size_t Index, class Tuple> struct osc_element;
109
110// select first element
111template<class This, class... Rest>
112struct osc_element<0, rtMsg<This, Rest...>>
113{
114 typedef This type;
115};
116
117// recursive tuple_element definition
118template <size_t Index, class This, class... Rest>
119struct osc_element<Index, rtMsg<This, Rest...>>
120: public osc_element<Index - 1, rtMsg<Rest...>>
121{
122};
123
124template<class T>
125T rt_get_impl(const char *msg, size_t i);
126
127template<>
128const char *rt_get_impl(const char *msg, size_t i)
129{
130 return rtosc_argument(msg,i).s;
131}
132
133template<>
134int32_t rt_get_impl(const char *msg, size_t i)
135{
136 return rtosc_argument(msg,i).i;
137}
138
139// get reference to _Index element of tuple
140template<size_t Index, class... Types> inline
141 typename osc_element<Index, rtMsg<Types...>>::type
143{
144 if(!Tuple.msg)
145 throw std::invalid_argument("Message Does Not Match Spec");
146 typedef typename std::remove_reference<typename osc_element<Index, rtMsg<Types...>>::type>::type T;
147 return rt_get_impl<T>(Tuple.msg, Index);
148}
149
150template<class... Types> inline
151 typename osc_element<0, rtMsg<Types...>>::type
153{
154 return get<0>(Tuple);
155}
156
157template<class... Types> inline
158 typename osc_element<1, rtMsg<Types...>>::type
160{
161 return get<1>(Tuple);
162}
163
164};
165#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
float arg(const fft_t *freqs, off_t x)
Definition OscilGen.cpp:58
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
rtMsg(const char *arg=NULL, const char *spec=NULL)
Definition typed-message.h:93
This This_
Definition typed-message.h:91
rtMsg< Rest... > T
Definition typed-message.h:92
rtMsg(const char *arg, const char *spec, bool)
Definition typed-message.h:100
rtMsg(const char *arg=NULL, const char *spec=NULL, bool _=false)
Definition typed-message.h:19
const char * msg
Definition typed-message.h:29
Definition typed-message.h:13
register unsigned i
Definition inflate.c:1575
const char * rtosc_match_path(const char *pattern, const char *msg, const char **path_end)
Definition dispatch.c:70
#define _(msgid)
Definition getopt.c:86
int int32_t
Definition mid.cpp:97
const char * msg
Definition missing_descriptor.c:20
Definition globals.h:37
bool valid_char< int32_t >(char c)
Definition typed-message.h:51
bool valid_char< float >(char c)
Definition typed-message.h:54
bool validate(const char *arg)
Definition typed-message.h:57
osc_element< Index, rtMsg< Types... > >::type get(rtMsg< Types... > &Tuple)
Definition typed-message.h:142
bool valid_char(char)
Definition typed-message.h:45
osc_element< 1, rtMsg< Types... > >::type second(rtMsg< Types... > &Tuple)
Definition typed-message.h:159
osc_element< 0, rtMsg< Types... > >::type first(rtMsg< Types... > &Tuple)
Definition typed-message.h:152
bool valid_char< const char * >(char c)
Definition typed-message.h:48
bool match_path(std::false_type, const char *arg)
Definition typed-message.h:63
T rt_get_impl(const char *msg, size_t i)
#define false
Definition ordinals.h:83
unsigned rtosc_narguments(const char *msg)
Definition rtosc.c:19
char rtosc_type(const char *msg, unsigned nargument)
Definition rtosc.c:67
rtosc_arg_t rtosc_argument(const char *msg, unsigned idx)
Definition rtosc.c:732
static std::false_type size
Definition typed-message.h:41
Definition typed-message.h:34
static std::true_type size
Definition typed-message.h:35
Definition typed-message.h:10
Definition typed-message.h:11
This type
Definition typed-message.h:114
Definition typed-message.h:108
int32_t i
Definition rtosc.h:47
const char * s
Definition rtosc.h:54
return c
Definition crypt.c:175
ulg size
Definition extract.c:2350
#define void
Definition unzip.h:396