Real Time Open Sound Control librtosc: /build/rtosc/src/rtosc-0.3.1/include/rtosc/ports.h Source File

Real Time Open Sound Control librtosc

Loading...
Searching...
No Matches
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
42namespace rtosc {
43
44//First define all types
45typedef const char *msg_t;
46
47struct Port;
48struct Ports;
49
51 struct RtData
52{
53 RtData(void);
54 virtual ~RtData() = default;
55
61 char *loc;
62 size_t loc_size;
63 void *obj;
64 int matches;
65 const Port *port;
68 const char *message;
69
70 int idx[16];
71 void push_index(int ind);
72 void pop_index(void);
73
74 virtual void replyArray(const char *path, const char *args,
75 rtosc_arg_t *vals);
76 virtual void reply(const char *path, const char *args, ...);
78 virtual void reply(const char *msg);
79 virtual void chain(const char *path, const char *args, ...);
81 virtual void chain(const char *msg);
82 virtual void chainArray(const char *path, const char *args,
83 rtosc_arg_t *vals);
85 virtual void broadcast(const char *path, const char *args, ...);
86 virtual void broadcast(const char *msg);
87 virtual void broadcastArray(const char *path, const char *args,
88 rtosc_arg_t *vals);
89
90 virtual void forward(const char *rational=NULL);
91};
92
93
97 struct Port {
98 const char *name;
99 const char *metadata;
100 const Ports *ports;
101 std::function<void(msg_t, RtData&)> cb;
102
103 class MetaIterator
104 {
105 public:
106 MetaIterator(const char *str);
107
108 //A bit odd to return yourself, but it seems to work for this
109 //context
110 const MetaIterator& operator*(void) const {return *this;}
111 const MetaIterator* operator->(void) const {return this;}
112 bool operator==(MetaIterator a) {return title == a.title;}
113 bool operator!=(MetaIterator a) {return title != a.title;}
114 MetaIterator& operator++(void);
115 operator bool() const;
116
117 const char *title;
118 const char *value;
119 };
120
121 class MetaContainer
122 {
123 public:
124 MetaContainer(const char *str_);
125
126 MetaIterator begin(void) const;
127 MetaIterator end(void) const;
128
129 MetaIterator find(const char *str) const;
130 size_t length(void) const;
133 const char *operator[](const char *str) const;
134
135 const char *str_ptr;
136 };
137
138 MetaContainer meta(void) const
139 {
140 if(metadata && *metadata == ':')
141 return MetaContainer(metadata+1);
142 else
143 return MetaContainer(metadata);
144 }
145
146};
147
158 struct Ports
159{
160 std::vector<Port> ports;
161 std::function<void(msg_t, RtData&)> default_handler;
162
163 typedef std::vector<Port>::const_iterator itr_t;
164
166 itr_t begin() const {return ports.begin();}
167
169 itr_t end() const {return ports.end();}
170
172 size_t size() const {return ports.size();}
173
175 const Port &operator[](unsigned i) const {return ports[i];}
176
177 Ports(std::initializer_list<Port> l);
178 ~Ports(void);
179
180 Ports(const Ports&) = delete;
181
200 void dispatch(const char *m, RtData &d, bool base_dispatch=false) const;
201
206 const Port *operator[](const char *name) const;
207
208
226 const Port *apropos(const char *path) const;
227
233 static char *collapsePath(char *p);
234
235 protected:
236 void refreshMagic(void);
237 private:
238 //Performance hacks
239 class Port_Matcher *impl;
240 unsigned elms;
241};
242
243 struct ClonePort
244{
245 const char *name;
246 std::function<void(msg_t, RtData&)> cb;
247};
248
249 struct ClonePorts:public Ports
250{
251 ClonePorts(const Ports &p,
252 std::initializer_list<ClonePort> c);
253};
254
255 struct MergePorts:public Ports
256{
257 MergePorts(std::initializer_list<const Ports*> c);
258};
259
275 int canonicalize_arg_vals(rtosc_arg_val_t* av, size_t n,
276 const char* port_args, Port::MetaContainer meta);
277
284 void map_arg_vals(rtosc_arg_val_t* av, size_t n,
285 Port::MetaContainer meta);
286
287/*********************
288 * Port walking code *
289 *********************/
290//typedef std::function<void(const Port*,const char*)> port_walker_t;
304 typedef void(*port_walker_t)(const Port*,const char*,const char*,
305 const Ports&,void*,void*);
306
321 void walk_ports(const Ports *base,
322 char *name_buffer,
323 size_t buffer_size,
324 void *data,
325 port_walker_t walker,
326 bool expand_bundles = true,
327 void *runtime = NULL,
328 bool ranges = false);
329
334 enum class path_search_opts
335{
340 unmodified,
345 sorted,
353 sorted_and_unique_prefix
354};
355
374 void path_search(const rtosc::Ports& root, const char *str, const char *needle,
375 char *types, std::size_t max_types,
376 rtosc_arg_t* args, std::size_t max_args,
377 path_search_opts opts = path_search_opts::sorted_and_unique_prefix,
378 bool reply_with_query = false);
379
403std::size_t path_search(const rtosc::Ports& root, const char *m,
404 std::size_t max_ports,
405 char *msgbuf, std::size_t bufsize,
406 path_search_opts opts = path_search_opts::sorted_and_unique_prefix,
407 bool reply_with_query = false);
408
416 int enum_key(Port::MetaContainer meta, const char* value);
417
418/*********************
419 * Port Dumping code *
420 *********************/
421
422 struct OscDocFormatter
423{
424 const Ports *p;
425 std::string prog_name;
426 std::string uri;
427 std::string doc_origin;
428 std::string author_first;
429 std::string author_last;
430 //TODO extend this some more
431};
432
433std::ostream &operator<<(std::ostream &o, OscDocFormatter &formatter);
434};
435#endif
rtosc::Port::MetaContainer
Definition: ports.h:122
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::Port::MetaIterator
Definition: ports.h:104
rtosc::enum_key
int enum_key(Port::MetaContainer meta, const char *value)
Return the index with value value from the metadata's enumeration.
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::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, path_search_opts opts=path_search_opts::sorted_and_unique_prefix, bool reply_with_query=false)
Returns paths and metadata of all direct children of a port, or of the port itself if that port has n...
rtosc::path_search_opts
path_search_opts
Options for path_search.
Definition: ports.h:335
rtosc::path_search_opts::sorted
@ sorted
Return ports sorted, but not filtered.
rtosc::path_search_opts::unmodified
@ unmodified
Return ports in the order they are found.
rtosc::path_search_opts::sorted_and_unique_prefix
@ sorted_and_unique_prefix
Sorted and prefix-filtered: If "a/" is found, don't add any "a/...".
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::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, bool ranges=false)
Call a function on all ports and subports.
rtosc.h
Functions handling messages and arguments.
rtosc::ClonePort
Definition: ports.h:244
rtosc::ClonePorts
Definition: ports.h:250
rtosc::MergePorts
Definition: ports.h:256
rtosc::OscDocFormatter
Definition: ports.h:423
rtosc::Port
Port in rtosc dispatching hierarchy.
Definition: ports.h:97
rtosc::Port::metadata
const char * metadata
Statically accessable data about port.
Definition: ports.h:99
rtosc::Port::name
const char * name
Pattern for messages to match.
Definition: ports.h:98
rtosc::Port::cb
std::function< void(msg_t, RtData &)> cb
Callback for matching functions.
Definition: ports.h:101
rtosc::Port::ports
const Ports * ports
Pointer to further ports.
Definition: ports.h:100
rtosc::Ports
Ports - a dispatchable collection of Port entries.
Definition: ports.h:159
rtosc::Ports::operator[]
const Port * operator[](const char *name) const
Retrieve local port by name TODO implement full matching.
rtosc::Ports::dispatch
void dispatch(const char *m, RtData &d, bool base_dispatch=false) const
Dispatches message to all matching ports.
rtosc::Ports::operator[]
const Port & operator[](unsigned i) const
Forwards to builtin container.
Definition: ports.h:175
rtosc::Ports::apropos
const Port * apropos(const char *path) const
Find the best match for a given path.
rtosc::Ports::end
itr_t end() const
Forwards to builtin container.
Definition: ports.h:169
rtosc::Ports::collapsePath
static char * collapsePath(char *p)
Collapse path with parent path identifiers "/..".
rtosc::Ports::begin
itr_t begin() const
Forwards to builtin container.
Definition: ports.h:166
rtosc::Ports::size
size_t size() const
Forwards to builtin container.
Definition: ports.h:172
rtosc::RtData
data object for the dispatch routine
Definition: ports.h:52
rtosc::RtData::port
const Port * port
dispatch will write the matching port's pointer here
Definition: ports.h:65
rtosc::RtData::matches
int matches
number of matches returned from dispatch routine
Definition: ports.h:64
rtosc::RtData::loc
char * loc
Location of where the dispatch routine is currently being called.
Definition: ports.h:61
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::reply
virtual void reply(const char *msg)
Reply if information has been requested.
rtosc::RtData::broadcast
virtual void broadcast(const char *path, const char *args,...)
Transmit initialization/change of a value to all listeners.
rtosc::RtData::obj
void * obj
runtime object to dispatch this object to
Definition: ports.h:63
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:68
rtosc_arg_val_t
Definition: rtosc.h:86
rtosc_arg_t
Definition: rtosc.h:70