Perlã®XSããPerlã®é¢æ°ãç°¡åã«å¼ã³åºããããã«ãã¦ã¿ã (C++ã§)
Perlã®è¬APIãwrapãã¦ãPerlã¹ã¯ãªããã®é¢æ°ãC++ããç°¡åã«å¼ã³åºããããã«ãã¦ã¿ãã
ã¦ãã
- ãªãã¡ã¬ã³ã¹ã«ã¦ã³ããééçã«è¡ããããpimplå½¢å¼ã®ã³ã³ããã使ã£ã¦ã¿ã
- mortalãééçã«è¡ããããcall_sv()ã«ããé¢æ°å¼ã³åºããå®å ¨ã«ãã©ãã¯ããã¯ã¹åãã
ã£ã¦å®é¨ãããããã£ãã ããªã®ã«Devel::BindPPã¨ã¯å®å ¨ã«å¥ãããã¯ãã«ãªã£ã¡ããã¾ããã
test1() ã¯ãä»»æã®æ°ã®å¼æ°ãåããå¼æ°ã®åè¨å¤ãè¿ã add()ã¨ããé¢æ° *1 ã C++ ããå¼ã³åºãä¾ã
test2() ã¯ãMIME::Base64::encode_base64() ãå¼ã³åºãä¾ã
test3() ã¯ãè¤æ°åã®å¤ãè¿ãé¢æ°ãå¦çããä¾ã
test4() ã¯ãperl::value() ã®æ¼ç®åãªã¼ããã¼ãã®ãã¹ãã
#include <iostream> #include "perlcall.hpp" XS(test1) { perl::function<IV> add($.function<IV>("add")); $.cout() << (add(1) == 1 ? "ok": "not ok") << " 1" << std::endl; $.cout() << (add(1, 2) == 3 ? "ok": "not ok") << " 2" << std::endl; $.cout() << (add(1, 2, 3) == 6 ? "ok": "not ok") << " 3" << std::endl; $.cout() << (add(1, 2, 3, 4) == 10 ? "ok": "not ok") << " 4" << std::endl; $.cout() << (add(1, 2, 3, 4, 5) == 15 ? "ok": "not ok") << " 5" << std::endl ; { dXSARGS; XSRETURN(0); } } XS(test2) { perl::function<std::string> encode_base64( $.function<std::string>( std::make_pair("MIME::Base64", "encode_base64"))); $.cout() << (encode_base64("test") == "dGVzdA==\n" ? "ok": "not ok") << " 6" << std::endl; { dXSARGS; XSRETURN(0); } } XS(test3) { perl::function<perl::array> tuple($.function<perl::array>("tuple")); perl::array r = tuple(1); $.cout() << (r[0] == $.value("1") ? "ok": "not ok") << " 7" << std::endl; $.cout() << (r[1] == $.value("2") ? "ok": "not ok") << " 8" << std::endl; $.cout() << (r[2] == $.value("3") ? "ok": "not ok") << " 9" << std::endl; { dXSARGS; XSRETURN(0); } } XS(test4) { $.cout() << ($.value(1) + $.value(2) == $.value(3) ? "ok": "not ok") << " 10 " << std::endl; { dXSARGS; XSRETURN(0); } } ACME_MOZO_PERL_BOOTSTRAP(Acme__Mozo__QuickXS) { ACME_MOZO_PERL_FUNC("test1", &test1); ACME_MOZO_PERL_FUNC("test2", &test2); ACME_MOZO_PERL_FUNC("test3", &test3); ACME_MOZO_PERL_FUNC("test4", &test4); return true; }
ã½ã¼ã¹ã¯ãªããGitHubã«ç½®ãã¦ããã¾ãã
è¿½è¨ è£è¶³ãã¨ãã¨ããã®ã³ã¼ãã¯æä½ã® Boost.PHP ã® Perl ã¸ã®ç§»æ¤ãªã®ã§ãæ£ç¢ºã«ã¯äºçªç ãã£ã¦ããã§ã¯ãªãã§ãã念ã®ããã
*1:åãã¯å¼æ°ã2ã¤ããã¨ã£ã¦ãªãã£ãã®ã§addã£ã¦ååã«ãªã£ã¦ããã©ãæ¬æ¥ã¯sumã®ã»ãããã£ãããã