Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

port-sugar.h 19KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2016 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. #include <assert.h>
  25. #include <type_traits>
  26. #ifndef RTOSC_PORT_SUGAR
  27. #define RTOSC_PORT_SUGAR
  28. //Hack to workaround old incomplete decltype implementations
  29. #ifdef __GNUC__
  30. #ifndef __clang__
  31. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 7)
  32. template<typename T>
  33. struct rtosc_hack_decltype_t
  34. {
  35. typedef T type;
  36. };
  37. #define decltype(expr) rtosc_hack_decltype_t<decltype(expr)>::type
  38. #endif
  39. #endif
  40. #endif
  41. //General macro utilities
  42. #define STRINGIFY2(a) #a
  43. #define STRINGIFY(a) STRINGIFY2(a)
  44. //Helper for documenting varargs
  45. #define IMPL(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,N, ...) N
  46. #define LAST_IMP(...) IMPL(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0,0,0)
  47. #define DOC_IMP9(a,b,c,d,e,f,g,h,i) a b c d e f g h rDoc(i)
  48. #define DOC_IMP8(a,b,c,d,e,f,g,h) a b c d e f g rDoc(h)
  49. #define DOC_IMP7(a,b,c,d,e,f,g) a b c d e f rDoc(g)
  50. #define DOC_IMP6(a,b,c,d,e,f) a b c d e rDoc(f)
  51. #define DOC_IMP5(a,b,c,d,e) a b c d rDoc(e)
  52. #define DOC_IMP4(a,b,c,d) a b c rDoc(d)
  53. #define DOC_IMP3(a,b,c) a b rDoc(c)
  54. #define DOC_IMP2(a,b) a rDoc(b)
  55. #define DOC_IMP1(a) rDoc(a)
  56. #define DOC_IMP0() YOU_MUST_DOCUMENT_YOUR_PORTS
  57. #define DOC_IMP(count, ...) DOC_IMP ##count(__VA_ARGS__)
  58. #define DOC_I(count, ...) DOC_IMP(count,__VA_ARGS__)
  59. #define DOC(...) DOC_I(LAST_IMP(__VA_ARGS__), __VA_ARGS__)
  60. #define rINC(x) rINC_ ## x
  61. #define rINC_0 1
  62. #define rINC_1 2
  63. #define rINC_2 3
  64. #define rINC_3 4
  65. #define rINC_4 5
  66. #define rINC_5 6
  67. #define rINC_6 7
  68. #define rINC_7 8
  69. #define rINC_8 9
  70. #define rINC_9 10
  71. #define rINC_10 11
  72. #define rINC_11 12
  73. #define rINC_12 13
  74. #define rINC_13 14
  75. #define rINC_14 15
  76. #define rINC_15 16
  77. //Helper for applying macro on varargs
  78. //arguments: counting offset, macro, macro args
  79. #define MAC_EACH_0(o, m, x, ...) INSUFFICIENT_ARGUMENTS_PROVIDED_TO_MAC_EACH
  80. #define MAC_EACH_1(o, m, x, ...) m(o, x)
  81. #define MAC_EACH_2(o, m, x, ...) m(o, x) MAC_EACH_1(rINC(o), m, __VA_ARGS__)
  82. #define MAC_EACH_3(o, m, x, ...) m(o, x) MAC_EACH_2(rINC(o), m, __VA_ARGS__)
  83. #define MAC_EACH_4(o, m, x, ...) m(o, x) MAC_EACH_3(rINC(o), m, __VA_ARGS__)
  84. #define MAC_EACH_5(o, m, x, ...) m(o, x) MAC_EACH_4(rINC(o), m, __VA_ARGS__)
  85. #define MAC_EACH_6(o, m, x, ...) m(o, x) MAC_EACH_5(rINC(o), m, __VA_ARGS__)
  86. #define MAC_EACH_7(o, m, x, ...) m(o, x) MAC_EACH_6(rINC(o), m, __VA_ARGS__)
  87. #define MAC_EACH_8(o, m, x, ...) m(o, x) MAC_EACH_7(rINC(o), m, __VA_ARGS__)
  88. #define MAC_EACH_9(o, m, x, ...) m(o, x) MAC_EACH_8(rINC(o), m, __VA_ARGS__)
  89. #define MAC_EACH_10(o, m, x, ...) m(o, x) MAC_EACH_9(rINC(o), m, __VA_ARGS__)
  90. #define MAC_EACH_11(o, m, x, ...) m(o, x) MAC_EACH_10(rINC(o), m, __VA_ARGS__)
  91. #define MAC_EACH_12(o, m, x, ...) m(o, x) MAC_EACH_11(rINC(o), m, __VA_ARGS__)
  92. #define MAC_EACH_13(o, m, x, ...) m(o, x) MAC_EACH_12(rINC(o), m, __VA_ARGS__)
  93. #define MAC_EACH_14(o, m, x, ...) m(o, x) MAC_EACH_13(rINC(o), m, __VA_ARGS__)
  94. #define MAC_EACH_15(o, m, x, ...) m(o, x) MAC_EACH_14(rINC(o), m, __VA_ARGS__)
  95. #define MAC_EACH_16(o, m, x, ...) m(o, x) MAC_EACH_15(rINC(o), m, __VA_ARGS__)
  96. #define MAC_EACH_IMP(off, mac, count, ...) \
  97. MAC_EACH_ ##count(off, mac,__VA_ARGS__)
  98. #define MAC_EACH_I(off, mac, count, ...) \
  99. MAC_EACH_IMP(off, mac, count, __VA_ARGS__)
  100. #define MAC_EACH_OFF(off, mac, ...) \
  101. MAC_EACH_I(off, mac, LAST_IMP(__VA_ARGS__), __VA_ARGS__)
  102. #define MAC_EACH(mac, ...) MAC_EACH_OFF(0, mac, __VA_ARGS__)
  103. // 1 2 3 4 5 6 7 8 910111213141516
  104. #define OPTIONS_IMP16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
  105. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  106. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)\
  107. rOpt(14,o)rOpt(15,p)
  108. #define OPTIONS_IMP15(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) \
  109. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  110. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)\
  111. rOpt(14,o)
  112. #define OPTIONS_IMP14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
  113. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  114. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)rOpt(13,n)
  115. #define OPTIONS_IMP13(a,b,c,d,e,f,g,h,i,j,k,l,m) \
  116. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  117. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)rOpt(12,m)
  118. #define OPTIONS_IMP12(a,b,c,d,e,f,g,h,i,j,k,l) \
  119. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  120. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)rOpt(11,l)
  121. #define OPTIONS_IMP11(a,b,c,d,e,f,g,h,i,j,k) \
  122. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  123. rOpt(7,h) rOpt(8,i) rOpt(9,j) rOpt(10,k)
  124. #define OPTIONS_IMP10(a,b,c,d,e,f,g,h,i,j) \
  125. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  126. rOpt(7,h) rOpt(8,i) rOpt(9,j)
  127. #define OPTIONS_IMP9(a,b,c,d,e,f,g,h,i) \
  128. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  129. rOpt(7,h) rOpt(8,i)
  130. #define OPTIONS_IMP8(a,b,c,d,e,f,g,h) \
  131. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g) \
  132. rOpt(7,h)
  133. #define OPTIONS_IMP7(a,b,c,d,e,f,g) \
  134. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f) rOpt(6,g)
  135. #define OPTIONS_IMP6(a,b,c,d,e,f) \
  136. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e) rOpt(5,f)
  137. #define OPTIONS_IMP5(a,b,c,d,e) \
  138. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d) rOpt(4,e)
  139. #define OPTIONS_IMP4(a,b,c,d) \
  140. rOpt(0,a) rOpt(1,b) rOpt(2,c) rOpt(3,d)
  141. #define OPTIONS_IMP3(a,b,c) \
  142. rOpt(0,a) rOpt(1,b) rOpt(2,c)
  143. #define OPTIONS_IMP2(a,b) \
  144. rOpt(0,a) rOpt(1,b)
  145. #define OPTIONS_IMP1(a) \
  146. rOpt(0,a)
  147. #define OPTIONS_IMP0() YOU_MUST_PROVIDE_OPTIONS
  148. #define OPTIONS_IMP(count, ...) OPTIONS_IMP ##count(__VA_ARGS__)
  149. #define OPTIONS_I(count, ...) OPTIONS_IMP(count, __VA_ARGS__)
  150. #define OPTIONS(...) OPTIONS_I(LAST_IMP(__VA_ARGS__), __VA_ARGS__)
  151. //Additional Change Callback (after parameters have been changed)
  152. //This can be used to queue up interpolation or parameter regen
  153. #define rChangeCb
  154. //Normal parameters
  155. #define rParam(name, ...) \
  156. {STRINGIFY(name) "::c", rProp(parameter) rMap(min, 0) rMap(max, 127) DOC(__VA_ARGS__), NULL, rParamCb(name)}
  157. #define rParamF(name, ...) \
  158. {STRINGIFY(name) "::f", rProp(parameter) DOC(__VA_ARGS__), NULL, rParamFCb(name)}
  159. #define rParamI(name, ...) \
  160. {STRINGIFY(name) "::i", rProp(parameter) DOC(__VA_ARGS__), NULL, rParamICb(name)}
  161. #define rToggle(name, ...) \
  162. {STRINGIFY(name) "::T:F",rProp(parameter) DOC(__VA_ARGS__), NULL, rToggleCb(name)}
  163. #define rOption(name, ...) \
  164. {STRINGIFY(name) "::i:c:S",rProp(parameter) rProp(enumerated) DOC(__VA_ARGS__), NULL, rOptionCb(name)}
  165. //Array operators
  166. #define rArrayF(name, length, ...) \
  167. {STRINGIFY(name) "#" STRINGIFY(length) "::f", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayFCb(name)}
  168. #define rArray(name, length, ...) \
  169. {STRINGIFY(name) "#" STRINGIFY(length) "::c:i", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayCb(name)}
  170. #define rArrayT(name, length, ...) \
  171. {STRINGIFY(name) "#" STRINGIFY(length) "::T:F", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayTCb(name)}
  172. #define rArrayI(name, length, ...) \
  173. {STRINGIFY(name) "#" STRINGIFY(length) "::i", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayICb(name)}
  174. #define rArrayOption(name, length, ...) \
  175. {STRINGIFY(name) "#" STRINGIFY(length) "::i:c:S", rProp(parameter) DOC(__VA_ARGS__), NULL, rArrayOptionCb(name)}
  176. //Method callback Actions
  177. #define rAction(name, ...) \
  178. {STRINGIFY(name) ":", DOC(__VA_ARGS__), NULL, rActionCb(name)}
  179. #define rActioni(name, ...) \
  180. {STRINGIFY(name) ":i", DOC(__VA_ARGS__), NULL, rActioniCb(name)}
  181. //Alias operators
  182. #define rParams(name, length, ...) \
  183. rArray(name, length, __VA_ARGS__), \
  184. {STRINGIFY(name) ":", rProp(alias) rDoc("get all data from aliased array"), NULL, rParamsCb(name, length)}
  185. template<class T> constexpr T spice(T*t) {return *t;}
  186. //Recursion [two ports in one for pointer manipulation]
  187. #define rRecur(name, ...) \
  188. {STRINGIFY(name) "/", DOC(__VA_ARGS__), &decltype(rObject::name)::ports, rRecurCb(name)}, \
  189. {STRINGIFY(name) ":", rProp(internal) rDoc("get obj pointer"), NULL, rRecurPtrCb(name)}
  190. #define rRecurp(name, ...) \
  191. {STRINGIFY(name) "/", DOC(__VA_ARGS__), \
  192. &decltype(spice(rObject::name))::ports, \
  193. rRecurpCb(name)}
  194. #define rRecurs(name, length, ...) \
  195. {STRINGIFY(name) "#" STRINGIFY(length)"/", DOC(__VA_ARGS__), \
  196. &decltype(spice(&rObject::name[0]))::ports, \
  197. rRecursCb(name, length)}
  198. //Technically this is a pointer pointer method...
  199. #define rRecursp(name, length, ...) \
  200. {STRINGIFY(name)"#" STRINGIFY(length) "/", DOC(__VA_ARGS__), \
  201. &decltype(spice(rObject::name[0]))::ports, \
  202. rRecurspCb(name)}
  203. //{STRINGIFY(name) ":", rProp(internal), NULL, rRecurPtrCb(name)}
  204. //let this recurring parameter depend on another port
  205. #define rEnabledBy(portname) rMap(enabled by, portname)
  206. #define rEnabledByCondition(cond_name) rEnabledBy(cond_name)
  207. #define rEnabledCondition(cond_name, condition) \
  208. {STRINGIFY(cond_name) ":", rProp(internal), NULL, rEnabledIfCb(condition)}
  209. #define rEnabledIfCb(condition) rBOIL_BEGIN \
  210. assert(!rtosc_narguments(msg)); \
  211. data.reply(loc, (condition)?"T":"F"); \
  212. rBOIL_END \
  213. #define rSelf(type, ...) \
  214. {"self:", rProp(internal) rMap(class, type) __VA_ARGS__ rDoc("port metadata"), 0, \
  215. [](const char *, rtosc::RtData &d){ \
  216. d.reply(d.loc, "b", sizeof(d.obj), &d.obj);}}\
  217. //Misc
  218. #define rDummy(name, ...) {STRINGIFY(name), rProp(dummy), NULL, [](msg_t, rtosc::RtData &){}}
  219. #define rString(name, len, ...) \
  220. {STRINGIFY(name) "::s", rMap(length, len) rProp(parameter) DOC(__VA_ARGS__), NULL, rStringCb(name,len)}
  221. //General property operators
  222. #define rMap(name, value) ":" STRINGIFY(name) "\0=" STRINGIFY(value) "\0"
  223. #define rProp(name) ":" STRINGIFY(name) "\0"
  224. //Scaling property
  225. #define rLinear(min_, max_) rMap(min, min_) rMap(max, max_) rMap(scale, linear)
  226. #define rLog(min_, max_) rMap(min, min_) rMap(max, max_) rMap(scale, logarithmic)
  227. //Special values
  228. #define rSpecial(doc) ":special\0" STRINGIFY(doc) "\0"
  229. #define rCentered ":centered\0"
  230. //Default values
  231. #define rDefault(default_value_) rMap(default, default_value_)
  232. #define rDefaultId(default_value_) ":default\0=\"" STRINGIFY(default_value_) "\"S\0"
  233. //#define rDefaultArr(default_value_, idx_) rMap(default[idx_], default_value_)
  234. #define rPreset(no, default_value) \
  235. ":default " STRINGIFY(no) "\0=" STRINGIFY(default_value) "\0"
  236. #define rPresetsAt(offs, ...) MAC_EACH_OFF(offs, rPreset, __VA_ARGS__)
  237. #define rPresets(...) rPresetsAt(0, __VA_ARGS__)
  238. #define rDefaultDepends(dep_path_) rMap(default depends, dep_path_)
  239. #define rDefaultMissing "" // macro to denote yet missing default values
  240. //#define rNoDefaults() ":no defaults\0" //!< this port (and all children) have no defaults
  241. //! @brief Denote that this port and its children must always be skipped from
  242. //! port-walking if a runtime is being given.
  243. #define rNoWalk rProp(no walk)
  244. //Misc properties
  245. #define rDoc(doc) ":documentation\0=" doc "\0"
  246. #define rOpt(numeric,symbolic) rMap(map numeric, symbolic)
  247. #define rOptions(...) OPTIONS(__VA_ARGS__)
  248. //Zest Metadata
  249. #define rShort(name) ":shortname\0=" name "\0"
  250. //Callback Implementations
  251. #define rBOIL_BEGIN [](const char *msg, rtosc::RtData &data) { \
  252. (void) msg; (void) data; \
  253. rObject *obj = (rObject*) data.obj;(void) obj; \
  254. const char *args = rtosc_argument_string(msg); (void) args;\
  255. const char *loc = data.loc; (void) loc;\
  256. auto prop = data.port->meta(); (void) prop;
  257. #define rBOIL_END }
  258. #define rLIMIT(var, convert) \
  259. if(prop["min"] && var < (decltype(var)) convert(prop["min"])) \
  260. var = convert(prop["min"]);\
  261. if(prop["max"] && var > (decltype(var)) convert(prop["max"])) \
  262. var = convert(prop["max"]);
  263. #define rTYPE(n) decltype(obj->n)
  264. //#define rAPPLY(n,t) if(obj->n != var) data.reply("undo_change", "s" #t #t, data.loc, obj->n, var); obj->n = var;
  265. #define rCAPPLY(getcode, t, setcode) if(getcode != var) data.reply("undo_change", "s" #t #t, data.loc, getcode, var); setcode;
  266. #define rAPPLY(n,t) rCAPPLY(obj->n, t, obj->n = var)
  267. #define rParamCb(name) rBOIL_BEGIN \
  268. if(!strcmp("", args)) {\
  269. data.reply(loc, "c", obj->name); \
  270. } else { \
  271. rTYPE(name) var = rtosc_argument(msg, 0).i; \
  272. rLIMIT(var, atoi) \
  273. rAPPLY(name, c) \
  274. data.broadcast(loc, "c", obj->name);\
  275. rChangeCb \
  276. } rBOIL_END
  277. #define rParamFCb(name) rBOIL_BEGIN \
  278. if(!strcmp("", args)) {\
  279. data.reply(loc, "f", obj->name); \
  280. } else { \
  281. rTYPE(name) var = rtosc_argument(msg, 0).f; \
  282. rLIMIT(var, atof) \
  283. rAPPLY(name, f) \
  284. data.broadcast(loc, "f", obj->name);\
  285. rChangeCb \
  286. } rBOIL_END
  287. #define rParamICb(name) rBOIL_BEGIN \
  288. if(!strcmp("", args)) {\
  289. data.reply(loc, "i", obj->name); \
  290. } else { \
  291. rTYPE(name) var = rtosc_argument(msg, 0).i; \
  292. rLIMIT(var, atoi) \
  293. rAPPLY(name, i) \
  294. data.broadcast(loc, "i", obj->name);\
  295. rChangeCb \
  296. } rBOIL_END
  297. #define rCOptionCb_(getcode, setcode) { \
  298. if(!strcmp("", args)) {\
  299. data.reply(loc, "i", getcode); \
  300. } else if(!strcmp("s", args) || !strcmp("S", args)) { \
  301. auto var = \
  302. enum_key(prop, rtosc_argument(msg, 0).s); \
  303. /* make sure we have no out-of-bound options */ \
  304. assert(!prop["min"] || \
  305. var >= atoi(prop["min"])); \
  306. assert(!prop["max"] || \
  307. var <= atoi(prop["max"])); \
  308. rCAPPLY(getcode, i, setcode) \
  309. data.broadcast(loc, "i", getcode); \
  310. rChangeCb \
  311. } else {\
  312. auto var = \
  313. rtosc_argument(msg, 0).i; \
  314. rLIMIT(var, atoi) \
  315. rCAPPLY(getcode, i, setcode) \
  316. data.broadcast(loc, rtosc_argument_string(msg), getcode);\
  317. rChangeCb \
  318. } \
  319. }
  320. #define rOptionCb_(name) rCOptionCb_(obj->name, obj->name = var)
  321. #define rOptionCb(name) rBOIL_BEGIN \
  322. rOptionCb_(name) \
  323. rBOIL_END
  324. #define rCOptionCb(getcode, setcode) rBOIL_BEGIN \
  325. rCOptionCb_(getcode, setcode) \
  326. rBOIL_END
  327. #define rToggleCb(name) rBOIL_BEGIN \
  328. if(!strcmp("", args)) {\
  329. data.reply(loc, obj->name ? "T" : "F"); \
  330. } else { \
  331. if(obj->name != rtosc_argument(msg, 0).T) { \
  332. data.broadcast(loc, args);\
  333. obj->name = rtosc_argument(msg, 0).T; \
  334. rChangeCb \
  335. } \
  336. } rBOIL_END
  337. #define SNIP \
  338. while(*msg && *msg!='/') ++msg; \
  339. msg = *msg ? msg+1 : msg;
  340. #define rRecurCb(name) rBOIL_BEGIN \
  341. data.obj = &obj->name; \
  342. SNIP \
  343. decltype(obj->name)::ports.dispatch(msg, data); \
  344. rBOIL_END
  345. #define rRecurPtrCb(name) rBOIL_BEGIN \
  346. void *ptr = &obj->name; \
  347. data.reply(loc, "b", sizeof(void*), &ptr); \
  348. rBOIL_END
  349. #define rRecurpCb(name) rBOIL_BEGIN \
  350. data.obj = obj->name; \
  351. if(obj->name == NULL) return; \
  352. SNIP \
  353. decltype(spice(rObject::name))::ports.dispatch(msg, data); \
  354. rBOIL_END
  355. #define rRecursCb(name, length) rBOILS_BEGIN \
  356. data.obj = &obj->name[idx]; \
  357. SNIP \
  358. decltype(spice(rObject::name))::ports.dispatch(msg, data); \
  359. rBOILS_END
  360. #define rRecurspCb(name) rBOILS_BEGIN \
  361. data.obj = obj->name[idx]; \
  362. SNIP \
  363. decltype(spice(rObject::name[0]))::ports.dispatch(msg, data); \
  364. rBOILS_END
  365. #define rActionCb(name) rBOIL_BEGIN obj->name(); rBOIL_END
  366. #define rActioniCb(name) rBOIL_BEGIN \
  367. obj->name(rtosc_argument(msg,0).i); rBOIL_END
  368. //Array ops
  369. #define rBOILS_BEGIN rBOIL_BEGIN \
  370. const char *mm = msg; \
  371. while(*mm && !isdigit(*mm)) ++mm; \
  372. unsigned idx = atoi(mm);
  373. #define rBOILS_END rBOIL_END
  374. #define rArrayCb(name) rBOILS_BEGIN \
  375. if(!strcmp("", args)) {\
  376. data.reply(loc, "c", obj->name[idx]); \
  377. } else { \
  378. char var = rtosc_argument(msg, 0).i; \
  379. rLIMIT(var, atoi) \
  380. rAPPLY(name[idx], c) \
  381. data.broadcast(loc, "c", obj->name[idx]);\
  382. rChangeCb \
  383. } rBOILS_END
  384. #define rArrayFCb(name) rBOILS_BEGIN \
  385. if(!strcmp("", args)) {\
  386. data.reply(loc, "f", obj->name[idx]); \
  387. } else { \
  388. float var = rtosc_argument(msg, 0).f; \
  389. rLIMIT(var, atof) \
  390. rAPPLY(name[idx], f) \
  391. data.broadcast(loc, "f", obj->name[idx]);\
  392. } rBOILS_END
  393. #define rArrayTCb(name) rBOILS_BEGIN \
  394. if(!strcmp("", args)) {\
  395. data.reply(loc, obj->name[idx] ? "T" : "F"); \
  396. } else { \
  397. if(obj->name[idx] != rtosc_argument(msg, 0).T) { \
  398. data.broadcast(loc, args);\
  399. rChangeCb \
  400. } \
  401. obj->name[idx] = rtosc_argument(msg, 0).T; \
  402. } rBOILS_END
  403. #define rArrayTCbMember(name, member) rBOILS_BEGIN \
  404. if(!strcmp("", args)) {\
  405. data.reply(loc, obj->name[idx].member ? "T" : "F"); \
  406. } else { \
  407. if(obj->name[idx].member != rtosc_argument(msg, 0).T) { \
  408. data.broadcast(loc, args);\
  409. rChangeCb \
  410. } \
  411. obj->name[idx].member = rtosc_argument(msg, 0).T; \
  412. } rBOILS_END
  413. #define rArrayICb(name) rBOILS_BEGIN \
  414. if(!strcmp("", args)) {\
  415. data.reply(loc, "i", obj->name[idx]); \
  416. } else { \
  417. char var = rtosc_argument(msg, 0).i; \
  418. rLIMIT(var, atoi) \
  419. rAPPLY(name[idx], i) \
  420. data.broadcast(loc, "i", obj->name[idx]);\
  421. rChangeCb \
  422. } rBOILS_END
  423. #define rArrayOptionCb(name) rBOILS_BEGIN \
  424. rOptionCb_(name[idx]) \
  425. rBOILS_END
  426. #define rParamsCb(name, length) rBOIL_BEGIN \
  427. data.reply(loc, "b", length, obj->name); rBOIL_END
  428. #define rStringCb(name, length) rBOIL_BEGIN \
  429. if(!strcmp("", args)) {\
  430. data.reply(loc, "s", obj->name); \
  431. } else { \
  432. strncpy(obj->name, rtosc_argument(msg, 0).s, length-1); \
  433. obj->name[length-1] = '\0'; \
  434. data.broadcast(loc, "s", obj->name);\
  435. rChangeCb \
  436. } rBOIL_END
  437. #endif