分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

metatable

发布时间:2023-09-06 01:52责任编辑:熊小新关键词:meta

元表在我们平时的开发中应用的不多,最熟悉的要数lua中的面向对象实现。今天就总结下metatable的使用,底层原理,以及大神们提供的使用场景。

metatable是什么?

 简单一句话,是lua提供给我们的一种操作table的方法。

metatable也是table,从源码中我们看到:

typedef struct Table { ?CommonHeader; ?lu_byte flags; ?/* 1<<p means tagmethod(p) is not present */ ?lu_byte lsizenode; ?/* log2 of size of ‘node‘ array */ ?unsigned int sizearray; ?/* size of ‘array‘ array */ ?TValue *array; ?/* array part */ ?Node *node; ?Node *lastfree; ?/* any free position is before this position */ ?struct Table *metatable; ?GCObject *gclist;} Table;

metatable相关的api只有两个:getMetatablesetMetatable

getmetatable (object)If object does not have a metatable, returns nil. Otherwise, if the object‘s metatable has a "__metatable" field, returns the associated value. Otherwise, returns the metatable of the given object.
setmetatable (table, metatable)
Sets the metatable for the given table. (You cannot change the metatable of other types from Lua, only from C.) If metatable is nil, removes the metatable of the given table. If the original metatable has a "__metatable" field, raises an error.

两个方法都提到了__metatable。使用__metatable可以保护元表,禁止用户访问元表中的成员或者修改元表。 

metamethod

元表中预定义了一些元方法,我们也只能对这些元方法自定义。我参照源码中的枚举值给出对应的lua中的元方法名,以及对应的使用场景:

typedef enum { ?TM_INDEX, ?TM_NEWINDEX, ?TM_GC, ?TM_MODE, ?TM_LEN, ?TM_EQ, ?/* last tag method with fast access */ ?TM_ADD, ?TM_SUB, ?TM_MUL, ?TM_MOD, ?TM_POW, ?TM_DIV, ?TM_IDIV, ?TM_BAND, ?TM_BOR, ?TM_BXOR, ?TM_SHL, ?TM_SHR, ?TM_UNM, ?TM_BNOT, ?TM_LT, ?TM_LE, ?TM_CONCAT, ?TM_CALL, ?TM_N ???????/* number of elements in the enum */} TMS;
enummetamethodapplication
TM_INDEX
__index  t[key],取值
TM_NEWINDEX
__newindext[key] = value, 赋值
TM_GC
__gccollectgarbage
TM_MODE
__mode  weak table
TM_LEN
__len  #
TM_EQ
__eq==
TM_ADD
__add+
TM_SUB
__sub-
TM_MUL
__mul*
TM_MOD
__mod%
TM_POW
__pow^
TM_DIV
__div/
TM_IDIV
__idiv  //   向下取整除法
TM_BAND
__band&   按位与
TM_BOR
__bor|    按位或
TM_BXOR
__bxor~   按位异或
TM_SHL
__shl<<  左移
TM_SHR
__shr>>  右移
TM_UNM
__unm-      取负
TM_BNOT
__bnot~     按位非
TM_LT
__lt<     小于
TM_LE
__le<=   小于等于
TM_CONCAT
__connect..
TM_CALL
__callfunc(args),  非函数类型的函数调用

 

 

metatable

原文地址:https://www.cnblogs.com/jimobuwu/p/8980808.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved