• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            Lua的weak table和Userdata

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

            lua 手冊原文是這樣解釋的:
            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這些類型的值都是引用傳遞,通過引用計數來判斷是否收掉對象,而弱引用(weak reference)會被垃圾回收器忽略.weak table就是它的元素是弱引用的,一個元素(鍵值對)可能鍵是弱引用,也可能值是弱引用的,也可能都是弱引用, 這個特性是通過弱表的metatable的__mode的值來設置的,特別有意思的是,當弱表中一個鍵值對,它的鍵或值關聯(引用/指向)的那個對象被垃圾回收器回收的時候,這個鍵值對會從弱表中被自動刪除掉.這是個很重要的特點.

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



            OldJiang.com
            久久人人爽人人爽人人AV东京热| 四虎国产精品免费久久5151| 99久久精品国产高清一区二区| 久久天天躁狠狠躁夜夜网站| 久久国产高清字幕中文| 亚洲伊人久久综合中文成人网| 久久99国产乱子伦精品免费| 久久av高潮av无码av喷吹| 久久久久久伊人高潮影院| 国产午夜精品理论片久久| 亚洲中文字幕无码久久精品1| 91精品日韩人妻无码久久不卡| 99久久做夜夜爱天天做精品| 99久久精品国产高清一区二区| 久久无码AV一区二区三区| 久久久综合九色合综国产| 精品无码久久久久国产动漫3d| 国产成人综合久久精品尤物| 久久精品久久久久观看99水蜜桃| 久久精品国产精品亚洲| 久久福利青草精品资源站免费| 久久精品人妻中文系列| 欧美亚洲国产精品久久| 久久久噜噜噜久久| 久久精品无码一区二区三区日韩| 久久综合丁香激情久久| 国产精品久久国产精品99盘| 亚洲AV无码久久精品色欲| 欧美伊人久久大香线蕉综合 | 久久久久亚洲AV成人网人人网站 | 国产精品久久久久久久人人看| 国产福利电影一区二区三区久久老子无码午夜伦不 | 免费一级做a爰片久久毛片潮| 亚洲国产精品人久久| 女人香蕉久久**毛片精品| 国产精品99久久久久久人| 久久国产一区二区| 国产高潮久久免费观看| 麻豆久久久9性大片| 无码国产69精品久久久久网站| 久久国产热精品波多野结衣AV|