Real Time Open Sound Control librtosc
Loading...
Searching...
No Matches
typed-message.h
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>
33struct advance_size : public std::true_type {};
34
35template<char ... C>
36struct advance_size<irqus::typestring<C...>> : public std::false_type {};
37
38template<class T>
39bool valid_char(char) { return false;}
40
41template<>
42bool valid_char<const char*>(char c) { return c=='s' || c=='S'; };
43
44template<>
45bool valid_char<int32_t>(char c) { return c=='i'; };
46
47template<>
48bool valid_char<float>(char c) { return c=='f'; };
49
50template<int i>
51bool validate(const char *arg)
52{
53 return rtosc_narguments(arg) == i;
54}
55
56template<class T>
57bool match_path(std::false_type, const char *arg)
58{
59 return rtosc_match_path(T::data(), arg, NULL);
60}
61
62template<class T>
63bool match_path(std::true_type, const char *)
64{
65 return true;
66}
67
68template<int i, class This, class... Rest>
69bool validate(const char *arg)
70{
71 advance_size<This> size;
72 if(size && !valid_char<This>(rtosc_type(arg,i)))
73 return false;
74 else if(!size && !match_path<This>(size, arg))
75 return false;
76 else
77 return validate<i+size.value,Rest...>(arg);
78}
79
80//Tuple Like Template Class Definition
81template<class This, class... Rest>
82class rtMsg<This, Rest...>:public rtMsg<Rest...>
83{
84 public:
85 typedef This This_;
86 typedef rtMsg<Rest...> T;
87 rtMsg(const char *arg = NULL, const char *spec=NULL)
88 :T(arg, spec, false)
89 {
90 if(this->msg && !validate<0,This,Rest...>(this->msg))
91 this->msg = NULL;
92 }
93
94 rtMsg(const char *arg, const char *spec, bool)
95 :T(arg, spec, false)
96 {}
97
98};
99
100
101// tuple_element
102template<size_t Index, class Tuple> struct osc_element;
103
104// select first element
105template<class This, class... Rest>
106struct osc_element<0, rtMsg<This, Rest...>>
107{
108 typedef This type;
109};
110
111// recursive tuple_element definition
112template <size_t Index, class This, class... Rest>
113struct osc_element<Index, rtMsg<This, Rest...>>
114: public osc_element<Index - 1, rtMsg<Rest...>>
115{
116};
117
118template<class T>
119T rt_get_impl(const char *msg, size_t i);
120
121template<>
122const char *rt_get_impl(const char *msg, size_t i)
123{
124 return rtosc_argument(msg,i).s;
125}
126
127template<>
128int32_t rt_get_impl(const char *msg, size_t i)
129{
130 return rtosc_argument(msg,i).i;
131}
132
133// get reference to _Index element of tuple
134template<size_t Index, class... Types> inline
135 typename osc_element<Index, rtMsg<Types...>>::type
136get(rtMsg<Types...>& Tuple)
137{
138 if(!Tuple.msg)
139 throw std::invalid_argument("Message Does Not Match Spec");
140 typedef typename std::remove_reference<typename osc_element<Index, rtMsg<Types...>>::type>::type T;
141 return rt_get_impl<T>(Tuple.msg, Index);
142}
143
144template<class... Types> inline
145 typename osc_element<0, rtMsg<Types...>>::type
146first(rtMsg<Types...>&Tuple)
147{
148 return get<0>(Tuple);
149}
150
151template<class... Types> inline
152 typename osc_element<1, rtMsg<Types...>>::type
153second(rtMsg<Types...>&Tuple)
154{
155 return get<1>(Tuple);
156}
157
158};
159#endif
Definition: typed-message.h:13
Functions handling messages and arguments.
unsigned rtosc_narguments(const char *msg)
Returns the number of arguments found in a given message.
rtosc_arg_t rtosc_argument(const char *msg, unsigned i)
Blob data may be safely written to.
char rtosc_type(const char *msg, unsigned i)
const char * rtosc_match_path(const char *pattern, const char *msg, const char **path_end)
Attempt to match a rtosc style path while ignoring arguments.
Definition: typed-message.h:33
Definition: typed-message.h:10
Definition: typed-message.h:11
Definition: typed-message.h:102