首页 > 代码库 > C++调用JS,可变參模板实现,方便调用(cocos2d-x)
C++调用JS,可变參模板实现,方便调用(cocos2d-x)
#include "scripting/js-bindings/manual/ScriptingCore.h" #include <sstream> #include <type_traits> /// args push helper template<typename _Ty> inline void jsx_unpack_arg(std::stringstream& ss, int& carg, const _Ty& arg) { // template adapter: must be numeric type static_assert(std::is_integral<_Ty>::value || std::is_floating_point<_Ty>::value, "error, jsx_unpack: unsupported type!"); ss << arg << ","; ++carg; } inline void jsx_unpack_arg(std::stringstream& ss, int& /*carg*/) { // empty arg } inline void jsx_unpack_arg(std::stringstream& ss, int& carg, const char* arg) { // numbers ss << "\"" << arg << "\"" << ","; ++carg; } inline void jsx_unpack_arg(std::stringstream& ss, int& carg, const std::string& arg) { ss << "\"" << arg << "\"" << ","; ++carg; } template<typename _Ty, typename..._Args> inline void jsx_unpack_arg(std::stringstream& ss, int& carg, const _Ty& arg1, const _Args&...args) { jsx_unpack_arg(ss, carg, arg1); jsx_unpack_arg(ss, carg, args...); } template<typename..._Args> inline void jsx_vcall(const char* func, const _Args&...args) { std::stringstream sstreamArgs; sstreamArgs.precision(16); sstreamArgs << func << "("; int carg = 0; jsx_unpack_arg(sstreamArgs, carg, args...); auto actrualArgs = sstreamArgs.str(); if (carg != 0) actrualArgs.pop_back(); // pop last ‘,‘ actrualArgs.append(");"); jsval v1; if(!ScriptingCore::getInstance()->evalString(actrualArgs.c_str(), &v1)) { cocos2d::log("jsx_vcall call func:%s failed, please make sure config your project.json properly!"); } }
C++调用JS,可变參模板实现,方便调用(cocos2d-x)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。