Toluapp 類別存取標籤 |
|
當 `access` 值為 `nil`、`false` 或 `0` 時,物件即視為公開。其他任何值都表示非公開,而 tolua++ 都不在乎。所有物件的預設存取值為 `nil`,所以這個新功能並不會影響目前的使用者。
這個範例會實作 'parser_hook',以尋找標籤,並將 `curr_member_access` 設為目前的存取權。它也會將 `curr_member_access` 設為私人(根據 `curr_member_access` 的預設值 `nil`)。標籤仍然能在其他容器(模組、名稱空間和主套件)中作用,但預設會是公開。
-- access_hook.lua local access = {public = 0, protected = 1, private = 2} function preparse_hook(p) -- we need to make all structs 'public' by default p.code = string.gsub(p.code, "(struct[^;]*{)", "%1\npublic:\n") end function parser_hook(s) local container = classContainer.curr -- get the current container if not container.curr_member_access and container.classtype == 'class' then -- default access for classes is private container.curr_member_access = access.private end -- try labels (public, private, etc) do local b,e,label = string.find(s, "^%s*(%w*)%s*:[^:]") -- we need to check for [^:], otherwise it would match 'namespace::type' if b then -- found a label, get the new access value from the global 'access' table if access[label] then container.curr_member_access = access[label] end -- else ? return strsub(s, e) -- normally we would use 'e+1', but we need to preserve the [^:] end end end
用法
$ tolua++ -L access-hook.lua package.pkg