首页 > 其他分享 >[十万个为什么] [lua] 自定义byte_buffer

[十万个为什么] [lua] 自定义byte_buffer

时间:2024-07-24 11:50:34浏览次数:6  
标签:bf 十万个 自定义 buffer lua int byte luaL

#include "lprefix.h"

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

#define BYTE_BUFFER_META_TABLE_NAME  "byte_buffer*"

#define GET_BYTE_BUFFER(L) ((byte_buffer_t*)luaL_checkudata(L, 1, BYTE_BUFFER_META_TABLE_NAME))

// ---------------------------------------------------------------------------
typedef struct byte_buffer_t byte_buffer_t;

struct byte_buffer_t
{
	int 	m_size;

	uint8_t  m_buffer[0];
};

// 创建自定义类型byte_buffer_t
// ---------------------------------------------------------------------------
static int byte_buffer_new(lua_State* L)
{
	int size = luaL_checkinteger(L, 1);

	if (size <= 0)
	{
		return 0;
	}

	byte_buffer_t* bf = (byte_buffer_t*)lua_newuserdatauv(L, 4+size, 0);
	if (bf == NULL)
	{
		return 0;
	}

	bf->m_size = size;

	luaL_setmetatable(L, BYTE_BUFFER_META_TABLE_NAME);

	return 1;
}

// ---------------------------------------------------------------------------
static int bf_meta_len(lua_State* L)
{
	byte_buffer_t* bf = GET_BYTE_BUFFER(L);
	lua_pushinteger(L, bf->m_size);
	return 1;
}

// ---------------------------------------------------------------------------
static int bf_meta_tostring(lua_State* L)
{
	byte_buffer_t* bf = GET_BYTE_BUFFER(L);
	lua_pushfstring(L, "byte_buffer(%d)@%p", bf->m_size, bf);
	return 1;
}

// ---------------------------------------------------------------------------
static int bf_get(lua_State* L)
{
	byte_buffer_t* bf = GET_BYTE_BUFFER(L);
	int index = luaL_checkinteger(L, 2);
	if (index >= 0 && index < bf->m_size)
	{
		lua_pushinteger(L, bf->m_buffer[index]);
		return 1;
	}
	return 0;
}

// ---------------------------------------------------------------------------
static int bf_set(lua_State* L)
{
	byte_buffer_t* bf = GET_BYTE_BUFFER(L);
	int index = luaL_checkinteger(L, 2);
	int value = luaL_checkinteger(L, 3);
	if (index >= 0 && index < bf->m_size)
	{
		bf->m_buffer[index] = value&0xFF;
		lua_pushboolean(L, 1);
		return 1;
	}
	return 0;
}

// ---------------------------------------------------------------------------
static const luaL_Reg byte_buffer_func[] =
{
	{"new", byte_buffer_new},
	{NULL, NULL},
};

// ---------------------------------------------------------------------------
static const luaL_Reg bf_meta_func[] =
{
	{"__index", NULL},  /* placeholder */
	{"__len", bf_meta_len},
	{"__tostring", bf_meta_tostring},
	{NULL, NULL},
};

// ---------------------------------------------------------------------------
static const luaL_Reg bf_func[] =
{
	{"len", bf_meta_len},
	{"get", bf_get},
	{"set", bf_set},
	{NULL, NULL},
};

// ---------------------------------------------------------------------------
LUAMOD_API int luaopen_byte_buffer(lua_State *L);

LUAMOD_API int luaopen_byte_buffer(lua_State *L)
{
	// 创建 byte_bufer = {...}
	luaL_newlib(L, byte_buffer_func);

	// 创建 bf_meta = {..}
	luaL_newmetatable(L, BYTE_BUFFER_META_TABLE_NAME);
	luaL_setfuncs(L, bf_meta_func, 0);

	// 创建bf = {...}
	luaL_newlibtable(L, bf_func);
	luaL_setfuncs(L, bf_func, 0);

	// 设置buf_meta.__index = bf
	lua_setfield(L, -2, "__index");

	lua_pop(L, 1);

	return 1;
}

 

标签:bf,十万个,自定义,buffer,lua,int,byte,luaL
From: https://www.cnblogs.com/kehuadong/p/18320564

相关文章

  • 【C语言】自定义类型——联合和枚举
    目录一、联合体1.1联合体类型的声明1.2联合体的特点1.2.1特点11.2.2特点21.2.3特点31.3联合体的大小1.4相同成员的结构体和联合体的对比1.5使用联合体节省空间的例子1.6运用联合体判断大小端1.7利用联合体打印存储的字节内容二、枚举类型2.1枚举类型的......
  • mybatisPlus3.4 自定义sqlSessionFactory sql注入器失效、mybatis-plus批量插入报错In
    文章目录一、报错背景二、解决方法在mybatis-plus项目中集成自定义批量插入方法后报错。以下整理一下报错及解决方法。一、报错背景mybatis-plus是不提供insertList批量插入方法的,本人在自定义批量插入方法后,启动时正常,但是执行到insertList时报错。org.apache.i......
  • flask写接口,定制日志输出以及将请求状态以及自定义日志写入文件中
    前言:无论是写接口还是写项目,配置日志是必选的;适合配置可以帮助自己排查代码逻辑问题简单说一说日志的等级以及用处1.日志等级DEBUG:10INFO:20WARN:30ERROR。40CRITICAL:50数字越大,等级越高!!2.日志用处:        DEBUG(调试):用于开发阶段的调试,开启后,会记录程序......
  • 在帝国CMS中设置自定义页面主要涉及以下步骤
    步骤1:创建新页面登录帝国CMS后台。在左侧导航栏中,单击“页面管理”。单击“添加单页信息”。步骤2:设置页面基本信息在“页面名称”字段中输入页面的名称。在“页面别名”字段中输入页面的别名(用于在URL中使用)。在“所属栏目”字段中选择页面的父栏目(如果没有,则选择“顶级栏......
  • Typora设置自定义脚本上传图片
    搭建图床服务这里利用CloudFlare搭建免费的图床服务cf-image-hosting部署Pages$gitclonehttps://github.com/ifyour/cf-image-hosting.git$cdcf-image-hosting$npminstall&&npmrundeploy部署成功后会显示如下信息设置自定义域名点击左侧Workers和Pages,选......
  • 织梦dedecms自定义表单选项必填怎么修改?
    我们先在plus/diy.php文件中的的第40行下加上一下代码//增加必填字段判断if($required!=&#39;&#39;){if(preg_match(&#39;/,/&#39;,$required)){$requireds=explode(&#39;,&#39;,$required);foreach($requiredsas$field){if($$field==&#39;&#39;){......
  • 自定义全选框,当勾选√添加到selection中,再次勾选从selection中移除
    <el-table:data="tableData"ref="tableData"height="450px"class="customer-no-border-table":row-class-name="tableRowClassName":......
  • Android开发 - ViewGroup解析与自定义
    ViewGroup解析ViewGroup是一个特殊的View,可以包含其他视图(称为子视图)。而ViewGroup是View的子类,所以ViewGroup可以当成普通的UI组件使用。ViewGroup是布局和视图容器的基类,该类还定义了ViewGroup.LayoutParams用作布局参数基类的类由于ViewGroup的直接子类和间接子类比较......
  • vue element-ui表格table 表格动态 添加行、删除行、添加列、删除列 自定义表头
       vuetable表格动态添加行、删除行、添加列、删除列自定义表头; 增加一行、删除一行、添加一列、删除一列;每行带输入框input代码1、HTML部分:<template><divclass="app-container"><el-table:data="tableData"borderstyle="width:600px;margin-top:2......
  • echarts的markline自定义起始位置和终点位置
    letmarkPoint=[10,20];markLine:{symbol:["none","none"],//去掉箭头silent:true,label:{show:true,formatter:"{b}",offset:[-......