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)
顯示具有 zh_TW 標籤的文章。 顯示所有文章
顯示具有 zh_TW 標籤的文章。 顯示所有文章

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.

2012年5月14日 星期一

QSnapshot 0.1.1 released

其實我一直覺得這頁面很醜 ... 不過我真的不知道怎麼改.
本次修正包含了數項 bug 及 UI 改善.
Debian 有套件包可用, Ubuntu 提供 PPA.

2012年5月12日 星期六

How to debug a program which grabs X11 input events

最近在做的抓圖程式, 因為要在非 focus 狀態也能接收到抓圖要求, 必須取得所有的輸入事件控制權.
但如果必須要在這種地方下中斷點, 因為 X11 所有的輸入都被搶走了, 你無法操作 gdb.

2012年4月18日 星期三

About the rvalue reference

之前拜讀過 yoco315 所翻譯的 Rvalue References 文章, 把原本相當複雜的概念寫得非常淺顯易懂, 讓我當時忍不住手癢實驗了一下.
由於這是一個全新的特性, 當時還沒有很完整的 coding standard 可參考, 因此越寫疑惑就越深, 最後就先把注意力轉移到其他特性, 如 lambda expression.
今天的 OSDC.tw 很幸運地遇到 fr3@K 給了一場 C++11 的 talk, 讓我又再次燃起熱血, 便藉著指導新生的機會也來順便練習 class design.

2012年3月9日 星期五

QSnapshot

買了 Mac,卻發現 Mac 上的抓圖程式沒辦法抓視窗的子元件,於是我偷了 KSnapshot 的程式碼,偷了 Apple 上的範例程式,做出這個適用於 Mac OS,Windows 與 Linux 的抓圖程式。

2011年2月2日 星期三

Lambda binding issue in VC10

VC10 的 lambda expression 的 bug 還真多 ... 這應該是我第三次踩到相關的 bug 了吧。
auto f( std::bind( []( int a, int b )->int {
    return a + b;
}, std::placeholders::_1, 2 ) );

f( 1 ); // returns 3

2010年6月12日 星期六

Phonon with raw PCM

由於 Phonon 在運作上會把讀到的東西轉交給它的後端,如果你已經有了一個原始的 PCM 資料,那麼在餵給 Phonon 時要記得填上 wav 的 header,才能讓後端辨識並找到合適的方式開啟它。

2010年5月22日 星期六

Templates needs no "__dllspec"

之前提到的, Visual C++ 要做 export/import symbol 的動作才能連結 dll, 但是 template classes 和 functions 是不需要的 ... 原因很單純, 就是 template 在 Visual C++ 是原地展開的, 不需要再額外做 symbol 的定義[?].

2010年4月2日 星期五

Converting literal

為了避免荒廢,來貼個廢文充數好了。
上一篇有提到 string literal 的問題,這是把文字轉成 hex 的 C 程式,不過要用 C99 去編譯才行。
#include <stdio.h>

int main() {
  for( char tmp[1024]; fgets( tmp, sizeof( tmp ), stdin ) != NULL; ) {
    for( unsigned char * c = tmp; *c; ++c ) {
      if( *c == '\n' ) {
        printf( "\n" );
      } else {
        printf( "\\x%02X", *c );
      }
    }
  }
  return 0;
}

2010年3月14日 星期日

Using UTF-8 literals in source code

直接在 code 裡打 UTF-8 字元在有的 IDE 會無法辨識,像是 Visual C++ 2005 和 2008 堅持使用 locale 的編碼來解讀 source,導致換 locale 就會編譯失敗。
這種時候沒什麼有效的解決方法,就是用 hex escape 字元啦 ...
/* 正體中文 in UTF-8 */
const char * utf8 = "\xE6\xAD\xA3\xE9\xAB\x94\xE4\xB8\xAD\xE6\x96\x87";
很煩,很難讀,不過至少所有的字元都保證在 C / C++ 的標準字元集裡;只是最好在旁加上註解說明那應該是什麼字,否則很難維護。

2010年3月12日 星期五

Forward declaration with template parameters

在避免編譯時間拉長的時候,forward declaration 是個很好的選擇。但是有一些時候你無法使用 forward declaration,一個是該型別其實是個 typedef 的時候,另一個是當這個型別用在 template parameter 的時候。
由於 template class 經常是 inline 的,所以它可能在 header 就必須要知道 template parameter 的大小或成員這些細節。這導致你必須要 include definition 或是改用 pointer。
你說 smart pointer? 不會吧?(笑)

2010年3月10日 星期三

Export the symbols of QtSingleApplication

現在 Qt Solution 只能在 git 上取得了。
若想要編成 library,在 configure 參數加上 -library 即可:
configure -library
qmake
make # or nmake for MSVC
真正需要的 header 只有 QtSingleApplication, qtsingleapplication.h, qtcoresingleapplication.h。
The following content is obsolete. Latest update: 2011/04/23
Qt Solutions 的慣例似乎是把它內附的 pri include 到 qmake 的專案檔內,直接把原始檔包進去編譯,而不會做出封裝檔[?]。但是這種做法只適用於用 qmake 管理的專案檔內,且編譯時間可能會拉長。如果想弄出封裝檔,就要自己對它作一點手腳。
QtSingleApplication 為例,可以對 buildlib/buildlib.pro 動手腳。這個設定主要是給 dynamic loading 使用的,只要在 CONFIG 加上 shared 就可以了。
-CONFIG += qt dll qtsingleapplication-buildlib
+CONFIG += qt dll qtsingleapplication-buildlib shared

2010年2月15日 星期一

Create source groups in Visual C++ solutions with CMake

使用 qmake 時,Visual C++ 的方案會自動把產生的檔案(e.g.: ui_*.h, moc_*.cpp ... etc.)歸類到 Generated Files,讓原始碼樹看起來比較清爽。但是 CMake 不會幫你做這件事,專案一大就會讓這些不必修改的檔案佔滿整個專案。
但是 CMake 其實是可以做到的,使用 SOURCE_GROUP 巨集就可以建造一個群組,把原始碼組織起來。過濾的方式有二:使用 regular expression,或是直接指明檔案路徑。以下是範例。
# 明指 moc, rcc, uic, qm 檔案為 Generated Files
source_group("Generated Files" FILES ${MOC_SOURCES} ${RCC_RESOURCES} ${UIC_HEADERS} ${QM_FILES})
# 使用 RE 把資源檔找出來
source_group("Resource Files" REGULAR_EXPRESSION .*\\.rc)
# 使用 RE 把 Designer forms 歸類在 Form Files
source_group("Form Files" REGULAR_EXPRESSION .*\\.ui)
注意 regular expression 要跳出 \。