平民程序 - linghuye's blog

天下风云出我辈,一入江湖岁月催。皇图霸业谈笑中,不胜人生一场醉。提剑跨骑挥鬼雨,白骨如山鸟惊飞。尘事如潮人如水,只笑江湖几人回。

随笔 - 221, 文章 - 0, 评论 - 680, 引用 - 0
数据加载中……

Lua概念备忘

1.当处理数据文件时,一般总是写比读来得简单,因为写文件时我们有完全的控制,读时却不能确保碰上什么样的数据,我们需要保证健壮的程序能优雅地处理各种各样的错误数据.

2.
Entry
{
 title = "Tecgraf",
    org = "Computer Graphics Technology Group, PUC-Rio",
    url = "http://www.tecgraf.puc-rio.br/",
    contact = "Waldemar Celes",
};
语法含义是Entry函数调用,传入参数为指定的table.

3.a.Metatables allow us to change the behavior of a table.
  b.Each table in Lua may have its own metatable.
  c.Lua always create new tables without metatables.
  d.use setmetatable to set or change the metatable of any table
  e.Any table can be the metatable of any other table.
  f.A group of related tables may share a common meta.
  g.A table can be its own metatable.
  h.__add: +
    __mul: *
    __sub: -
    __div: /
    __unm: 取负
    __pow: 次方
    __concat: ..
    __eq: equality
    __lt: less than
    __le: less or equal
    __tostring
    __metatable
    __index
    __newindex

  i:If the first value has a metatable with an __add field, Lua uses this value as the metamethod, independently of the second value; (2) otherwise, if the second value has a metatable with an __add field, Lua uses this value as the metamethod; (3) otherwise, Lua raises an error.
4.将light user data作为指针使用,并在脚本环境中维护一个以lightuserdata为key的table,value为对应的lua对象,则有,key为C++对象,value为lua对象.
5.userdata本质为一段内存区,在脚本中可作为单一参数进行传递.可为之定义脚本函数以操作该内存,一簇的函数形成一个对象概念,一个内存对象概念.
6.userdata可以有metatable,意味着其可以完成完整的C++对象语义. 检查userdata的metatable(luaL_checkudata)以确保该userdata是所期望的内存对象.
7.Remember that a:size() is equivalent to a.size(a). Therefore, we have to arrange for the expression a.size to return our getsize function. The key mechanism here is the __index metamethod. For tables, this metamethod is called whenever Lua cannot find a value for a given key. For userdata, it is called in every access, because userdata have no keys at all.
8.设置metatable的__index为get方法,__newindex设为set方法,以实现[]的语法语义.
9.设置metatable的__index为metatable自身,并定义get,set,size函数,以实现C++对象语法语义

posted on 2007-11-12 20:49 linghuye 阅读(1902) 评论(0)  编辑 收藏 引用 所属分类: 游戏理论和技术

只有注册用户登录后才能发表评论。