系列教程指南【1】

注意

在你学习了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】的更多相关文章

  1. Lua 5.3 -- SOL2.0 用户指南 【1】

    SOL2.2 是一个快速.简单的C++与LUA的绑定器.如果确定要在你的程序里面同时运行Lua和C++,SOL 是一个高性能的绑定器,是一个API使用方便的 GO-TO 框架. 简单看一下特点:这个链 ...

  2. Gradle2.0用户指南翻译——第一章. 介绍

    翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...

  3. Gradle2.0用户指南翻译——第三章. 教程

    翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...

  4. Gradle2.0用户指南翻译——第二章. 概述

    翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...

  5. Netty4.0 用户指南

    原文链接http://netty.io/wiki/user-guide-for-4.x.html 前言 Nowadays we use general purpose applications or ...

  6. 【翻译】Flume 1.8.0 User Guide(用户指南) Processors

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  7. 【翻译】Flume 1.8.0 User Guide(用户指南) Channel

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  8. 【翻译】Flume 1.8.0 User Guide(用户指南) Sink

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  9. 【翻译】Flume 1.8.0 User Guide(用户指南) source

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

随机推荐

  1. 036.集群网络-K8S网络模型及Linux基础网络

    一 Kubernetes网络模型概述 1.1 Kubernetes网络模型 Kubernetes网络模型设计的一个基础原则是:每个Pod都拥有一个独立的IP地址,并假定所有Pod都在一个可以直接连通的 ...

  2. 给 ABP vNext 应用安装私信模块

    在上一节五分钟完成 ABP vNext 通讯录 App 开发 中,我们用完成了通讯录 App 的基础开发. 这本章节,我们会给通讯录 App 安装私信模块,使不同用户能够通过相互发送消息,并接收新私信 ...

  3. Idea中使用http请求解决中文乱码问题

    以请求百度为例,使用如下代码即可解决: GET https://www.baidu.com User-Agent: Mozilla/.X MetaSr 1.0

  4. 我的第一个html静态网页

    <!doctype html> <html>     <head>         <title>王兆国的个人博客</title>      ...

  5. 记一次有趣的thinkphp代码执行

    0x00 前言 朋友之前给了个站,拿了很久终于拿下,简单记录一下. 0x01 基础信息 漏洞点:tp 5 method 代码执行,payload如下 POST /?s=captcha _method= ...

  6. 解决WSL在执行32位程序时报错“Exec format error”的问题

    当你尝试在WSL上运行32位的程序时,shell将会报错:cannot execute binary file: Exec format error. 这是因为WSL目前暂不支持32位的ELF可执行文 ...

  7. 【bug】table重新加载数据,页面滚动条下沉到底部,记录scrollTop后将其恢复scrollTop出现闪烁

    1.table数据请求前记录scrollTop $scope.scrollPos = document.documentElement.scrollTop; 2.html中添加指令repeat-fin ...

  8. 图像的特征工程:HOG特征描述子的介绍

    介绍 在机器学习算法的世界里,特征工程是非常重要的.实际上,作为一名数据科学家,这是我最喜欢的方面之一!从现有特征中设计新特征并改进模型的性能,这就是我们进行最多实验的地方. 世界上一些顶级数据科学家 ...

  9. 用卷积神经网络和自注意力机制实现QANet(问答网络)

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/ ,学习更多的机器学习.深度学习的知识! 在这篇文章中,我们将解决自然语言处理(具体是指问答)中最具挑战性 ...

  10. SpringBoot安装与配置

    1.环境准备 1.1.Maven安装配置 Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件. 下载Maven可执行文件 cd /usr/local ...