• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            OldJiang.com

            浩毛的博客

            OldJiang.com
            posts - 14, comments - 81, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            Lua的weak table和Userdata

            Posted on 2011-08-11 14:33 浩毛 閱讀(7047) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Lua腳本
            weak table是什么意思,建議不要看中文翻譯的,我以前看了半天還是沒(méi)懂啥子意思.

            lua 手冊(cè)原文是這樣解釋的:
            A weak table is a table whose elements are weak references. A weak reference is ignored by the garbage collector. In other words, if the only references to an object are weak references, then the garbage collector will collect this object.
            A weak table can have weak keys, weak values, or both. A table with weak keys allows the collection of its keys, but prevents the collection of its values. A table with both weak keys and weak values allows the collection of both keys and values. In any case, if either the key or the value is collected, the whole pair is removed from the table. The weakness of a table is controlled by the __mode field of its metatable. If the __mode field is a string containing the character 'k', the keys in the table are weak. If __mode contains 'v', the values in the table are weak.
            After you use a table as a metatable, you should not change the value of its __mode field. Otherwise, the weak behavior of the tables controlled by this metatable is undefined.

            在lua中,像table,userdata,function這些類(lèi)型的值都是引用傳遞,通過(guò)引用計(jì)數(shù)來(lái)判斷是否收掉對(duì)象,而弱引用(weak reference)會(huì)被垃圾回收器忽略.weak table就是它的元素是弱引用的,一個(gè)元素(鍵值對(duì))可能鍵是弱引用,也可能值是弱引用的,也可能都是弱引用, 這個(gè)特性是通過(guò)弱表的metatable的__mode的值來(lái)設(shè)置的,特別有意思的是,當(dāng)弱表中一個(gè)鍵值對(duì),它的鍵或值關(guān)聯(lián)(引用/指向)的那個(gè)對(duì)象被垃圾回收器回收的時(shí)候,這個(gè)鍵值對(duì)會(huì)從弱表中被自動(dòng)刪除掉.這是個(gè)很重要的特點(diǎn).

            那么弱表到底有什么用呢? 在lua的wiki中有一篇使用userdata的例子 ,其中就很巧妙的用到了弱表,原文地址 http://lua-users.org/wiki/CppBindingWithLunar
            這篇文章介紹了如何通過(guò)userdata綁定c++對(duì)象到腳本中
            fulluserdata能夠設(shè)置metatable,也就能模擬出對(duì)象的效果出來(lái),對(duì)一個(gè)C++的類(lèi)的對(duì)象實(shí)例來(lái)說(shuō),push到腳本中,一般是創(chuàng)建了一個(gè)userdata,文章中用弱表避免了同一個(gè)對(duì)象實(shí)例(指針) push到腳本中,多次創(chuàng)建userdata的問(wèn)題.
            換句話來(lái)說(shuō),如果C++對(duì)象的生存周期是靠lua的垃圾回收來(lái)控制的話(userdata被回收時(shí),調(diào)用元表的__gc方法,__gc方法中析構(gòu)c++對(duì)象),一個(gè)C++對(duì)象只能有一個(gè)唯一的userdata. 在userdata的metatable中創(chuàng)建一個(gè)值是弱引用的弱表,用C++對(duì)象指針做鍵,每次push c++對(duì)象的時(shí)候,就去用指針值查弱表,如果有,就push那個(gè)userdata,沒(méi)有就創(chuàng)建,  同時(shí),當(dāng)userdata是被弱引用的,當(dāng)被垃圾回收掉的時(shí)候,弱表中它所在的鍵值對(duì)自動(dòng)被銷(xiāo)毀了. 




            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            OldJiang.com
            性欧美丰满熟妇XXXX性久久久| 久久九九精品99国产精品| 久久毛片免费看一区二区三区| 久久乐国产精品亚洲综合| 精品久久久久成人码免费动漫| 久久精品国产第一区二区三区| 狠狠色丁香久久综合五月| 三级韩国一区久久二区综合 | 亚洲一本综合久久| 亚洲精品久久久www| 97久久精品午夜一区二区| 国产午夜精品久久久久九九| 精品久久亚洲中文无码| 久久se这里只有精品| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 中文字幕日本人妻久久久免费 | 国产香蕉97碰碰久久人人| 伊人精品久久久久7777| 国内精品久久久久久不卡影院| 久久久久亚洲av成人网人人软件| 狠狠久久综合| 久久精品国产69国产精品亚洲| 伊人久久精品无码av一区| 人人狠狠综合久久亚洲高清| 国产成人久久激情91| 久久久久人妻一区二区三区 | 色综合久久综合中文综合网| 国内精品久久久久久久久| 久久精品国产91久久综合麻豆自制 | 久久久久人妻精品一区二区三区| 久久亚洲AV无码精品色午夜 | 久久精品99无色码中文字幕| 久久国产成人精品麻豆| 久久精品无码午夜福利理论片 | 久久这里有精品视频| 国产精品免费久久久久电影网| 国产精品久久久久久一区二区三区 | 亚洲精品视频久久久| 久久男人中文字幕资源站| 日日狠狠久久偷偷色综合96蜜桃 | 思思久久99热只有频精品66|