-
Notifications
You must be signed in to change notification settings - Fork 5
/
PythonBlock.cpp
68 lines (56 loc) · 1.74 KB
/
PythonBlock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2014-2021 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include <Pothos/Managed.hpp>
#include <Pothos/Proxy.hpp>
class PythonBlock : Pothos::Block
{
public:
PythonBlock(void)
{
this->registerCall(this, POTHOS_FCN_TUPLE(PythonBlock, _setPyBlock));
}
static Block *make(void)
{
return new PythonBlock();
}
void _setPyBlock(const Pothos::Proxy &block)
{
_block = block;
}
void work(void)
{
_block.call("work");
}
void activate(void)
{
_block.call("activate");
}
void deactivate(void)
{
_block.call("deactivate");
}
void propagateLabels(const Pothos::InputPort *input)
{
//forward to wrapper that takes input port name
auto not_implemeneted = _block.call<bool>("_propagateLabels", input->name());
//if the overload was not implemented, call base function
if (not_implemeneted) Pothos::Block::propagateLabels(input);
}
Pothos::Object opaqueCallHandler(const std::string &name, const Pothos::Object *inputArgs, const size_t numArgs)
{
if (name == "_setPyBlock") return Pothos::Block::opaqueCallHandler(name, inputArgs, numArgs);
if (not _block) throw name;
auto env = _block.getEnvironment();
Pothos::ProxyVector args(numArgs);
for (size_t i = 0; i < numArgs; i++)
{
args[i] = env->convertObjectToProxy(inputArgs[i]);
}
auto result = _block.getHandle()->call(name, args.data(), args.size());
return env->convertProxyToObject(result);
}
Pothos::Proxy _block;
};
static Pothos::BlockRegistry registerPythonBlock(
"/blocks/python_block", &PythonBlock::make);