static Pothos::Object @class_name@Factory(const Pothos::Object *args, const size_t numArgs) { //create python environment auto env = Pothos::ProxyEnvironment::make("python"); //convert arguments into proxy environment std::vector<Pothos::Proxy> proxyArgs(numArgs); for (size_t i = 0; i < numArgs; i++) { proxyArgs[i] = env->makeProxy(args[i]); } //locate the module auto mod = env->findProxy("@package_name@"); //call into the factory auto block = mod.getHandle()->call("@class_name@", proxyArgs.data(), proxyArgs.size()); return Pothos::Object(block); }
/*********************************************************************** * make topology from JSON string - implementation **********************************************************************/ std::shared_ptr<Pothos::Topology> Pothos::Topology::make(const std::string &json) { //parse the json string/file to a JSON object const auto topObj = parseJSONStr(json); //create the proxy environment (local) and the registry auto env = Pothos::ProxyEnvironment::make("managed"); auto registry = env->findProxy("Pothos/BlockRegistry"); auto evaluator = env->findProxy("Pothos/Util/EvalEnvironment").callProxy("make"); //create thread pools std::map<std::string, Pothos::Proxy> threadPools; Poco::JSON::Object::Ptr threadPoolObj; if (topObj->isObject("threadPools")) threadPoolObj = topObj->getObject("threadPools"); std::vector<std::string> threadPoolNames; if (threadPoolObj) threadPoolObj->getNames(threadPoolNames); for (const auto &name : threadPoolNames) { std::stringstream ss; threadPoolObj->getObject(name)->stringify(ss); Pothos::ThreadPoolArgs args(ss.str()); threadPools[name] = env->findProxy("Pothos/ThreadPool").callProxy("new", args); } //create the topology and add it to the blocks //the IDs 'self', 'this', and '' can be used std::map<std::string, Pothos::Proxy> blocks; auto topology = Pothos::Topology::make(); blocks["self"] = env->makeProxy(topology); blocks["this"] = env->makeProxy(topology); blocks[""] = env->makeProxy(topology); //create the blocks Poco::JSON::Array::Ptr blockArray; if (topObj->isArray("blocks")) blockArray = topObj->getArray("blocks"); if (blockArray) for (size_t i = 0; i < blockArray->size(); i++) { if (not blockArray->isObject(i)) throw Pothos::DataFormatException( "Pothos::Topology::make()", "blocks["+std::to_string(i)+"] must be an object"); const auto &blockObj = blockArray->getObject(i); if (not blockObj->has("id")) throw Pothos::DataFormatException( "Pothos::Topology::make()", "blocks["+std::to_string(i)+"] missing 'id' field"); const auto id = blockObj->getValue<std::string>("id"); blocks[id] = makeBlock(registry, evaluator, blockObj); //set the thread pool const auto threadPoolName = blockObj->optValue<std::string>("threadPool", "default"); auto threadPoolIt = threadPools.find(threadPoolName); if (threadPoolIt != threadPools.end()) blocks[id].callVoid("setThreadPool", threadPoolIt->second); else if (threadPoolName != "default") throw Pothos::DataFormatException( "Pothos::Topology::make()", "blocks["+id+"] unknown threadPool = " + threadPoolName); } //create the topology and connect the blocks Poco::JSON::Array::Ptr connArray; if (topObj->isArray("connections")) connArray = topObj->getArray("connections"); if (connArray) for (size_t i = 0; i < connArray->size(); i++) { if (not connArray->isArray(i)) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] must be an array"); const auto &connArgs = connArray->getArray(i); if (connArgs->size() != 4) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] must be size 4"); //extract connection arg fields const auto srcId = connArgs->getElement<std::string>(0); const auto srcPort = connArgs->get(1).toString(); const auto dstId = connArgs->getElement<std::string>(2); const auto dstPort = connArgs->get(3).toString(); //check that the block IDs exist if (blocks.count(srcId) == 0) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] no such ID: " + srcId); if (blocks.count(dstId) == 0) throw Pothos::DataFormatException( "Pothos::Topology::make()", "connections["+std::to_string(i)+"] no such ID: " + dstId); //make the connection topology->connect(blocks.at(srcId), srcPort, blocks.at(dstId), dstPort); } return topology; }
#include <Pothos/Testing.hpp> #include <Pothos/Proxy.hpp> #include <iostream> #include <vector> #include <cstdlib> POTHOS_TEST_BLOCK("/proxy/java/tests", test_basic_types) { auto env = Pothos::ProxyEnvironment::make("java"); auto noneProxy = env->convertObjectToProxy(Pothos::Object()); auto nullObject = env->convertProxyToObject(noneProxy); POTHOS_TEST_TRUE(not nullObject); POTHOS_TEST_EQUAL(env->makeProxy(true).convert<bool>(), true); POTHOS_TEST_EQUAL(env->makeProxy(false).convert<bool>(), false); const char charVal = char((std::rand()-RAND_MAX/2)/(RAND_MAX >> 8)); POTHOS_TEST_EQUAL(env->makeProxy(charVal).convert<char>(), charVal); const short shortVal = short((std::rand()-RAND_MAX/2)); POTHOS_TEST_EQUAL(env->makeProxy(shortVal).convert<short>(), shortVal); const int intVal = int(std::rand()-RAND_MAX/2); POTHOS_TEST_EQUAL(env->makeProxy(intVal).convert<int>(), intVal); const long longVal = long(std::rand()-RAND_MAX/2); POTHOS_TEST_EQUAL(env->makeProxy(longVal).convert<long>(), longVal); const long long llongVal = (long long)(std::rand()-RAND_MAX/2);