Lua 5.3 -- SOL2.0 用户指南 【2】
注意
在你学习了sol的基础知识之后,建议你如果认为某些东西可以运行,你应该尝试一下。它可能会运行!
以下所有代码均可在sol2教程示例中找到。
断言/先决条件
The implementation for assert.hpp with c_assert looks like so:
你需要代码中#include<sol.hpp>#include "sol.hpp"。SOL只有头文件,所以你不需要编译任何东西。但是,Lua必须编译并可用。有关更多详细信息,请参阅入门教程。
assert.hpp 和 c_assert 的实现看起来像这样:
#ifndef EXAMPLES_ASSERT_HPP
#define EXAMPLES_ASSERT_HPP # define m_assert(condition, message) \
do { \
if (! (condition)) { \
std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
<< " line " << __LINE__ << ": " << message << std::endl; \
std::terminate(); \
} \
} while (false) # define c_assert(condition) \
do { \
if (! (condition)) { \
std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
<< " line " << __LINE__ << std::endl; \
std::terminate(); \
} \
} while (false)
#else
# define m_assert(condition, message) do { if (false) { (void)(condition); (void)sizeof(message); } } while (false)
# define c_assert(condition) do { if (false) { (void)(condition); } } while (false)
#endif #endif // EXAMPLES_ASSERT_HPP 这是下面示例代码中使用的断言。
打开状态
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp> #include <iostream>
#include "../../assert.hpp" int main(int, char*[]) {
std::cout << "=== opening a state ===" << std::endl; sol::state lua;
// open some common libraries
lua.open_libraries(sol::lib::base, sol::lib::package);
lua.script("print('bark bark bark!')"); std::cout << std::endl; return 0;
}
在lua_State上使用sol2 *
对于已经使用Lua或使用其他的Lua系统(LuaBridge,kaguya,Luwra等)的系统/游戏,你仍然会喜欢sol2的:
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp> #include <iostream> int use_sol2(lua_State* L) {
sol::state_view lua(L);
lua.script("print('bark bark bark!')");
return 0;
} int main(int, char*[]) {
std::cout << "=== opening sol::state_view on raw Lua ===" << std::endl; lua_State* L = luaL_newstate();
luaL_openlibs(L); lua_pushcclosure(L, &use_sol2, 0);
lua_setglobal(L, "use_sol2"); if (luaL_dostring(L, "use_sol2()")) {
lua_error(L);
return -1;
} std::cout << std::endl; return 0;
}
运行lua代码
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp> #include <fstream>
#include <iostream>
#include "../../assert.hpp" int main(int, char*[]) {
std::cout << "=== running lua code ===" << std::endl; sol::state lua;
lua.open_libraries(sol::lib::base); // load and execute from string
lua.script("a = 'test'");
// load and execute from file
lua.script_file("a_lua_script.lua"); // run a script, get the result
int value = lua.script("return 54");
c_assert(value == 54); //要运行Lua代码但需有错误处理程序以防出现问题:
auto bad_code_result = lua.script("123 herp.derp", [](lua_State*, sol::protected_function_result pfr) {
// pfr will contain things that went wrong, for either loading or executing the script
// Can throw your own custom error
// You can also just return it, and let the call-site handle the error if necessary.
return pfr;
});
// it did not work
c_assert(!bad_code_result.valid());
// the default handler panics or throws, depending on your settings
// uncomment for explosions:
//auto bad_code_result_2 = lua.script("bad.code", &sol::script_default_on_error);
return 0;
}
运行lua代码(底层)
您可以使用单独的加载和函数调用操作符来加载,检查,然后运行和检查代码。
警告
这只是在你需要某种细粒度控制的情况下:对于99%的情况,运行lua代码是首选,并避免在不理解脚本/加载与需要在加载后运行块之间的差异时的缺陷。
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp> #include <fstream>
#include <iostream>
#include <cstdio>
#include "../../assert.hpp" int main(int, char*[]) {
std::cout << "=== running lua code (low level) ===" << std::endl; sol::state lua;
lua.open_libraries(sol::lib::base); // load file without execute
sol::load_result script1 = lua.load_file("a_lua_script.lua");
//execute
script1(); // load string without execute
sol::load_result script2 = lua.load("a = 'test'");
//execute
sol::protected_function_result script2result = script2();
// optionally, check if it worked
if (script2result.valid()) {
// yay!
}
else {
// aww
} sol::load_result script3 = lua.load("return 24");
// execute, get return value
int value2 = script3();
c_assert(value2 == 24); return 0;
}
QQ群号: 790763256
Lua 5.3 -- SOL2.0 用户指南 【2】的更多相关文章
- Lua 5.3 -- SOL2.0 用户指南 【1】
SOL2.2 是一个快速.简单的C++与LUA的绑定器.如果确定要在你的程序里面同时运行Lua和C++,SOL 是一个高性能的绑定器,是一个API使用方便的 GO-TO 框架. 简单看一下特点:这个链 ...
- Gradle2.0用户指南翻译——第一章. 介绍
翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...
- Gradle2.0用户指南翻译——第三章. 教程
翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...
- Gradle2.0用户指南翻译——第二章. 概述
翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...
- Netty4.0 用户指南
原文链接http://netty.io/wiki/user-guide-for-4.x.html 前言 Nowadays we use general purpose applications or ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) Processors
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) Channel
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) Sink
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) source
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
随机推荐
- Jenkins的制品管理
Jenkins的制品管理 制品是什么? 也叫产出物或工件.制品是软件开发过程中产生的多种有形副产品之一.广义的制品包括用例.UML图.设计文档等.而狭义的制品就可以简单地理解为二进制包.虽然有些代码是 ...
- VMware虚拟机安装Mac OS X 10.12
VMware Workstation Pro 14 安装Mac OS X 10.12 下面是所需要的补丁工具及镜像 VMware Workstation unlocker-master(OS X 虚拟 ...
- loadrunner-事务
自从安装了loadrunner之后,就没怎么用过它了,项目之前也没做过性能测试,所以学习起来比较困难,而且性能测试远远不止使用工具这么简单.下面介绍一下最近学习的loadrunner添加事务. 事务是 ...
- 洛谷 P3909 异或之积 题解
原题链接 本人看了其它解法,发现本人的解法还是 首创 ! 而且我的解法好像和 \(\times 6\) 没什么关系 -- (如果没 \(\times 6\),我没还不用算逆元) 别人的思路呢,大都是从 ...
- 北邮oj 97. 二叉排序树
97. 二叉排序树 时间限制 1000 ms 内存限制 65536 KB 题目描述 二叉排序树,也称为二叉查找树.可以是一颗空树,也可以是一颗具有如下特性的非空二叉树: 若左子树非空,则左子树上所有节 ...
- 使用scrapy-selenium, chrome-headless抓取动态网页
在使用scrapy抓取网页时, 如果遇到使用js动态渲染的页面, 将无法提取到在浏览器中看到的内容. 针对这个问题scrapy官方给出的方案是scrapy-selenium, 这是一个把sel ...
- PHP7内核(五):系统分析生命周期
上篇文章讲述了模块初始化阶段之前的准备工作,本篇我来详细介绍PHP生命周期的五个阶段. 一.模块初始化阶段 我们先来看一下该阶段的每个函数的作用. 1.1.sapi_initialize_reques ...
- CocoaPods 安装卸载
建议升级10.15的系统,什么都装好了 sudo gem install cocoapods pod setup搞定不能有search命令,可以pod init,下载用的是cdn,打开项目正常使用 问 ...
- python中的可变和不可变对象 有序和无序对象
可变对象和不可变对象的定义:对象存放在地址的值是否可以被改变 不可变对象包括:整形int.浮点型float .字符串str .元祖tuple.布尔型boole 可变对象包括 :列表list.集合set ...
- Python学习笔记:set集合类型所有方法汇总
################################################## 集合的作用是:# 1.获得两个集合之间某种关系的集合(比如求两个集合的交集)# 2.计算集合之间的 ...