From 16ec769fd0246ec446ad13903c1701d5becb545e Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 4 Sep 2022 23:28:55 +0100 Subject: added new opcode, updated doc --- README.md | 10 ++++++++++ examples/example5.csd | 3 ++- src/opcodes.cpp | 44 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f59c453..f62d016 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Some examples are provided in the examples directory. ## Opcode reference + ### jsonloads Parse a JSON string and load to an object handle for use in other opcodes. @@ -74,6 +75,7 @@ Output a JSON object handle to a text file. * **Sfile** file path to write serialised object contents to * **ipretty** 1=pretty print with formatting and indenting, 0=raw + ### jsoninit Initialise an empty JSON object (equivalent to `iJson jsonloads "{}"`). @@ -81,6 +83,13 @@ Initialise an empty JSON object (equivalent to `iJson jsonloads "{}"`). * **iJson** new empty object +### jsondestroy +Delete a JSON object and free memory. + + jsondestroy iJson +* **iJson** JSON object handle to destroy + + ### jsonmerge Shallow merge two JSON object handles, from *iJsonSource* into *iJsonTarget*. If *iupdate* = 1, then any existing keys will be altered, otherwise existing keys will not be merged. @@ -289,6 +298,7 @@ Add a value at a location specified by the JSON Pointer expression *Spointer*. * **kvalue** numeric value to add * **Svalue** string value to add + ### jsonptradd Add a JSON object handle to a location specified by the JSON Pointer expression *Spointer*. diff --git a/examples/example5.csd b/examples/example5.csd index 4ba6d07..3c3f9ba 100644 --- a/examples/example5.csd +++ b/examples/example5.csd @@ -15,6 +15,7 @@ -d -m0 +-odac sr = 44100 @@ -115,7 +116,7 @@ endin -f1 0 64 10 1 ; sine +f1 0 16 10 1 ; sine i"boot" 0 15 \ No newline at end of file diff --git a/src/opcodes.cpp b/src/opcodes.cpp index 6991051..ab3cd91 100644 --- a/src/opcodes.cpp +++ b/src/opcodes.cpp @@ -30,13 +30,15 @@ #define ARGT static constexpr char const -struct JSONSession { - jsoncons::json data; -}; const char* badHandle = "cannot obtain data from handle"; +const char* deadHandle = "object has been destroyed"; const char* handleName = "jsonsession"; +struct JSONSession { + jsoncons::json data; + bool active; +}; /* Initialise an array and return STRINDAT pointer in case it is required @@ -132,11 +134,13 @@ int getJsonType(JSONSession* jsonSession) { if (!(jsonSession = getHandle(csound, idInArgs[0], handleName))) {\ throw std::runtime_error(badHandle);\ }\ + if (!jsonSession->active) throw std::runtime_error(deadHandle);\ }\ void getSession(MYFLT handle, JSONSession** returnSession) {\ if (!(*returnSession = getHandle(csound, handle, handleName))) {\ throw std::runtime_error(badHandle);\ }\ + if (!jsonSession->active) throw std::runtime_error(deadHandle);\ } #define _PLUGINITBASE(votypes, vitypes, doGetSession) \ @@ -224,6 +228,7 @@ struct jsonloads : plugin<1, 1> { PLUGINIT("i", "S", false) void irun() { outargs[0] = createHandle(csound, &jsonSession, handleName); + jsonSession->active = true; jsonSession->data = jsoncons::json::parse(std::string(inargs.str_data(0).data)); } }; @@ -236,6 +241,7 @@ struct jsoninit : plugin<1, 0> { PLUGINIT("i", "", false) void irun() { outargs[0] = createHandle(csound, &jsonSession, handleName); + jsonSession->active = true; jsonSession->data = jsoncons::json::parse("{}"); } }; @@ -539,6 +545,7 @@ struct jsongetvalString : plugin<1, 2> { jsoncons::json selected = jsonSession->data[std::string(input.data)]; JSONSession* jsonSessionOutput; outargs[0] = createHandle(csound, &jsonSessionOutput, handleName); + jsonSessionOutput->active = true; jsonSessionOutput->data = selected; } }; @@ -553,6 +560,7 @@ struct jsongetvalNumeric : plugin<1, 2> { jsoncons::json selected = jsonSession->data[(int) inargs[1]]; JSONSession* jsonSessionOutput; outargs[0] = createHandle(csound, &jsonSessionOutput, handleName); + jsonSessionOutput->active = true; jsonSessionOutput->data = selected; } }; @@ -569,6 +577,7 @@ struct jsonpath : plugin<1, 2> { ); JSONSession* jsonSessionOutput; outargs[0] = createHandle(csound, &jsonSessionOutput, handleName); + jsonSessionOutput->active = true; jsonSessionOutput->data = queried; } }; @@ -642,6 +651,7 @@ struct jsonptr : plugin<1, 2> { ); JSONSession* jsonSessionOutput; outargs[0] = createHandle(csound, &jsonSessionOutput, handleName); + jsonSessionOutput->active = true; jsonSessionOutput->data = queried; } }; @@ -850,6 +860,7 @@ struct jsonarr : plugin<1, 1> { MYFLT handle; for (std::size_t index = 0; index < vals.size(); index++) { handle = createHandle(csound, &jsonSession2, handleName); + jsonSession2->active = true; jsonSession2->data = vals[index]; array->data[index] = handle; } @@ -882,20 +893,37 @@ struct jsondumpsK : jsondumpsBase { }; -struct jsonload : csnd::Plugin<1, 1> { - PLUGINSESSION +/* + Destroy object and clear memory + */ +struct jsondestroy : inplug<1> { + INPLUGINIT("i") + void irun() { + jsonSession->data.~basic_json(); + jsonSession->active = false; + } +}; + + +/* + Load from file + */ +struct jsonload : plugin<1, 1> { PLUGINIT("i", "S", false) void irun() { std::ifstream fileStream(inargs.str_data(0).data); jsoncons::json parsed = jsoncons::json::parse(fileStream); outargs[0] = createHandle(csound, &jsonSession, handleName); + jsonSession->active = true; jsonSession->data = parsed; } }; -struct jsondump : csnd::InPlug<3> { - INPLUGSESSION +/* + Serialise to file + */ +struct jsondump : inplug<3> { INPLUGINIT("iSp") void irun() { std::ofstream fileStream; @@ -912,11 +940,13 @@ struct jsondump : csnd::InPlug<3> { } }; + #include void csnd::on_load(csnd::Csound *csound) { csnd::plugin(csound, "jsoninit", csnd::thread::i); csnd::plugin(csound, "jsonloads", csnd::thread::i); csnd::plugin(csound, "jsondumps", csnd::thread::i); + csnd::plugin(csound, "jsondestroy", csnd::thread::i); csnd::plugin(csound, "jsondumpsk", csnd::thread::ik); csnd::plugin(csound, "jsonload", csnd::thread::i); csnd::plugin(csound, "jsondump", csnd::thread::i); -- cgit v1.2.3