Real Time Open Sound Control librtosc: include/rtosc/ports.h Source File

Real Time Open Sound Control librtosc

ports.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2012 Mark McCurry
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
31 #ifndef RTOSC_PORTS
32 #define RTOSC_PORTS
33
34 #include <vector>
35 #include <functional>
36 #include <initializer_list>
37 #include <rtosc/rtosc.h>
38 #include <string>
39 #include <cstdio>
40 #include <iosfwd>
41
42 namespace rtosc {
43
44 //First define all types
45 typedef const char *msg_t;
46
47 struct Port;
48 struct Ports;
49
51 struct RtData
52 {
53 RtData(void);
54
60 char *loc;
61 size_t loc_size;
62 void *obj;
63 int matches;
64 const Port *port;
65 const char *message;
68
69 int idx[16];
70 void push_index(int ind);
71 void pop_index(void);
72
73 virtual void replyArray(const char *path, const char *args,
74 rtosc_arg_t *vals);
75 virtual void reply(const char *path, const char *args, ...);
77 virtual void reply(const char *msg);
78 virtual void chain(const char *path, const char *args, ...);
80 virtual void chain(const char *msg);
81 virtual void chainArray(const char *path, const char *args,
82 rtosc_arg_t *vals);
84 virtual void broadcast(const char *path, const char *args, ...);
85 virtual void broadcast(const char *msg);
86 virtual void broadcastArray(const char *path, const char *args,
87 rtosc_arg_t *vals);
88
89 virtual void forward(const char *rational=NULL);
90 };
91
92
96 struct Port {
97 const char *name;
98 const char *metadata;
99 const Ports *ports;
100 std::function<void(msg_t, RtData&)> cb;
101
102 class MetaIterator
103 {
104 public:
105 MetaIterator(const char *str);
106
107 //A bit odd to return yourself, but it seems to work for this
108 //context
109 const MetaIterator& operator*(void) const {return *this;}
110 const MetaIterator* operator->(void) const {return this;}
111 bool operator==(MetaIterator a) {return title == a.title;}
112 bool operator!=(MetaIterator a) {return title != a.title;}
113 MetaIterator& operator++(void);
114 operator bool() const;
115
116 const char *title;
117 const char *value;
118 };
119
120 class MetaContainer
121 {
122 public:
123 MetaContainer(const char *str_);
124
125 MetaIterator begin(void) const;
126 MetaIterator end(void) const;
127
128 MetaIterator find(const char *str) const;
129 size_t length(void) const;
132 const char *operator[](const char *str) const;
133
134 const char *str_ptr;
135 };
136
137 MetaContainer meta(void) const
138 {
139 if(metadata && *metadata == ':')
140 return MetaContainer(metadata+1);
141 else
142 return MetaContainer(metadata);
143 }
144
145 };
146
157 struct Ports
158 {
159 std::vector<Port> ports;
160 std::function<void(msg_t, RtData&)> default_handler;
161
162 typedef std::vector<Port>::const_iterator itr_t;
163
165 itr_t begin() const {return ports.begin();}
166
168 itr_t end() const {return ports.end();}
169
171 size_t size() const {return ports.size();}
172
174 const Port &operator[](unsigned i) const {return ports[i];}
175
176 Ports(std::initializer_list<Port> l);
177 ~Ports(void);
178
179 Ports(const Ports&) = delete;
180
199 void dispatch(const char *m, RtData &d, bool base_dispatch=false) const;
200
205 const Port *operator[](const char *name) const;
206
207
225 const Port *apropos(const char *path) const;
226
232 static char *collapsePath(char *p);
233
234 protected:
235 void refreshMagic(void);
236 private:
237 //Performance hacks
238 class Port_Matcher *impl;
239 unsigned elms;
240 };
241
242 struct ClonePort
243 {
244 const char *name;
245 std::function<void(msg_t, RtData&)> cb;
246 };
247
248 struct ClonePorts:public Ports
249 {
250 ClonePorts(const Ports &p,
251 std::initializer_list<ClonePort> c);
252 };
253
254 struct MergePorts:public Ports
255 {
256 MergePorts(std::initializer_list<const Ports*> c);
257 };
258
274 int canonicalize_arg_vals(rtosc_arg_val_t* av, size_t n,
275 const char* port_args, Port::MetaContainer meta);
276
283 void map_arg_vals(rtosc_arg_val_t* av, size_t n,
284 Port::MetaContainer meta);
285
286 /*********************
287 * Port walking code *
288 *********************/
289 //typedef std::function<void(const Port*,const char*)> port_walker_t;
303 typedef void(*port_walker_t)(const Port*,const char*,const char*,
304 const Ports&,void*,void*);
305
320 void walk_ports(const Ports *base,
321 char *name_buffer,
322 size_t buffer_size,
323 void *data,
324 port_walker_t walker,
325 bool expand_bundles = true,
326 void *runtime = NULL);
327
344 void path_search(const rtosc::Ports& root, const char *str, const char *needle,
345 char *types, std::size_t max_types,
346 rtosc_arg_t* args, std::size_t max_args);
347
369 std::size_t path_search(const rtosc::Ports& root, const char *m,
370 std::size_t max_ports,
371 char *msgbuf, std::size_t bufsize);
372
380 int enum_key(Port::MetaContainer meta, const char* value);
381
382 /*********************
383 * Port Dumping code *
384 *********************/
385
386 struct OscDocFormatter
387 {
388 const Ports *p;
389 std::string prog_name;
390 std::string uri;
391 std::string doc_origin;
392 std::string author_first;
393 std::string author_last;
394 //TODO extend this some more
395 };
396
397 std::ostream &operator<<(std::ostream &o, OscDocFormatter &formatter);
398 };
399 #endif
rtosc::Ports::operator[]
const Port * operator[](const char *name) const
Retrieve local port by name TODO implement full matching.
rtosc::Port::ports
const Ports * ports
Pointer to further ports.
Definition: ports.h:99
rtosc::RtData::reply
virtual void reply(const char *msg)
Reply if information has been requested.
rtosc::Ports::operator[]
const Port & operator[](unsigned i) const
Forwards to builtin container.
Definition: ports.h:174
rtosc::RtData::matches
int matches
number of matches returned from dispatch routine
Definition: ports.h:63
rtosc::Ports::apropos
const Port * apropos(const char *path) const
Find the best match for a given path.
rtosc::RtData::obj
void * obj
runtime object to dispatch this object to
Definition: ports.h:62
rtosc::RtData::broadcast
virtual void broadcast(const char *path, const char *args,...)
Transmit initialization/change of a value to all listeners.
rtosc_arg_t
Definition: rtosc.h:70
rtosc::Ports
Ports - a dispatchable collection of Port entries.
Definition: ports.h:158
rtosc::enum_key
int enum_key(Port::MetaContainer meta, const char *value)
Return the index with value value from the metadata's enumeration.
rtosc::MergePorts
Definition: ports.h:255
rtosc::Port::MetaIterator
Definition: ports.h:103
rtosc::Ports::size
size_t size() const
Forwards to builtin container.
Definition: ports.h:171
rtosc::path_search
void path_search(const rtosc::Ports &root, const char *str, const char *needle, char *types, std::size_t max_types, rtosc_arg_t *args, std::size_t max_args)
Returns paths and metadata of all direct children of a port, or of the port itself if that port has n...
rtosc::ClonePorts
Definition: ports.h:249
rtosc::canonicalize_arg_vals
int canonicalize_arg_vals(rtosc_arg_val_t *av, size_t n, const char *port_args, Port::MetaContainer meta)
Convert given argument values to their canonical representation.
rtosc::Port::metadata
const char * metadata
Statically accessable data about port.
Definition: ports.h:98
rtosc_arg_val_t
Definition: rtosc.h:86
rtosc::Port::MetaContainer::operator[]
const char * operator[](const char *str) const
Return the key to the value str, or NULL if the key is invalid or if there's no value for that key.
rtosc::RtData::loc
char * loc
Location of where the dispatch routine is currently being called.
Definition: ports.h:60
rtosc::Ports::dispatch
void dispatch(const char *m, RtData &d, bool base_dispatch=false) const
Dispatches message to all matching ports.
rtosc::Port
Port in rtosc dispatching hierarchy.
Definition: ports.h:96
rtosc::Port::cb
std::function< void(msg_t, RtData &)> cb
Callback for matching functions.
Definition: ports.h:100
rtosc::OscDocFormatter
Definition: ports.h:387
rtosc.h
Functions handling messages and arguments.
rtosc::RtData
data object for the dispatch routine
Definition: ports.h:52
rtosc::Port::MetaContainer
Definition: ports.h:121
rtosc::Ports::collapsePath
static char * collapsePath(char *p)
Collapse path with parent path identifiers "/..".
rtosc::map_arg_vals
void map_arg_vals(rtosc_arg_val_t *av, size_t n, Port::MetaContainer meta)
Convert each of the given arguments to their mapped symbol, if possible.
rtosc::Port::name
const char * name
Pattern for messages to match.
Definition: ports.h:97
rtosc::walk_ports
void walk_ports(const Ports *base, char *name_buffer, size_t buffer_size, void *data, port_walker_t walker, bool expand_bundles=true, void *runtime=NULL)
Call a function on all ports and subports.
rtosc::Ports::end
itr_t end() const
Forwards to builtin container.
Definition: ports.h:168
rtosc::RtData::chain
virtual void chain(const char *msg)
Bypass message to some kind of backend if the message can not be handled.
rtosc::RtData::port
const Port * port
dispatch will write the matching port's pointer here
Definition: ports.h:64
rtosc::ClonePort
Definition: ports.h:243
rtosc::RtData::message
const char * message
Will be set to point to the full OSC message in case of a base dispatch.
Definition: ports.h:67
rtosc::Ports::begin
itr_t begin() const
Forwards to builtin container.
Definition: ports.h:165