{"id":4343,"date":"2020-06-12T15:57:06","date_gmt":"2020-06-12T07:57:06","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4343"},"modified":"2020-06-12T15:57:06","modified_gmt":"2020-06-12T07:57:06","slug":"sol2%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4343","title":{"rendered":"Sol2\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"<header class=\"post-header\">\n<p class=\"post-title\"><a href=\"http:\/\/aiplay.github.io\/2016\/08\/09\/2016-08-09-sol2-tutorail\/#more\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<div class=\"post-meta\"><span class=\"post-time\"><span class=\"post-meta-item-icon\"><i class=\"fa fa-calendar-o\"><\/i>\u00a0<\/span><span class=\"post-meta-item-text\">\u53d1\u8868\u4e8e<\/span>\u00a0<time title=\"\u521b\u5efa\u65f6\u95f4\uff1a2016-08-09 00:00:00\" datetime=\"2016-08-09T00:00:00+08:00\">2016-08-09<\/time>\u00a0<span class=\"post-meta-divider\">|<\/span>\u00a0<span class=\"post-meta-item-icon\"><i class=\"fa fa-calendar-check-o\"><\/i>\u00a0<\/span><span class=\"post-meta-item-text\">\u66f4\u65b0\u4e8e<\/span>\u00a0<time title=\"\u4fee\u6539\u65f6\u95f4\uff1a2018-08-21 16:10:46\" datetime=\"2018-08-21T16:10:46+08:00\">2018-08-21<\/time>\u00a0<\/span><span class=\"post-category\"><span class=\"post-meta-divider\">|<\/span>\u00a0<span class=\"post-meta-item-icon\"><i class=\"fa fa-folder-o\"><\/i>\u00a0<\/span><span class=\"post-meta-item-text\">\u5206\u7c7b\u4e8e<\/span>\u00a0<a href=\"http:\/\/aiplay.github.io\/categories\/Lua\/\" rel=\"index\">Lua<\/a>\u00a0<\/span><span class=\"post-comments-count\"><span class=\"post-meta-divider\">|<\/span>\u00a0<span class=\"post-meta-item-icon\"><i class=\"fa fa-comment-o\"><\/i>\u00a0<\/span><a href=\"http:\/\/aiplay.github.io\/2016\/08\/09\/2016-08-09-sol2-tutorail\/#comments\"><span class=\"post-comments-count disqus-comment-count\" data-disqus-identifier=\"2016\/08\/09\/2016-08-09-sol2-tutorail\/\">0 Comments<\/span><\/a><\/span><\/div>\n<\/header>\n<div class=\"post-body\">\n<h3 id=\"Sol2\u7b80\u4ecb\">Sol2\u7b80\u4ecb<\/h3>\n<p>Sol\u662f\u4e00\u4e2a\u7528\u4e8eC++\u7ed1\u5b9aLua\u811a\u672c\u7684\u5e93\uff0c\u4ec5\u7531\u5934\u6587\u4ef6\u7ec4\u6210\uff0c\u65b9\u4fbf\u96c6\u6210\uff0c\u5e76\u63d0\u4f9b\u4e86\u5927\u91cf\u6613\u7528\u7684API\u63a5\u53e3\uff0c\u53ef\u4ee5\u4fbf\u5229\u5730\u5c06Lua\u811a\u672c\u4e0eC++\u4ee3\u7801\u7ed1\u5b9a\u8d77\u6765\uff0c\u800c\u4e0d\u5fc5\u53bb\u5173\u5fc3\u5982\u4f55\u4f7f\u7528\u90a3\u4e9b\u6666\u6da9\u7684Lua C API\u3002\u6b63\u5982\u5176\u4f5c\u8005\u6240\u8a00\uff0cSol\u7684\u76ee\u7684\u5c31\u662f\u63d0\u4f9b\u6781\u5176\u7b80\u6d01\u7684API\uff0c\u5e76\u80fd\u9ad8\u6548\u5230\u4e0eC\u8bed\u8a00\u5ab2\u7f8e\uff0c\u6781\u5927\u5730\u6765\u65b9\u4fbf\u4eba\u4eec\u4f7f\u7528\u3002<\/p>\n<p>Sol\u652f\u6301Lua\u7684\u7edd\u5927\u591a\u6570\u7248\u672c\uff0c\u5305\u62ec 5.1\u30015.2\u30015.3\u548cLuaJit\u7b49\uff0c\u4f46\u7531\u4e8e\u4ee3\u7801\u4e2d\u7528\u5230\u4e86\u8bb8\u591aC++11\/14\u7279\u6027\uff0c\u56e0\u6b64\u7f16\u8bd1\u65f6\u9700\u8981\u7f16\u8bd1\u5668\u652f\u6301C++14\u6807\u51c6\u3002<\/p>\n<p>\u5177\u4f53\u4f7f\u7528\u4ee5\u53ca\u4ecb\u7ecd\u8be6\u60c5\uff0c\u8bf7\u524d\u5f80<a href=\"https:\/\/github.com\/ThePhD\/sol2\" target=\"_blank\" rel=\"noopener noreferrer\">Github\u4ed3\u5e93<\/a>\u548c<a href=\"http:\/\/sol2.readthedocs.io\/en\/latest\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">Sol2\u4e3b\u9875<\/a>\u3002<\/p>\n<h3 id=\"\u57fa\u7840\u4f7f\u7528\">\u57fa\u7840\u4f7f\u7528<\/h3>\n<p>\u4eceSol\u7684<a href=\"https:\/\/github.com\/ThePhD\/sol2\" target=\"_blank\" rel=\"noopener noreferrer\">Github\u4ed3\u5e93<\/a>clone\u4e0b\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u5176\u76ee\u5f55\u4e0b\u5f88\u591atest\u5f00\u5934\u7684cpp\/hpp\u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u91cc\u9762\u6709\u7740\u5927\u91cf\u7684Sol\u7684\u4f7f\u7528\u793a\u4f8b\u4ee5\u53ca\u5404\u79cd\u7279\u6027\u7684\u5c55\u793a\uff0c\u800c\u5728example\u76ee\u5f55\u4e0b\u7684cpp\u6587\u4ef6\u90fd\u4ec5\u4ec5\u662f\u4e00\u4e9b\u6700\u57fa\u7840\u7684\u4f7f\u7528\u793a\u4f8b\u3002\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5\u548c\u4f53\u9a8cSol\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5efa\u7acb\u4e00\u4e9b\u81ea\u5df1\u7684test.cpp\u6587\u4ef6\uff0c\u9996\u5148\u4f60\u8981\u5728\u6e90\u6587\u4ef6\u4e2dinclude\u5f15\u7528sol.hpp\u5934\u6587\u4ef6\uff0c\u8fd9\u6837\u624d\u80fd\u4f7f\u7528Sol\u63d0\u4f9b\u7684\u63a5\u53e3\u3002\u800c\u5728\u4f7f\u7528gcc\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u9700\u8981\u6307\u5b9a\u5173\u8054\u5934\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>g++ test.cpp -Isolpath\/single\/sol -llua -std=c++14\r\n<\/code><\/pre>\n<p>\u5176\u4e2dsolpath\u662f\u4f60Sol2\u7684\u5177\u4f53\u8def\u5f84\uff0c\u5728Sol2\u7684\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u6709\u4e00\u4e2asingle\/sol\/sol.hpp\u5934\u6587\u4ef6\uff0c\u8fd9\u4e2a\u5934\u6587\u4ef6\u96c6\u6210\u4e86\u6240\u6709\u7684\u76f8\u5173\u4ee3\u7801\u5230\u4e00\u8d77\uff0c\u6240\u4ee5\u7f16\u8bd1\u65f6 -I \u540e\u4ec5\u6307\u5b9a\u8fd9\u4e00\u4e2a\u8def\u5f84\u5c31\u53ef\u4ee5\u4e86\uff0c\u540c\u65f6\u8981\u4fdd\u8bc1\u4f60\u7684gcc\u7f16\u8bd1\u5668\u652f\u6301C++14\u6807\u51c6\u3002<\/p>\n<p><a id=\"more\" class=\"active\"><\/a><\/p>\n<h4 id=\"\u7ed1\u5b9a\u7b2c\u4e00\u4e2a\u7c7b\">\u7ed1\u5b9a\u7b2c\u4e00\u4e2a\u7c7b<\/h4>\n<pre><code>#include \"stdio.h\"\r\n#include \"sol.hpp\"\r\n\r\nclass A\r\n{\r\npublic:\r\n    A () = default;\r\n    A (int a) { mA = a; };\r\n    virtual ~A() = default;\r\n    void foo()\r\n    {\r\n        printf(\"foo excute!\\n\");\r\n    }\r\nprivate:\r\n    int mA;\r\n}\r\n\r\nint main(void)\r\n{\r\n    sol::state lua;\r\n    \/\/ \u6253\u5f00lua\u4e2d\u7684\u76f8\u5173\u5e93\uff0c\u5982table\u3001string\u7b49\r\n    lua.open_libraries();\r\n    lua.new_usertype&lt;A&gt;\r\n    (\r\n        \"A\", \/\/ Lua\u4e2d\u8bc6\u522b\u7684\u7c7b\u540d\r\n        sol::constructors&lt;sol::types&lt;int&gt;&gt;(),\r\n        \"foo\", &amp;A::foo \/\/ \u7ed1\u5b9a\u6210\u5458\u51fd\u6570foo\r\n    )\r\n\r\n    \/\/ \u6267\u884c\u4e00\u6bb5Lua\u7a0b\u5e8f\r\n    lua.script(\" local a = A.new(); a:foo(); \")\r\n}\r\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5e8f\u4e2d\uff0c\u5c06\u4e00\u4e2a\u7b80\u5355\u7684\u7c7bA\u7ed1\u5b9a\u5230\u4e86Lua\u4ee3\u7801\u4e2d\uff0c\u5728Lua\u4ee3\u7801\u4e2d\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aa\u5bf9\u8c61\u5e76\u8c03\u7528\u5176\u6210\u5458\u51fd\u6570foo()\u3002\u5176\u4e2d\uff0cA\u7c7b\u9664\u4e86\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\uff0c\u9700\u8981\u4f7f\u7528sol::constructors\u8fdb\u884c\u5c01\u88c5\uff0c\u5e76\u901a\u8fc7sol::types&lt;&gt;\u6a21\u677f\u6765\u6307\u5b9a\u5176\u53c2\u6570\u5217\u8868\u3002<\/p>\n<h4 id=\"\u8bbe\u7f6e\u5168\u5c40\u51fd\u6570\">\u8bbe\u7f6e\u5168\u5c40\u51fd\u6570<\/h4>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2aC++\u7684\u51fd\u6570\u8bbe\u7f6e\u5230Lua\u4e2d\u4f5c\u4e3a\u5168\u5c40\u51fd\u6570\u8c03\u7528\uff0c\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code>sol::state lua;\r\nlua.set_function(\"func\", func); \/\/ func\u53d8\u91cf\u4e3aC++\u51fd\u6570\u5730\u5740\r\n<\/code><\/pre>\n<h4 id=\"\u91cd\u8f7d\u51fd\u6570\u7ed1\u5b9a\">\u91cd\u8f7d\u51fd\u6570\u7ed1\u5b9a<\/h4>\n<p>\u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u7684\u67d0\u4e2a\u6210\u5458\u51fd\u6570\u88ab\u91cd\u8f7d\u4e86\uff0c\u5373\u51fd\u6570\u540d\u53ca\u8fd4\u56de\u503c\u76f8\u540c\uff0c\u53c2\u6570\u5217\u8868\u4e0d\u540c\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u7ed1\u5b9a:<\/p>\n<pre><code>sol::state lua;\r\nlua.new_usertype&lt;A&gt;\r\n(\r\n    \"A\", \/\/ sol\u4f1a\u5e2e\u4f60\u6784\u9020\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\r\n    \"foo\", sol::overload(sol::resolve&lt;void(int)&gt;(&amp;A::foo),\r\n        sol::overload(sol::resolve&lt;void(int, int)&gt;(&amp;A::foo)))\r\n)\r\n<\/code><\/pre>\n<p>\u5047\u8bbeA\u7c7b\u5305\u542b\u4e24\u4e2a\u540d\u5b57\u90fd\u53eb\u505afoo\u7684\u6210\u5458\u51fd\u6570\uff0c\u90fd\u6ca1\u6709\u8fd4\u56de\u503c(void)\uff0c\u4e00\u4e2a\u662f\u9700\u8981\u4e00\u4e2aint\u4f5c\u4e3a\u53c2\u6570\uff0c\u53e6\u4e00\u4e2a\u662f\u9700\u8981\u4e24\u4e2aint\u4f5c\u4e3a\u53c2\u6570\uff0c\u5f53Lua\u4ee3\u7801\u4e2d\u8c03\u7528foo\u51fd\u6570\u7684\u65f6\u5019\uff0cSol\u4f1a\u6839\u636e\u4f60\u4f20\u9012\u7684\u53c2\u6570\u6765\u5224\u65ad\u8c03\u7528\u5bf9\u5e94\u54ea\u4e2a\u7ed1\u5b9a\u7684C++\u51fd\u6570\u3002<\/p>\n<h4 id=\"\u5c06\u679a\u4e3e\u6620\u5c04\u5230Lua\u7684\u5168\u5c40table\u4e2d\">\u5c06\u679a\u4e3e\u6620\u5c04\u5230Lua\u7684\u5168\u5c40table\u4e2d<\/h4>\n<p>\u6709\u65f6\u5019\u5728C++\u4e2d\u4f1a\u5b9a\u4e49\u8bb8\u591a\u679a\u4e3e\uff0c\u4e3a\u4e86\u65b9\u4fbfLua\u4ee3\u7801\u4e2d\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u5bf9\u5e94\u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u7ed1\u5b9a\u5230\u4e00\u4e2aLua\u5168\u5c40table\u4e2d\u3002<\/p>\n<pre><code>enum Color\r\n{\r\n    RED,\r\n    GREEN,\r\n    BLUE\r\n};\r\nsol::state lua;\r\nlua[\"Color\"] = lua.create_table_with(\"RED\", RED, \"GREEN\", GREEN, \"BLUE\", BLUE);\r\nlua.script(\"Color.RED\")\r\n<\/code><\/pre>\n<p>\u4e0a\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9e\u9645\u4e0a\u6784\u5efa\u4e86\u4e00\u4e2a\u5168\u5c40table\uff0c\u540d\u5b57\u53eb\u505aColor\uff0c\u5e76\u5c06table\u7684key\u5bf9\u5e94\u7684value\u8bbe\u7f6e\u6210\u679a\u4e3eColor\u4e2d\u7684\u5bf9\u5e94\u9879\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5728Lua\u4f7f\u7528\u67d0\u4e2a\u679a\u4e3e\u91cc\u7684\u5185\u5bb9\u3002<\/p>\n<h3 id=\"\u8fdb\u9636\u4f7f\u7528\">\u8fdb\u9636\u4f7f\u7528<\/h3>\n<h4 id=\"\u5c06Lua\u7684table\u4f20\u5230C-\u4e2d\">\u5c06Lua\u7684table\u4f20\u5230C++\u4e2d<\/h4>\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e00\u4e2atable\u4f20\u5230C++\u5c42\u6765\u4f7f\u7528\uff0cSol\u4e3a\u6211\u4eec\u5c01\u88c5\u4e86sol::table\u7c7b\u578b\u6765\u8868\u793aLua\u4e2d\u7684table\uff0c\u5047\u8bbe\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aC++\u51fd\u6570\uff0c\u5176\u53c2\u6570\u5c31\u662fsol::table\u3002<\/p>\n<pre><code>void foo(sol::table t)\r\n{\r\n    \/\/ t \u8868\u793a\u4e00\u4e2a\u4eceLua\u5c42\u4f20\u6765\u7684table\r\n}\r\n\r\nsol::state lua;\r\nlua.set_function(\"foo\", foo);\r\n\r\nlua.script(\"local t = { 1, 2, 3, 4 }; foo(t); \")\r\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u8fb9Lua\u4ee3\u7801\u7684\u8c03\u7528\uff0c\u4f7f\u5f97\u4e00\u4e2a\u5185\u5bb9\u662f\u201c1, 2, 3, 4\u201d\u7684table\u4f20\u7ed9\u4e86C++\u5c42\u3002<\/p>\n<h4 id=\"\u904d\u5386Lua\u4f20\u6765\u7684table\">\u904d\u5386Lua\u4f20\u6765\u7684table<\/h4>\n<p>\u7531\u4e8eLua\u4e2d\u53ea\u6709table\u8fd9\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u65e2\u53ef\u4ee5\u8868\u793a\u6570\u7ec4\u4e5f\u53ef\u4ee5\u8868\u793aHashMap\u7684\u7ed3\u6784\uff0c\u56e0\u6b64\u904d\u5386\u83b7\u53d6sol::table\u4e2d\u7684\u5185\u5bb9\u663e\u5f97\u66f4\u52a0\u590d\u6742\u3002Sol\u91cd\u8f7d\u4e86[]\u8ba9\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u53bb\u83b7\u53d6table\u4e2d\u7684\u503c\uff0c\u53ef\u4ee5\u4f20\u9012\u7d22\u5f15\u6216key\u503c\u3002<\/p>\n<pre><code>void foo(sol::table t)\r\n{\r\n    \/\/ lua table { 1, 2, 3, 4 }\r\n    sol::object obj = t[1];\r\n    int first = obj.as&lt;int&gt;();  \/\/ first is 1\r\n\r\n    \/\/ lua table { a = \"a\", b = 2 }\r\n    sol::object a = t[\"a\"];\r\n    sol::object b = t[\"b\"];\r\n    sol::type atype = a.get_type(); \/\/ atype \u7b49\u4e8e sol::type::string\r\n    sol::type btype = b.get_type(); \/\/ btype \u7b49\u4e8e sol::type::number\r\n    std::string astr = a.as&lt;std::string&gt;(); \/\/ a\r\n    int bnum = b.as&lt;int&gt;();  \/\/ 2\r\n}\r\n<\/code><\/pre>\n<p>\u901a\u8fc7[]\u5f97\u5230\u7684table\u4e2d\u7684\u503c\u5e76\u4e0d\u80fd\u76f4\u63a5\u5f53\u6210number\u6216\u662fstring\u6765\u4f7f\u7528\uff0c\u5b83\u5b9e\u9645\u4e0a\u662fsol::proxy\u4ee3\u7406\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u9690\u5f0f\u8f6c\u6362\u6210sol::object\u7c7b\u578b\uff0c\u5e76\u901a\u8fc7\u00a0<strong>as&lt;&gt;()<\/strong>\u00a0\u6765\u83b7\u53d6\u5176\u5728Lua\u4e2d\u4ee3\u8868\u7684\u503c\u5bf9\u5e94\u7684C++\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e76\u4e0d\u6e05\u695a\u8fd9\u4e2aLua\u5c42\u4f20\u5165\u7684table\u662f\u600e\u6837\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u91c7\u53d6\u904d\u5386\u5e76\u5224\u65ad\u5bf9\u5e94key\u3001value\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u5f97\u5230\u6211\u4eec\u5b9e\u9645\u60f3\u8981\u7684\u503c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>void foo(sol::table t)\r\n{\r\n    for (auto&amp; kv : t)\r\n    {\r\n        sol::object key = kv.first;\r\n        sol::object val = kv.second;\r\n        printf(\"key type : %d, val type : %d\\n\", key.get_type(), val.get_type());\r\n        switch (key.get_type())\r\n        {\r\n            case sol::type::number:\r\n                \/\/ \u8fd9\u662f\u4e00\u4e2a\u6570\u7ec4\r\n                break;\r\n            case sol::type::string:\r\n                \/\/ \u952e\u503c\u5bf9table\r\n                break;\r\n        }\r\n        switch (val.get_type())\r\n        {\r\n            case sol::type::number:\r\n                break;\r\n            case sol::type::string:\r\n                break;\r\n            case sol::type::table:\r\n                break;\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<h4 id=\"\u8ba9\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\">\u8ba9\u4ee3\u7801\u66f4\u52a0\u5065\u58ee<\/h4>\n<p>\u7531\u4e8e\u5728Lua\u4e2d\u7c7b\u578b\u7684\u6982\u5ff5\u5f88\u5f31\uff0c\u51fd\u6570\u5b9a\u4e49\u7684\u53c2\u6570\u5e76\u4e0d\u4f1a\u6307\u5b9a\u5177\u4f53\u7c7b\u578b\uff0c\u8fd9\u5bfc\u81f4\u5f53\u6211\u4eec\u60f3\u8c03\u7528\u67d0\u4e2aC++\u7ed1\u5b9a\u8fc7\u6765\u7684\u63a5\u53e3\u4f20\u9012table\u7684\u65f6\u5019\uff0c\u5f88\u6709\u53ef\u80fd\u6709\u610f\u6216\u65e0\u610f\u4f20\u9012\u4e86\u4e00\u4e2anil\u8fc7\u53bb\u3002\u5982\u679c\u8be5\u51fd\u6570\u5728C++\u4e2d\u6307\u5b9a\u7684\u7c7b\u578b\u662fsol::table\uff0c\u800c\u5728Lua\u4f20\u9012\u4e00\u4e2anil\u503c\u4f1a\u53d1\u751f\u5f88\u591a\u4e0d\u53ef\u63a7\u7684\u9519\u8bef\uff0c\u56e0\u6b64\u9700\u8981\u6211\u4eec\u9488\u5bf9\u8fd9\u79cd\u60c5\u51b5\u7279\u6b8a\u5904\u7406\u4ee5\u4fdd\u8bc1\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n<p>\u65e0\u8bba\u662fsol::object\u8fd8\u662fsol::table\u3001sol::function\u7b49\u7c7b\u578b\uff0c\u90fd\u7ee7\u627f\u4e8esol::reference\u7c7b\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u5728sol::reference\u4e2d\u7684\u00a0<strong>valid()<\/strong>\u00a0\u51fd\u6570\u5373\u53ef\u5224\u65ad\u5f53\u524d\u5bf9\u8c61\u662f\u5426\u53ef\u7528\u3002sol::table\u7c7b\u578b\u4e5f\u53ef\u4ee5\u901a\u8fc7\u00a0<strong>empty()<\/strong>\u00a0\u6765\u5224\u65ad\u5f53\u524dtable\u662f\u5426\u5305\u542b\u5185\u5bb9\u3002<\/p>\n<pre><code>void foo(sol::table t)\r\n{\r\n    if (! t.valid() || t.empty())\r\n        return;\r\n    \/\/ \u6267\u884c\u5230\u8fd9\u8868\u660et\u5e76\u4e0d\u4e3anil\u6216\u662f\u7a7a\u8868\uff0c\u53ef\u4ee5\u7ee7\u7eed\u4ee3\u7801\u903b\u8f91\r\n}\r\n<\/code><\/pre>\n<h4 id=\"\u9690\u5f0f\u4f20\u9012Lua\u72b6\u6001\u673a\">\u9690\u5f0f\u4f20\u9012Lua\u72b6\u6001\u673a<\/h4>\n<p>\u867d\u7136Sol\u5e2e\u6211\u4eec\u5c01\u88c5\u4e86\u5f88\u591a\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u4f46\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u96be\u514d\u8fd8\u662f\u9700\u8981\u81ea\u5df1\u53bb\u548cLua\u72b6\u6001\u673a\u6253\u4ea4\u9053\u3002\u5728\u4e4b\u524d\u4ee3\u7801\u7ecf\u5e38\u51fa\u73b0\u7684sol::state\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u00a0<strong>lua_state()<\/strong>\u00a0\u63a5\u53e3\u6765\u83b7\u53d6lua_State\u6307\u9488\u3002<\/p>\n<p>\u540c\u65f6\uff0cSol\u5c01\u88c5\u4e86\u4e00\u4e2asol::this_state\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5176\u9690\u5f0f\u5730\u8f6c\u6210lua_State\u6307\u9488\u3002\u5728\u7ed1\u5b9a\u5230Lua\u4e2d\u7684C++\u51fd\u6570\u91cc\uff0c\u53ef\u4ee5\u5728\u53c2\u6570\u5217\u8868\u7684\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0sol::this_state\u58f0\u660e\uff0c\u800c\u5728Lua\u4ee3\u7801\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u5219\u53ef\u4ee5\u5ffd\u7565\u8be5\u53c2\u6570\uff0cSol\u4f1a\u81ea\u52a8\u5e2e\u4f60\u5c06Lua\u4ee3\u7801\u6267\u884c\u65f6\u7684\u72b6\u6001\u673a\u4f5c\u4e3asol::this_state\u6765\u8fdb\u884c\u4f20\u9012\u3002<\/p>\n<pre><code>void foo(sol::table t, sol::this_state l)\r\n{\r\n    \/\/ \u5411Lua\u6808\u4e2dpush\u4e00\u4e2a\u5b57\u7b26\u4e32\r\n    lua_pushstring(l, \"hello\");\r\n    \/\/ \u6784\u5efa\u4e00\u4e2asol::object\u5bf9\u8c61\r\n    sol::object obj = sol::make_object(l, t);\r\n}\r\n\r\nint main(void)\r\n{\r\n    sol::state lua;\r\n    lua.set_function(\"foo\", foo);\r\n    \/\/ \u6267\u884cLua\u4ee3\u7801\u65f6\uff0c\u8c03\u7528foo\u51fd\u6570\u4ec5\u4f20\u9012\u4e86\u4e00\u4e2atable\u4f5c\u4e3a\u53c2\u6570\r\n    lua.script(\"local t = { a = 1, b = 2 }; foo(t);\");\r\n}\r\n<\/code><\/pre>\n<h4 id=\"C-\u4e0eLua\u7684\u8fd4\u56de\u503c\">C++\u4e0eLua\u7684\u8fd4\u56de\u503c<\/h4>\n<h5 id=\"\u63a5\u6536\u6765\u81eaLua\u7684\u591a\u4e2a\u8fd4\u56de\u503c\">\u63a5\u6536\u6765\u81eaLua\u7684\u591a\u4e2a\u8fd4\u56de\u503c<\/h5>\n<pre><code>int main () {\r\n    sol::state lua;\r\n\r\n    lua.script(\"function f (a, b, c) return a, b, c end\");\r\n\r\n    std::tuple&lt;int, int, int&gt; result;\r\n    result = lua[\"f\"](1, 2, 3);\r\n    \/\/ result == { 1, 2, 3 }\r\n    int a, int b;\r\n    std::string c;\r\n    sol::tie( a, b, c ) = lua[\"f\"](1, 2, \"bark\");\r\n    \/\/ a == 1\r\n    \/\/ b == 2\r\n    \/\/ c == \"bark\"\r\n}\r\n<\/code><\/pre>\n<h5 id=\"\u8fd4\u56de\u591a\u79cd\u7c7b\u578b\u7ed9Lua\">\u8fd4\u56de\u591a\u79cd\u7c7b\u578b\u7ed9Lua<\/h5>\n<p>\u5728C++\u4ee3\u7801\u4e2d\uff0c\u65e0\u6cd5\u505a\u5230\u50cfLua\u90a3\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u8fd4\u56de\u5404\u79cd\u7c7b\u578b\u7684\u503c\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u5229\u7528sol::object\u6765\u5c01\u88c5\u6210\u4e0d\u540c\u7684\u201cLua\u7c7b\u578b\u201d\u8fd4\u56de\u7ed9Lua\u5c42\u3002<\/p>\n<pre><code>sol::object fancy_func (sol::object a, sol::object b, sol::this_state s) {\r\n    sol::state_view lua = s;\r\n    if (a.is&lt;int&gt;() &amp;&amp; b.is&lt;int&gt;()) {\r\n            return sol::make_object(lua, a.as&lt;int&gt;() + b.as&lt;int&gt;());\r\n    }\r\n    else if (a.is&lt;bool&gt;()) {\r\n            bool do_triple = a.as&lt;bool&gt;();\r\n            return sol::make_object(lua, b.as&lt;double&gt;() * ( do_triple ? 3 : 1 ) );\r\n    }\r\n    return sol::make_object(lua, sol::nil);\r\n}\r\n\r\nsol::state lua;\r\n\r\nlua[\"f\"] = fancy_func;\r\n\r\nint result = lua[\"f\"](1, 2);\r\n\/\/ result == 3\r\ndouble result2 = lua[\"f\"](false, 2.5);\r\n\/\/ result2 == 2.5\r\n\r\n\/\/ call in Lua, get result\r\nlua.script(\"result3 = f(true, 5.5)\");\r\ndouble result3 = lua[\"result3\"];\r\n\/\/ result3 == 16.5\r\n<\/code><\/pre>\n<\/div>\n<div>\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; \u00a0\u53d1\u8868\u4e8e\u00a0201<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4343","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4343"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4343\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}