Real Time Open Sound Control librtosc
Loading...
Searching...
No Matches
bundle-foreach.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Johannes Lorenz
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 BUNDLE_FOREACH
32#define BUNDLE_FOREACH
33
34#include <cctype>
35#include <cstdlib>
36#include <cstdio>
37#include "ports.h"
38
39namespace rtosc {
40
46template<class F>
47void bundle_foreach(const struct Port& p, const char* name, char* old_end,
48 /* data which is only being used by the ftors */
49 const char* name_buffer, const struct Ports& base,
50 void* data, void* runtime, const F& ftor,
51 /* options */
52 bool expand_bundles = true,
53 bool cut_afterwards = true,
54 bool ranges = false)
55{
56 char *pos = old_end;
57 while(*name != '#') *pos++ = *name++;
58 const unsigned max = atoi(name+1);
59 while(isdigit(*++name)) ;
60
61 char* pos2 = pos;
62
63 if(expand_bundles && !ranges)
64 for(unsigned i=0; i<max; ++i)
65 {
66 const char* name2_2 = name;
67 pos2 = pos + sprintf(pos,"%d",i);
68
69 // append everything behind the '#' (for cases like a#N/b)
70 while(*name2_2 && *name2_2 != ':')
71 *pos2++ = *name2_2++;
72 *pos2 = 0;
73
74 ftor(&p, name_buffer, old_end, base, data, runtime);
75 }
76 else // !expand_bundles || ranges
77 {
78 const char* name2_2 = name;
79 if(ranges)
80 pos2 += sprintf(pos,"[0,%d]",max-1);
81
82 // append everything behind the '#' (for cases like a#N/b)
83 while(*name2_2 && *name2_2 != ':')
84 *pos2++ = *name2_2++;
85 *pos2 = 0;
86
87 ftor(&p, name_buffer, old_end, base, data, runtime);
88 }
89
90 if(cut_afterwards)
91 *old_end = 0;
92 else
93 *pos2 = 0;
94}
95
96// use this function if you don't want to do anything in bundle_foreach
97// (useful to create paths if cut_afterwards is true)
98inline void bundle_foreach_do_nothing(const Port*, const char*, const char*,
99 const Ports&, void*, void*){}
100
101}
102
103#endif // BUNDLE_FOREACH
void bundle_foreach(const struct Port &p, const char *name, char *old_end, const char *name_buffer, const struct Ports &base, void *data, void *runtime, const F &ftor, bool expand_bundles=true, bool cut_afterwards=true, bool ranges=false)
Execute a callback for all bundle elements of a bundle port.
Definition: bundle-foreach.h:47
Collection of functions for ports.
Port in rtosc dispatching hierarchy.
Definition: ports.h:97
Ports - a dispatchable collection of Port entries.
Definition: ports.h:159