C + + call JS, variable template implementation, easy to call (Cocos2d-x)

Source: Internet
Author: User

#include "scripting/js-bindings/manual/scriptingcore.h" #include <sstream> #include <type_traits>/// Args push helpertemplate<typename _ty> inlinevoid 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_flo    Ating_point<_ty>::value, "error, jsx_unpack:unsupported type!");    SS << Arg << ","; ++carg;} Inlinevoid Jsx_unpack_arg (std::stringstream& SS, int&/*carg*/) {//Empty arg}inlinevoid Jsx_unpack_arg (std:: stringstream& SS, int& carg, const char* arg) {//numbers SS << "\" "<< arg <<" \ "" <<    ","; ++carg;} Inlinevoid Jsx_unpack_arg (std::stringstream& SS, int& carg, const std::string& ARG) {ss << "\" "<& Lt    Arg << "\" "<<", "; ++carg;} Template<typename _ty, typename..._args> inlinevoid 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> inlinevoid jsx_vcall (const char* func, const _args&...args) {Std::stringstream ss    Treamargs;        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 F Ailed, please make sure config your Project.json properly! ");}

C + + call JS, variable template implementation, easy to call (Cocos2d-x)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.