当前位置: 首页 > news >正文

LuaJIT 学习(3)—— ffi.* API 函数

文章目录

    • Glossary
    • Declaring and Accessing External Symbols
      • `ffi.cdef(def)`
      • `ffi.C`
      • `clib = ffi.load(name [,global])`
        • 例子:ffi.load 函数的使用
    • Creating cdata Objects
      • `cdata = ffi.new(ct [,nelem] [,init...]) `
      • `cdata = ctype([nelem,] [init...])`
        • 例子:匿名 C 结构体
      • `ctype = ffi.typeof(ct)`
      • `cdata = ffi.cast(ct, init)`
        • 例子:ffi.cast 函数的使用
      • `ctype = ffi.metatype(ct, metatable)`
        • 例子:ffi.metatype 函数的使用
      • `cdata = ffi.gc(cdata, finalizer)`
    • C Type Information
      • `size = ffi.sizeof(ct [,nelem])`
      • `align = ffi.alignof(ct)`
      • `ofs [,bpos,bsize] = ffi.offsetof(ct, field)`
      • `status = ffi.istype(ct, obj)`
    • Utility Functions
      • `err = ffi.errno([newerr])`
      • `str = ffi.string(ptr [,len])`
        • 例子:ffi.string 函数的使用
      • `ffi.copy(dst, src, len) `
      • `ffi.copy(dst, str)`
        • 例子:ffi.copy 函数的使用
      • `ffi.fill(dst, len [,c])`
    • Target-specific Information
      • `status = ffi.abi(param)`
      • `ffi.os`
      • `ffi.arch`
    • Methods for Callbacks
      • `cb:free()`
      • `cb:set(func)`
        • 例子:callback 的创建与使用
    • Extended Standard Library Functions
      • `n = tonumber(cdata)`
      • `s = tostring(cdata)`
      • `iter, obj, start = pairs(cdata) `
      • `iter, obj, start = ipairs(cdata)`
    • Extensions to the Lua Parser

Glossary

  • cdecl — An abstract C type declaration (a Lua string).
  • ctype — A C type object. This is a special kind of cdata returned by ffi.typeof(). It serves as a cdata constructor when called.
  • cdata — A C data object. It holds a value of the corresponding ctype.
  • ct — A C type specification which can be used for most of the API functions. Either a cdecl, a ctype or a cdata serving as a template type.
  • cb — A callback object. This is a C data object holding a special function pointer. Calling this function from C code runs an associated Lua function.
  • VLA — A variable-length array is declared with a ? instead of the number of elements, e.g. "int[?]". The number of elements (nelem) must be given when it’s created.
  • VLS — A variable-length struct is a struct C type where the last element is a VLA. The same rules for declaration and creation apply.

LuaJIT 新增了一种数据类型叫 cdata,用来表示 C 语言数据对象。ctypecb 都是一种特殊的 cdata

如果数据类型是 cdata,可以使用 tostring 函数返回一个字符串表示一个 cdatactype 对象的 C 语言类型。

Declaring and Accessing External Symbols

External symbols must be declared first and can then be accessed by indexing a C library namespace, which automatically binds the symbol to a specific library.

ffi.cdef(def)

Adds multiple C declarations for types or external symbols (named variables or functions). def must be a Lua string. It’s recommended to use the syntactic sugar for string arguments as follows:

ffi.cdef[[
typedef struct foo { int a, b; } foo_t;  // Declare a struct and typedef.
int dofoo(foo_t *f, int n);  /* Declare an external C function. */
]]

The contents of the string (the part in green above) must be a sequence of C declarations, separated by semicolons. The trailing semicolon for a single declaration may be omitted.

Please note, that external symbols are only declared, but they are not bound to any specific address, yet. Binding is achieved with C library namespaces (see below).

C declarations are not passed through a C pre-processor, yet. No pre-processor tokens are allowed, except for #pragma pack. Replace #define in existing C header files with enum, static const or typedef and/or pass the files through an external C pre-processor (once). Be careful not to include unneeded or redundant declarations from unrelated header files.

ffi.C

This is the default C library namespace — note the uppercase 'C'. It binds to the default set of symbols or libraries on the target system. These are more or less the same as a C compiler would offer by default, without specifying extra link libraries.

On POSIX systems, this binds to symbols in the default or global namespace. This includes all exported symbols from the executable and any libraries loaded into the global namespace. This includes at least libc, libm, libdl (on Linux), libgcc (if compiled with GCC), as well as any exported symbols from the Lua/C API provided by LuaJIT itself.

On Windows systems, this binds to symbols exported from the *.exe, the lua51.dll (i.e. the Lua/C API provided by LuaJIT itself), the C runtime library LuaJIT was linked with (msvcrt*.dll), kernel32.dll, user32.dll and gdi32.dll.

clib = ffi.load(name [,global])

This loads the dynamic library given by name and returns a new C library namespace which binds to its symbols. On POSIX systems, if global is true, the library symbols are loaded into the global namespace, too.

If name is a path, the library is loaded from this path. Otherwise name is canonicalized in a system-dependent way and searched in the default search path for dynamic libraries:

On POSIX systems, if the name contains no dot, the extension .so is appended. Also, the lib prefix is prepended if necessary. So ffi.load("z") looks for "libz.so" in the default shared library search path.

On Windows systems, if the name contains no dot, the extension .dll is appended. So ffi.load("ws2_32") looks for "ws2_32.dll" in the default DLL search path.

例子:ffi.load 函数的使用

测试的 C 库代码 mytest.c

#include <stdio.h>void test(void)
{printf("hello world!\n");
}

编译成动态库

gcc -o mytest.so -shared mytest.c -fPIC

LuaJIT 中加载并使用这个库

local ffi = require("ffi")
local c = ffi.Cffi.cdef[[void test(void);
]]-- 第二个参数为 true ,库的符号会加载到 C 库命名空间
ffi.load("./mytest.so", true)
c.test()-- local mytest = ffi.load("./mytest.so", false)
-- mytest.test()

Creating cdata Objects

The following API functions create cdata objects (type() returns "cdata"). All created cdata objects are garbage collected.

cdata = ffi.new(ct [,nelem] [,init...])

cdata = ctype([nelem,] [init...])

Creates a cdata object for the given ct. VLA/VLS types require the nelem argument. The second syntax uses a ctype as a constructor and is otherwise fully equivalent.

The cdata object is initialized according to the rules for initializers, using the optional init arguments. Excess initializers cause an error.

Performance notice: if you want to create many objects of one kind, parse the cdecl only once and get its ctype with ffi.typeof(). Then use the ctype as a constructor repeatedly.

Please note, that an anonymous struct declaration implicitly creates a new and distinguished ctype every time you use it for ffi.new(). This is probably not what you want, especially if you create more than one cdata object. Different anonymous structs are not considered assignment-compatible by the C standard, even though they may have the same fields! Also, they are considered different types by the JIT-compiler, which may cause an excessive number of traces. It’s strongly suggested to either declare a named struct or typedef with ffi.cdef() or to create a single ctype object for an anonymous struct with ffi.typeof().

例子:匿名 C 结构体
local ffi = require("ffi")ffi.cdef [[typedef struct {int x;int y;} point;
]]print(tostring(ffi.new("point")))                  --> cdata<struct 97>: 0x7fa13b1bf1d8
print(tostring(ffi.new("point")))                  --> cdata<struct 97>: 0x7fa13b1beff0
print(tostring(ffi.new("struct {int x; int y;}"))) --> cdata<struct 101>: 0x7fa13b1c2e50
-- 会创建新的结构体类型
print(tostring(ffi.new("struct {int x; int y;}"))) --> cdata<struct 104>: 0x7fa13b1c3048local t = ffi.typeof("struct {int x; int y;}")
print(tostring(t())) --> cdata<struct 107>: 0x7fa13b1c3100
-- 不会创建新的结构体类型
print(tostring(t())) --> cdata<struct 107>: 0x7fa13b1c3198

从输出可以看到,每次调用 ffi.new 创建一个匿名结构体对象都会创建一个新的结构体类型。

使用 ffi.typeof 为匿名结构体创建一个ctype 对象后,用 ctype 创建结构体对象,就不会再创建新的类型。

ctype = ffi.typeof(ct)

Creates a ctype object for the given ct.

This function is especially useful to parse a cdecl only once and then use the resulting ctype object as a constructor.

cdata = ffi.cast(ct, init)

Creates a scalar cdata object for the given ct. The cdata object is initialized with init using the “cast” variant of the C type conversion rules.

This functions is mainly useful to override the pointer compatibility checks or to convert pointers to addresses or vice versa.

例子:ffi.cast 函数的使用
local ffi = require("ffi")ffi.cdef [[typedef struct {int x;int y;} point;
]]local p = ffi.new("point")
local pp = ffi.cast("point *", p)
pp.x = 1
pp.y = 2
print(p.x)
print(p.y)

使用 ffi.cast 创建一个执行结构体的指针,然后对结构体赋值。

之所以是这样是因为 FFI 语义中C type conversion rules,会先将cdata对象转换成C类型也就是结构体,

然后会将结构体的基地址转换为指针!

ctype = ffi.metatype(ct, metatable)

Creates a ctype object for the given ct and associates it with a metatable. Only struct/union types, complex numbers and vectors are allowed. Other types may be wrapped in a struct, if needed.

The association with a metatable is permanent and cannot be changed afterwards. Neither the contents of the metatable nor the contents of an __index table (if any) may be modified afterwards. The associated metatable automatically applies to all uses of this type, no matter how the objects are created or where they originate from. Note that predefined operations on types have precedence (e.g. declared field names cannot be overridden).

All standard Lua metamethods are implemented. These are called directly, without shortcuts, and on any mix of types. For binary operations, the left operand is checked first for a valid ctype metamethod. The __gc metamethod only applies to struct/union types and performs an implicit ffi.gc() call during creation of an instance.

例子:ffi.metatype 函数的使用
local ffi = require("ffi")ffi.cdef[[
typedef struct { double x, y; } point_t;
]]local point
local mt = {__add = function(a, b) return point(a.x+b.x, a.y+b.y) end,__len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,__index = {area = function(a) return a.x*a.x + a.y*a.y end,},
}
point = ffi.metatype("point_t", mt)local a = point(3, 4)
print(a.x, a.y)  --> 3  4
print(#a)        --> 5
print(a:area())  --> 25
local b = a + point(0.5, 8)
print(#b)        --> 12.5-- 并不一定需要使用 ffi.metatype 返回的 ctype 来创建数据,只要调用了 ffi.metatype,关联的元表会自动应用到所有该类型的使用上。
local b = ffi.new("point_t", 3, 4)
print(#b)

cdata = ffi.gc(cdata, finalizer)

Associates a finalizer with a pointer or aggregate cdata object. The cdata object is returned unchanged.

This function allows safe integration of unmanaged resources into the automatic memory management of the LuaJIT garbage collector. Typical usage:

local p = ffi.gc(ffi.C.malloc(n), ffi.C.free)
...
p = nil -- Last reference to p is gone.
-- GC will eventually run finalizer: ffi.C.free(p)

A cdata finalizer works like the __gc metamethod for userdata objects: when the last reference to a cdata object is gone, the associated finalizer is called with the cdata object as an argument. The finalizer can be a Lua function or a cdata function or cdata function pointer. An existing finalizer can be removed by setting a nil finalizer, e.g. right before explicitly deleting a resource:

ffi.C.free(ffi.gc(p, nil)) -- Manually free the memory.

C Type Information

The following API functions return information about C types. They are most useful for inspecting cdata objects.

size = ffi.sizeof(ct [,nelem])

Returns the size of ct in bytes. Returns nil if the size is not known (e.g. for "void" or function types). Requires nelem for VLA/VLS types, except for cdata objects.

align = ffi.alignof(ct)

Returns the minimum required alignment for ct in bytes.

ofs [,bpos,bsize] = ffi.offsetof(ct, field)

Returns the offset (in bytes) of field relative to the start of ct, which must be a struct. Additionally returns the position and the field size (in bits) for bit fields.

status = ffi.istype(ct, obj)

Returns true if obj has the C type given by ct. Returns false otherwise.

C type qualifiers (const etc.) are ignored. Pointers are checked with the standard pointer compatibility rules, but without any special treatment for void *. If ct specifies a struct/union, then a pointer to this type is accepted, too. Otherwise the types must match exactly.

Note: this function accepts all kinds of Lua objects for the obj argument, but always returns false for non-cdata objects.

Utility Functions

err = ffi.errno([newerr])

Returns the error number set by the last C function call which indicated an error condition. If the optional newerr argument is present, the error number is set to the new value and the previous value is returned.

This function offers a portable and OS-independent way to get and set the error number. Note that only some C functions set the error number. And it’s only significant if the function actually indicated an error condition (e.g. with a return value of -1 or NULL). Otherwise, it may or may not contain any previously set value.

You’re advised to call this function only when needed and as close as possible after the return of the related C function. The errno value is preserved across hooks, memory allocations, invocations of the JIT compiler and other internal VM activity. The same applies to the value returned by GetLastError() on Windows, but you need to declare and call it yourself.

str = ffi.string(ptr [,len])

Creates an interned Lua string from the data pointed to by ptr.

If the optional argument len is missing, ptr is converted to a "char *" and the data is assumed to be zero-terminated. The length of the string is computed with strlen().

Otherwise ptr is converted to a "void *" and len gives the length of the data. The data may contain embedded zeros and need not be byte-oriented (though this may cause endianess issues).

This function is mainly useful to convert (temporary) "const char *" pointers returned by C functions to Lua strings and store them or pass them to other functions expecting a Lua string. The Lua string is an (interned) copy of the data and bears no relation to the original data area anymore. Lua strings are 8 bit clean and may be used to hold arbitrary, non-character data.

Performance notice: it’s faster to pass the length of the string, if it’s known. E.g. when the length is returned by a C call like sprintf().

例子:ffi.string 函数的使用
local ffi = require("ffi")local function replace(s)local s1 = ffi.new("char [?]", #s)ffi.copy(s1, s, #s)s1[0] = string.byte("a")s1[1] = string.byte("b")s1[2] = string.byte("c")return ffi.string(s1, #s)
endlocal s = replace("123abc")
print(s) -- abcabc

ffi.copy(dst, src, len)

ffi.copy(dst, str)

Copies the data pointed to by src to dst. dst is converted to a "void *" and src is converted to a "const void *".

In the first syntax, len gives the number of bytes to copy. Caveat: if src is a Lua string, then len must not exceed #src+1.

In the second syntax, the source of the copy must be a Lua string. All bytes of the string plus a zero-terminator are copied to dst (i.e. #src+1 bytes).

Performance notice: ffi.copy() may be used as a faster (inlinable) replacement for the C library functions memcpy(), strcpy() and strncpy().

例子:ffi.copy 函数的使用
local ffi = require("ffi")-- 假设有一个 C 类型的结构体
ffi.cdef[[
typedef struct {int a;int b;
} MyStruct;
]]-- 分配内存
local src = ffi.new("MyStruct", 10, 20)
local dst = ffi.new("MyStruct")-- 使用 ffi.copy 拷贝数据
ffi.copy(dst, src, ffi.sizeof("MyStruct"))-- 输出目标内存的数据,验证是否拷贝成功
print("dst.a =", dst.a)  -- 10
print("dst.b =", dst.b)  -- 20

ffi.fill(dst, len [,c])

Fills the data pointed to by dst with len constant bytes, given by c. If c is omitted, the data is zero-filled.

Performance notice: ffi.fill() may be used as a faster (inlinable) replacement for the C library function memset(dst, c, len). Please note the different order of arguments!

Target-specific Information

status = ffi.abi(param)

Returns true if param (a Lua string) applies for the target ABI (Application Binary Interface). Returns false otherwise. The following parameters are currently defined:

ParameterDescription
32bit32 bit architecture
64bit64 bit architecture
leLittle-endian architecture
beBig-endian architecture
fpuTarget has a hardware FPU
softfpsoftfp calling conventions
hardfphardfp calling conventions
eabiEABI variant of the standard ABI
winWindows variant of the standard ABI
pauthPointer authentication ABI
uwpUniversal Windows Platform
gc6464 bit GC references

ffi.os

Contains the target OS name. Same contents as jit.os.

ffi.arch

Contains the target architecture name. Same contents as jit.arch.

Methods for Callbacks

The C types for callbacks have some extra methods:

cb:free()

Free the resources associated with a callback. The associated Lua function is unanchored and may be garbage collected. The callback function pointer is no longer valid and must not be called again (it may be reused by a subsequently created callback).

cb:set(func)

Associate a new Lua function with a callback. The C type of the callback and the callback function pointer are unchanged.

This method is useful to dynamically switch the receiver of callbacks without creating a new callback each time and registering it again (e.g. with a GUI library).

例子:callback 的创建与使用
local ffi = require("ffi")ffi.cdef[[typedef void (*func)(int);
]]local function test_func1(n)print(n)
endlocal function test_func2(n)print(n*2)
end-- 创建一个 callback
local cb = ffi.cast("func", test_func1)
cb(1) --> 1cb:set(test_func2)
cb(1) --> 2-- 释放 callback
cb:free()

Extended Standard Library Functions

The following standard library functions have been extended to work with cdata objects:

n = tonumber(cdata)

Converts a number cdata object to a double and returns it as a Lua number. This is particularly useful for boxed 64 bit integer values. Caveat: this conversion may incur a precision loss.

s = tostring(cdata)

Returns a string representation of the value of 64 bit integers (nnnLL” or nnnULL”) or complex numbers (re±imi”).

Otherwise returns a string representation of the C type of a ctype object (“ctype<type>”) or a cdata object ("cdata<type>: address"), unless you override it with a __tostring metamethod (see ffi.metatype()).

iter, obj, start = pairs(cdata)

iter, obj, start = ipairs(cdata)

Calls the __pairs or __ipairs metamethod of the corresponding ctype.

Extensions to the Lua Parser

The parser for Lua source code treats numeric literals with the suffixes LL or ULL as signed or unsigned 64 bit integers. Case doesn’t matter, but uppercase is recommended for readability. It handles decimal (42LL), hexadecimal (0x2aLL) and binary (0b101010LL) literals.

比如

print(type(123LL)) --> cdata
print(type(123ULL)) --> cdata

The imaginary part of complex numbers can be specified by suffixing number literals with i or I, e.g. 12.5i. Caveat: you’ll need to use 1i to get an imaginary part with the value one, since i itself still refers to a variable named i.

相关文章:

LuaJIT 学习(3)—— ffi.* API 函数

文章目录 GlossaryDeclaring and Accessing External Symbolsffi.cdef(def)ffi.Cclib ffi.load(name [,global])例子&#xff1a;ffi.load 函数的使用 Creating cdata Objectscdata ffi.new(ct [,nelem] [,init...]) cdata ctype([nelem,] [init...])例子&#xff1a;匿名 C…...

[资源分享]-web3/区块链/学习路线/资料/找工作方式/水龙头

记录个人学习web3整理的资料 后续如果有 了解/入坑 打算, 提前收藏一下. 1. 学习路线 登链社区-学习路线图 2. 学习资料 国内成系统的资料比较少,我整理的网盘的,关注私信我,资料互相学习 前言 | 区块链技术指南 学习web3-僵尸小游戏 web3.js文档 ethers.js官方文档 Hardhat文…...

Django-ORM-prefetch_related

Django-ORM-prefetch_related 模型定义N1 查询问题示例 使用 prefetch_related 优化查询处理更复杂的查询示例&#xff1a;预取特定条件的书籍示例&#xff1a;预取多个关联字段 性能比较注意事项总结 通过 Author 和 Books 两个模型来理解 Django 的 prefetch_related 方法。 …...

MySQL 批量插入 vs 逐条插

MySQL 插入数据&#xff1a;批量插入 vs 逐条插入&#xff0c;哪个更快&#xff1f; 在 MySQL 中&#xff0c;插入数据有两种常见方式&#xff1a; 批量插入&#xff1a;一条 SQL 插入多条数据。逐条插入&#xff1a;每次插入一条数据。 这两种方式有什么区别&#xff1f;哪…...

Linux centos 7 grub引导故障恢复

CentOS 7误删GRUB2可以通过以下步骤恢复&#xff1a; 进入救援模式 1. 插入CentOS 7安装光盘&#xff0c;重启系统。在开机时按BIOS设置对应的按键&#xff08;通常是F2等&#xff09;&#xff0c;将启动顺序调整为CD - ROM优先。 2. 系统从光盘启动后&#xff0c;选择“…...

要在Unreal Engine 5(UE5)中实现角色打击怪物并让怪物做出受击反应,

UE5系列文章目录 文章目录 UE5系列文章目录前言一、实现思路二、最终效果 前言 ue5角色受击没有播放受击动画&#xff0c;主角达到怪物身上没有反应 一、实现思路 要在Unreal Engine 5&#xff08;UE5&#xff09;中实现角色打击怪物并让怪物做出受击反应&#xff0c;你需要…...

Navicat for Snowflake 震撼首发,激活数据仓库管理全新动能

近日&#xff0c;Navicat 家族迎来了一位全新成员 — Navicat for Snowflake。Snowflake 是一款基于云架构的现代数据仓库解决方案&#xff0c;以其弹性扩展、高性能和易用性著称。这次首发的Navicat for Snowflake 专为简化 Snowflake 数据库管理任务而精心打造。它凭借其直观…...

【redis】发布订阅

Redis的发布订阅&#xff08;Pub/Sub&#xff09;是一种基于消息多播的通信机制&#xff0c;它允许消息的**发布者&#xff08;Publisher&#xff09;向特定频道发送消息&#xff0c;而订阅者&#xff08;Subscriber&#xff09;**通过订阅频道或模式来接收消息。 其核心特点如…...

高级java每日一道面试题-2025年2月26日-框架篇[Mybatis篇]-Mybatis是如何将sql执行结果封装为目标对象并返回的?都有哪些映射形式 ?

如果有遗漏,评论区告诉我进行补充 面试官: Mybatis是如何将sql执行结果封装为目标对象并返回的?都有哪些映射形式 ? 我回答: 在Java高级面试中讨论MyBatis如何将SQL执行结果封装为目标对象并返回的过程时&#xff0c;我们可以从过程细节和映射形式两个方面来综合解答这个问…...

linux root丢失修改密

在RHEL7下重置密码 第一种方式&#xff1a;光驱进入急救模式 //做之前最好 selinuxdisabled Conntinue 然后chroot /mnt/sysimag 然后编辑/etc/shadow文件 第二种方式&#xff1a; 1&#xff1a;编辑启动菜单按e,找到linux16行&#xff0c;在行尾加入 init/bin/sh,同时在…...

OpenCV中文路径图片读写终极指南(Python实现)

文章目录 OpenCV中文路径图片读写终极指南&#xff08;Python实现&#xff09;一、问题深度解析1.1 现象观察1.2 底层原因 二、中文路径读取方案2.1 终极解决方案&#xff08;推荐&#xff09;2.2 快速修复 三、中文路径保存方案3.1 通用保存函数3.2 使用示例 四、技术原理详解…...

linux 时间同步(阿里云ntp服务器)

1、安装ntp服务 rootlocalhost ~]# yum -y install ntp 已加载插件&#xff1a;fastestmirror, langpacks Loading mirror speeds from cached hostfile* base: mirrors.nju.edu.cn* centos-sclo-rh: mirrors.nju.edu.cn* centos-sclo-sclo: mirrors.huaweicloud.com* epel: m…...

Go vs Rust vs C++ vs Python vs Java:谁主后端沉浮

一、核心性能对比(基于TechEmpower基准测试) 语言单核QPS延迟(ms)内存消耗适用场景Rust650,0000.1245MB高频交易/区块链C++720,0000.0932MB游戏服务器/实时渲染Go230,0000.45110MB微服务/API网关Java180,0001.2450MB企业ERP/银行系统Python12,0008.5220MBAI接口/快速原型技术…...

5 分钟搭建 Prometheus + Grafana 监控

一.安装 Prometheus cd /usr/local/ wget https://github.com/prometheus/prometheus/releases/download/v2.38.0/prometheus-2.38.0.linux-amd64.tar.gz tar xvf prometheus-2.38.0.linux-amd64.tar.gz ln -s prometheus-2.38.0.linux-amd64 prometheus二.安装 node_exporter…...

【机器人-基础知识】标定 - 相机内参求解原理(单应性矩阵、内参约束方程)

1. 求解目标&#xff1a;内参 从世界坐标系到像素坐标系的齐次坐标形式&#xff1a; s [ u v 1 ] K [ R t ] [ X w Y w Z w 1 ] s \begin{bmatrix} u \\ v \\ 1 \end{bmatrix} K \, [\, R \quad t \,] \begin{bmatrix} X_w \\ Y_w \\ Z_w \\ 1 \end{bmatrix} s ​uv1​ ​K…...

【QT】-一文说清楚QT定时器怎么用

在 Qt 中&#xff0c;定时器&#xff08;QTimer&#xff09;是用来定时执行某些任务的非常有用的类。它可以帮助你在指定的时间间隔后重复执行某个函数。常见的用法是启动一个定时器&#xff0c;每过一段时间自动执行某个操作&#xff0c;比如更新 UI、检查状态或发送数据等。 …...

QT编程之JSON处理

一、核心类库及功能 Qt 提供了一套完整的 JSON 处理类库&#xff08;位于 QtCore 模块&#xff09;&#xff0c;支持解析和生成 JSON 数据&#xff1a; ‌QJsonDocument‌&#xff1a;表示完整的 JSON 文档&#xff0c;支持从 QJsonObject 或 QJsonArray 初始化‌。‌QJsonOb…...

优选算法系列(1.双指针_下)

目录 五. 有效三角形的个数&#xff08;medium&#xff09; 题目链接&#xff1a;有效三角形的个数 解法: 代码&#xff1a; 六&#xff1a;和为 s 的两个数字&#xff08;easy&#xff09; 题目链接&#xff1a;和为 s 的两个数字 解法&#xff1a; 代码; 七&#xf…...

江科大51单片机笔记【15】直流电机驱动(PWM)

写在前言 此为博主自学江科大51单片机&#xff08;B站&#xff09;的笔记&#xff0c;方便后续重温知识 在后面的章节中&#xff0c;为了防止篇幅过长和易于查找&#xff0c;我把一个小节分成两部分来发&#xff0c;上章节主要是关于本节课的硬件介绍、电路图、原理图等理论…...

MS51FB9AE单片机解密实践与解析

MS51FB9AE 单片机解密实践与解析 在电子技术领域&#xff0c;MS51FB9AE 作为一款基于 8051 内核的单片机&#xff0c;凭借其强大功能与良好性能&#xff0c;在家用电器、智能仪表等诸多领域广泛应用&#xff0c;深受市场青睐。然而&#xff0c;厂家所采用的独特加密技术&#x…...

java 手搓一个http工具类请求传body

import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets;public class HttpUtil {/*** JSON请求发起*/public static String httpJsonRequest(String requestUrl, String requestJson) {String responseJson &…...

从零搭建微服务项目Pro(第2-2章——JSR303自定义文件校验+整合至微服务公共模块)

前言&#xff1a; JSR 303&#xff0c;即 Bean Validation&#xff0c;是 Java EE 6 中的一项子规范&#xff0c;旨在为 Java Bean 提供一种标准化的数据验证机制。它通过注解的方式&#xff0c;允许开发者在 Java 类的字段或方法上直接定义验证规则&#xff0c;从而将验证逻辑…...

Python 实现大文件的高并发下载

项目背景 基于一个 scrapy-redis 搭建的分布式系统&#xff0c;所有item都通过重写 pipeline 存储到 redis 的 list 中。这里我通过代码演示如何基于线程池 协程实现对 item 的中文件下载。 Item 结构 目的是为了下载 item 中 attachments 保存的附件内容。 {"crawl_tim…...

【论文笔记】Contrastive Learning for Compact Single Image Dehazing(AECR-Net)

文章目录 问题创新网络主要贡献Autoencoder-like Dehazing NetworkAdaptive Mixup for Feature PreservingDynamic Feature Enhancement1. 可变形卷积的使用2. 扩展感受野3. 减少网格伪影4. 融合空间结构信息 Contrastive Regularization1. 核心思想2. 正样本对和负样本对的构建…...

Java vs Go:SaaS 系统架构选型解析与最佳实践

在构建 SaaS&#xff08;Software as a Service&#xff09;系统时&#xff0c;选用合适的技术栈至关重要。Java 和 Go 是当今最受欢迎的后端开发语言之一&#xff0c;各自有其优势和适用场景。那么&#xff0c;SaaS 系统开发应该选择 Java 还是 Go&#xff1f;本文将从多个维度…...

OpenEuler-22.03-LTS上利用Ansible轻松部署MySQL 5.7

一、需求 使用ansible自动化部署mysql二进制部署mysql部署mysql并创建JDBC用户 二、环境信息 本文涉及的代码&#xff0c;配置文件地址&#xff1a; 链接&#xff1a;百度网盘 请输入提取码 提取码&#xff1a;1g6y 软件名称版本备注Ansible2.9.27All modules — Ansible Doc…...

vscode编译器的一些使用问题

目录 解决pip不可用问题 检查VSCode的终端配置 解决pip不可用问题 eg&#xff1a; C:\Users\student>pip pip 不是内部或外部命令&#xff0c;也不是可运行的程序或批处理文件。 先找到系统环境变量 高级->环境变量 系统属性->Path 变量名随意&#xff0c;自己后续知道…...

解决Windows版Redis无法远程连接的问题

&#x1f31f; 解决Windows版Redis无法远程连接的问题 在Windows系统下使用Redis时&#xff0c;很多用户会遇到无法远程连接的问题。尤其是在配置了Redis并尝试通过工具如RedisDesktopManager连接时&#xff0c;可能会报错“Cannot connect to ‘redisconnection’”。今天&am…...

MFC中使用Create或CreateDialog创建对话框失败,GetLastError错误码为1813(找不到映像文件中指定的资源类型)

文章目录 创建对话框失败示例、原因分析及解决方案示例代码错误原因解决方案 AFX_MANAGE_STATE(AfxGetStaticModuleState())作用一、功能1. 模块状态切换2. 自动状态恢复 二、为什么要用该函数&#xff1f;三、必须使用该宏的典型场景1. MFC 扩展 DLL&#xff08;Extension DLL…...

std::invoke详解

基础介绍 c17版本引入了std::invoke特性&#xff0c;这是一个通用的调用包装器&#xff0c;可以统一调用&#xff1a; 普通函数成员函数函数对象Lambda表达式指向成员的指针 它的主要作用是提供一个统一的方式来调用各种可调用对象。 std::invoke依赖的头文件&#xff1a;#…...

【Rust】枚举和模式匹配——Rust语言基础14

文章目录 1. 枚举类型1.2. Option 枚举 2. match 控制流结构2.1. match 对绑定值的匹配2.2. Option<T> 的匹配2.3. 通配模式以及 _ 占位符 3. if let 控制流4. 小测试 1. 枚举类型 枚举&#xff08;enumerations&#xff09;&#xff0c;也被称作 enums。枚举允许你通过…...

视频理解之Actionclip(论文宏观解读)

配合解读代码解读 1.研究背景 1. 视频行为识别的重要性 视频行为识别是视频理解领域的核心任务之一&#xff0c;旨在通过分析视频内容来识别和分类其中的人物行为或活动。这一任务在多个领域具有重要的应用价值&#xff0c;例如智能监控、人机交互、自动驾驶、医疗健康等。随…...

【论文精读】Deformable DETR:用于端到端目标检测可变形 Transformer

论文&#xff1a;DEFORMABLE DETR: DEFORMABLE TRANSFORMERS FOR END-TO-END OBJECT DETECTION 代码&#xff1a;Deformable-DETR 摘要 DETR 最近被提出用于消除目标检测中许多手工设计组件的需求&#xff0c;同时展示了良好的性能。然而&#xff0c;它存在收敛速度慢和特征空…...

Odoo18 Http鉴权+调用后端接口

最近在调研Odoo18&#xff0c;包括它的前后端原理、源码等。发现官方的开发文档并不十分实用&#xff0c;比如标题这种简单的实用需求&#xff0c;竟然浪费了一点时间&#xff0c;特此记录。 官方文档&#xff1a;External API — Odoo 18.0 documentation 前提&#xff1a;首…...

doris:SQL 方言兼容

提示 从 2.1 版本开始&#xff0c;Doris 可以支持多种 SQL 方言&#xff0c;如 Presto、Trino、Hive、PostgreSQL、Spark、Clickhouse 等等。通过这个功能&#xff0c;用户可以直接使用对应的 SQL 方言查询 Doris 中的数据&#xff0c;方便用户将原先的业务平滑的迁移到 Doris…...

Linux红帽:RHCSA认证知识讲解(六)创建、管理和删除本地用戶和组

Linux红帽&#xff1a;RHCSA认证知识讲解&#xff08;六&#xff09;创建、管理和删除本地用戶和组 前言一、用户和组概念用户类型对比表格主要组和补充组对比表格&#xff1a; 二、本地用户账户增删改查三、本地组账户 前言 上篇博客我们详细了解了从红帽和 DNF 软件仓库下载…...

【Repos系列】yum install nginx 是怎么从仓库中下载并安装的?

yum install nginx 是 YUM 包管理工具从配置的软件仓库中下载并安装软件包的核心操作。以下是其完整工作流程的详细步骤&#xff08;结合缓存机制和依赖处理&#xff09;&#xff1a; ​1. 隐式元数据同步&#xff08;若缓存过期&#xff09;​ ​检查缓存有效性&#xff1a;…...

《JavaScript高级程序设计(第5版)》学习大纲

《JavaScript高级程序设计&#xff08;第5版&#xff09;》学习大纲 《JavaScript高级程序设计&#xff08;第5版&#xff09;》是JavaScript领域的经典“红宝书”&#xff0c;它从基础语法讲到高级特性&#xff0c;还包含浏览器环境和前端工程化等内容。本书2024年12月出版&a…...

基于微信小程序开发的宠物领养平台——代码解读

项目前端 一、项目的技术架构概况 一句话概括&#xff1a;该项目是基于微信小程序开发的宠物领养平台&#xff0c;采用原生小程序框架进行用户界面的构建&#xff0c;使用 wx.request 进行 API 请求&#xff0c;并通过 getApp() 和本地存储来管理全局状态和用户信息。 一&am…...

KICK第五课:Mac 系统下安装 Xcode 或 Clang

Mac 系统下安装 Xcode 或 Clang 详细指南 一、安装前的准备 确认系统版本 macOS 10.9 及以上版本支持 Xcode 和 Clang。若版本过低&#xff0c;需先升级系统。 了解工具区别 Xcode&#xff1a;苹果官方 IDE&#xff0c;包含完整开发环境、模拟器、调试工具等&#xff0c;适合…...

PHP语法基础

PHP语法基础 一&#xff0c;变量 在PHP中&#xff0c;变量是存储数据的容器&#xff0c;其灵活性和动态类型系统是PHP的核心特性之一。以下是PHP变量的详细解析&#xff0c;涵盖声明、作用域、类型转换及最佳实践&#xff1a; 1. 变量基础 声明与命名规则 无需显式声明类型&…...

嵌入式硬件: GPIO与二极管基础知识详解

1. 前言 在嵌入式系统和硬件开发中&#xff0c;GPIO&#xff08;通用输入输出&#xff09;是至关重要的控制方式&#xff0c;而二极管作为基础电子元件&#xff0c;广泛应用于信号整流、保护电路等。本文将从基础原理出发&#xff0c;深入解析GPIO的输入输出模式&#xff0c;包…...

母婴商城系统Springboot设计与实现

项目概述 《母婴商城系统Springboot》是一款基于Springboot框架开发的母婴类电商平台&#xff0c;旨在为母婴产品提供高效、便捷的在线购物体验。该系统功能全面&#xff0c;涵盖用户管理、商品分类、商品信息、商品资讯等核心模块&#xff0c;适合母婴电商企业或个人开发者快…...

Redis相关面试题

以下是150道Redis相关面试题&#xff1a; Redis基础概念 1. Redis是什么&#xff1f; Redis是一个开源的、基于内存的高性能键值存储数据库&#xff0c;常用于缓存、消息队列等场景。 2. Redis的特点有哪些&#xff1f; • 高性能&#xff0c;读写速度快。 • 支持多种数据…...

ArcGIS助力水文分析:数据处理、地图制作与流域特征提取

在水文水环境保护中&#xff0c;对于信息的采集、处理和分析是关键步骤。水文水环境及其相关数据均具有空间分布特征&#xff0c;传统的方法难以发挥作用。地理信息系统&#xff08;GIS&#xff09;强大的空间数据管理和分析功能&#xff0c;在空间信息处理上有独到的优势&…...

docker桌面版启动redis,解决无法连接

docker run -d --name redis -p 6379:6379 -v E:\2\redis\redis.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf 在本地创建一个目录&#xff0c;里面有个redis.conf文件&#xff0c;内容如下&#xff0c;启动时绑定这个配置文件目…...

Vue 项目中 CDN 引入的利弊及解决方案

在Vue项目中&#xff0c;引入到工程中的所有js、css文件&#xff0c;编译时都会被打包进vendor.js&#xff0c;浏览器在加载该文件之后才能开始显示首屏。若是引入的库众多&#xff0c;那么vendor.js文件体积将会相当的大&#xff0c;影响首屏的体验。通过调试发送时间主要消耗…...

【QT】】qcustomplot的使用

1.下载并添加qcustomplot.c和qcustomplot.h文件 拖动一个Widget&#xff0c;提升为qcustomplot 成功后是这样的&#xff0c; 改第三行&#xff1a;greaterThan(QT_MAJOR_VERSION, 4): QT widgets printsupport 编译&#xff0c;不报错&#xff0c;出现带坐标轴的界面&#…...

第三周日志-web(2)

原本计划的web不是这个&#xff0c;但是b站上一个大佬讲web做到了连我都能听懂&#xff0c;不趁热打铁学一学记一记就怕忘记了 指路&#xff1a;Shiro反序列化漏洞(一)-shiro550流程分析_哔哩哔哩_bilibili khttps://www.zhihu.com/question/486555909 学模板先看看结构和功…...

KICK第四讲Linux 系统下安装 GCC 编译器全指南

Linux 系统下安装 GCC 编译器全指南 GCC&#xff08;GNU Compiler Collection&#xff09;是 Linux 系统下最常用的编译器之一&#xff0c;支持 C/C、Java 等多种编程语言。本文将介绍不同 Linux 发行版下的安装方法&#xff0c;帮助开发者快速配置开发环境。 一、使用包管理…...