Skip to content

Commit

Permalink
[examples] Add ashell example and a tag to detect that a process shou…
Browse files Browse the repository at this point in the history
…ld run only once
  • Loading branch information
jcelerier committed Feb 25, 2024
1 parent 0620702 commit f218047
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
58 changes: 58 additions & 0 deletions examples/Raw/Shell.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

/* SPDX-License-Identifier: GPL-3.0-or-later */

#include <functional>
#include <string>
#include <thread>
namespace examples
{
struct Shell
{
static consteval auto name() { return "Shell command"; }
static consteval auto c_name() { return "avnd_shell"; }
static consteval auto category() { return "Script"; }
static consteval auto uuid() { return "7e4ae744-1825-4f1c-9fc9-675e41f316bc"; }

// This tag is an indication that the operator() should only called on
// the first tick, not on every tick
enum
{
single_exec
};

struct
{
struct
{
static constexpr auto name() { return "Script"; }
static constexpr auto language() { return "shell"; }
enum widget
{
textedit
};

struct range
{
const std::string_view init = "#!/bin/bash\n";
};
std::string value;
} command;
} inputs;

struct
{
} outputs;

void operator()() { worker.request(std::move(inputs.command.value)); }

struct worker
{
std::function<void(std::string)> request;
static void work(std::string&& s)
{
std::thread{[code = std::move(s)] { ::system(code.c_str()); }}.detach();
}
} worker;
};
}
12 changes: 12 additions & 0 deletions include/avnd/binding/ossia/data_node.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include <avnd/binding/ossia/node.hpp>
#include <avnd/concepts/temporality.hpp>

namespace oscr
{

Expand All @@ -10,11 +12,21 @@ class safe_node<T> : public safe_node_base<T, safe_node<T>>
public:
using safe_node_base<T, safe_node<T>>::safe_node_base;

bool exec_once{};

constexpr bool scan_audio_input_channels() { return false; }

OSSIA_MAXIMUM_INLINE
void run(const ossia::token_request& tk, ossia::exec_state_facade st) noexcept override
{
if constexpr(avnd::tag_single_exec<T>)
{
if(std::exchange(exec_once, true))
{
return;
}
}

auto [start, frames] = st.timings(tk);

if(!this->prepare_run(tk, start, frames))
Expand Down
18 changes: 18 additions & 0 deletions include/avnd/concepts/temporality.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

/* SPDX-License-Identifier: GPL-3.0-or-later OR BSL-1.0 OR CC0-1.0 OR CC-PDCC OR 0BSD */

#include <avnd/common/concepts_polyfill.hpp>
#include <avnd/common/enums.hpp>
#include <avnd/concepts/generic.hpp>

namespace avnd
{
/**
* @brief single_exec tag: the 'process' method should only be called once
* per execution.
*
* Example: in ossia, this means that the processor can be assigned to a state.
*/
AVND_DEFINE_TAG(single_exec)
}

0 comments on commit f218047

Please sign in to comment.