Calendar

二月 2012
« 十二月    
 12345
6789101112
13141516171819
20212223242526
272829  

Categories

用 Boost.Spirit 搭 std::string 使用時容易遇到的問題

這個問題太多人問了,
所以還是先寫下來當成 FAQ 用;
官方文件的 quick start 這一章是類似下面這樣教:

parse("ffff 1234 abcde", +hex_p, space_p);

加上 The Scanner and Parsing 那一小節也提到 pharse-level parsing 有兩種版本:

    template <typename IteratorT, typename ParserT, typename SkipT>
    parse_info<iteratort>
    parse
    (
        IteratorT const&        first,
        IteratorT const&        last,
        parser<parsert> const&  p,
        parser<skipt> const&    skip
    );
 
    template <typename CharT, typename ParserT, typename SkipT>
    parse_info<chart const*>
    parse
    (
        CharT const*            str,
        parser<parsert> const&  p,
        parser<skipt> const&    skip
    );
</skipt></parsert></chart></typename></skipt></parsert></iteratort></typename>

導致有人寫出這種 code 來結果編不過在那邊哭:

rule<> r = +hex_p;
string str("ffff 1234 abcde");
 
parse(str.begin(), str.end(), r, space_p);

Continue reading 用 Boost.Spirit 搭 std::string 使用時容易遇到的問題 »

Boost.Serialization

在設計 editor、game 或是實現軟體系統中的 heuristic algorithm 等情況下,
常需要設計 save/load 或 undo 之類的還原功能。
學過 design pattern 的人大都知道 memento 和 command 這兩個 pattern。
通常比較簡單的東西靠 command pattern 就能順便做出 undo 功能了;
但是在其它比較複雜的狀況下就需要靠 memento pattern 來幫忙,
將物件的狀態儲存起來以便隨時復原。
有些語言如 Java 提供了將物件本身序列化 (serialize) 的機制,
所謂序列化就是把物件狀態變成一串資料儲存起來。
這樣就可以做到和 memento 相同的功能,
更進一步的話還能將序列化後的資料存在 disk 上或透過網路傳遞。
C++ 語言本身沒有支援 serialization 的功能,
所以 boost 以 library support 的形式做了一個類似的。
內建的支援有 binary、text 和 xml 三種輸出格式。
這篇只會出現 text,
反正用法都沒差 (唯一要注意的就是 binary 並非 portable)。

Continue reading Boost.Serialization »

Boost.MultiIndex

很久沒發程式文了。
最近常到處在網路上亂逛,
發現 boost 在台灣真的是沒什麼人在用。
應該說 C++ 普及的程度實在太低了,
看來還是放一點東西比較好...
不過用 1280x1024 解析度 + IE7 來讀這篇大概 code 右半邊會無法顯示,
懶得玩複製貼上的話就乖乖切到 1920x1200 或用 firefox 吧 (字會小一點但是很難看);
不然就等我有空去改改 css。

Continue reading Boost.MultiIndex »