TAG

首都機能移轉 (2) 歌詞 (2) 靠北文 (40) 戲言 (30) 糟糕 (7) ACG (23) Assembly (2) Boost (2) C (31) C++ (69) CMake (4) CSIE (67) Debian (34) Design_Pattern (2) Django (1) Eclipse (1) en_US (13) FFmpeg (3) FoolproofProject (26) FreeBSD (2) Git (4) GNU_Linux (65) IDE (5) Java (11) JavaScript (19) KDE (15) Khopper (16) KomiX (3) Kubuntu (18) Life (1) Lighttpd (2) Mac_OS_X (2) Opera (1) PHP (2) PicKing (2) Programing (21) Prolog (1) Python (7) QSnapshot (2) Qt (30) Qt_Jambi (1) Regular_Expression (1) Shell_Script (7) Talk (98) VirtualBox (7) Visual_Studio (13) Windows (18) zh_TW (36)

2012年8月3日 星期五

Garbage Collection (2)

上一篇提到了 reference counting 以及 weak reference,這篇我將以 C++11 的 smart pointer 為骨幹做介紹,但核心概念都是差不多的。
C++11 引入了一系列的 smart pointer,使用 RAII (Resource Acquisition Is Initialization) 手法來確保資源在生命週期結束後的回收行為。想法非常簡單:配置在 stack 上的變數,在離開 scope 後一定會呼叫 destructor,因此只要設計一系列的容器,讓它們在 destructor 裡做回收行為,就可以讓編譯器為我們回收資源。C++11 的 smart pointer 實際上不只是自動化的 new/delete 容器,因為其 allocator/deleter 可自訂,programmer 實際上可用它來管理任何種類的資源。

2012年8月2日 星期四

CMake: grouping sources by directory

function(group_sources root)
  function(__group_sources__ root prefix)
    file(GLOB entries RELATIVE "${root}" "${root}/*")
    foreach(entry ${entries})
      set(abspath "${root}/${entry}")
      if(IS_DIRECTORY "${abspath}")
        __group_sources__("${abspath}" "${prefix}/${entry}")
      else()
        list(APPEND group "${abspath}")
      endif()
    endforeach()
    string(REPLACE "/" "\\" prefix "${prefix}")
    source_group("${prefix}" FILES ${group})
  endfunction()

  get_filename_component(prefix "${root}" NAME)
  __group_sources__("${root}" "${prefix}")
endfunction()
Usage:
group_sources("${CMAKE_SOURCE_DIR}/src")
group_sources("${CMAKE_SOURCE_DIR}/include")
Note this function only creates filters in project, you still need to add source files by ADD_EXECUTABLE or ADD_LIBRARY commands. For more complete example, see this gist.