SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Boostライブラリ一周の旅
                       ver.1.44.0



                 高橋晶(Akira Takahashi)

   ブログ:「Faith and Brave – C++で遊ぼう」
    http://d.hatena.ne.jp/faith_and_brave/
                      Boost.勉強会#2 2010/09/11(Sat)
はじめに



前回は、Boost 1.40.0までを紹介しました。

今回は1.44.0までの差分を紹介します。
本日紹介するライブラリ


1. Property Tree
2. Uuid
3. Range 2.0
4. Filesystem v3
5. Polygon
6. Meta State Machine(MSM)
Property Tree 1/4
汎用的な、木構造をもつデータのプロパティ管理。
XML、JSON、INIファイルのパーサーを提供している。

全てのデータは、
boost::property_tree::ptree型
に対して操作を行う。

値の取得には、
失敗時に例外を投げるptee::get<T>()と
boost::optionalを返すptree::get_optional<T>()
が用意されている。
Property Tree 2/4
 XMLの読込、要素、属性の取得。
 XMLパーサーにはRapidXmlを採用している。
                                          <root>
                                           <elem attr="World">
                                             Hello
using namespace boost::property_tree;      </elem>
                                          </root>
ptree pt;
read_xml("test.xml", pt, xml_parser::trim_whitespace);

// 要素の取得
const string& elem = pt.get<string>("root.elem");

// 属性の取得 : <xmlattr>という特殊な要素名を介してアクセスする
const string& attr = pt.get<string>("root.elem.<xmlattr>.attr");
Property Tree 3/4
 JSONの読込、データの取得。                         {
                                             "Data": {
                                               "Value": 314,
                                               "Str": "Hello"
                                             }
using namespace boost::property_tree;    }

ptree pt;
read_json("test.json", pt);

const int     value = pt.get<int>("Data.Value");
const string& str = pt.get<string>("Data.Str");
Property Tree 4/4
 iniの読込、データの取得。                          [Data]
                                         Value = 314
                                         Str = Hello


using namespace boost::property_tree;

ptree pt;
read_ini("test.ini", pt);

const int     value = pt.get<int>("Data.Value");
const string& str = pt.get<string>("Data.Str");
Uuid
 ユニークIDの生成。
 COMとか、分散環境での情報の識別とかで
 使われることが多い。

using namespace boost::uuids;

// 擬似乱数生成器でのUUID生成。デフォルトはmt19937
uuid u1 = random_generator()();

// 文字列からUUID生成
uuid u2 = string_generator()("0123456789abcdef0123456789abcdef");

cout << u1 << endl;
cout << u2 << endl;

                      31951f08-5512-4942-99ce-ae2f19351b82
                      01234567-89ab-cdef-0123-456789abcdef
Range2.0 1/2
 ユーティリティ程度だったBoost.Rangeに、
 RangeアルゴリズムとRangeアダプタを拡張。

std::vector<int> v;

// Rangeアルゴリズム : イテレータの組ではなく範囲を渡す
boost::sort(v);
boost::for_each(v, f);

// Rangeアダプタ
using namespace boost::adaptors;
boost::for_each(v | filtered(p) | transformed(conv), f);


 Rangeアルゴリズム : STLアルゴリズムのRange版
 Rangeアダプタ   : 遅延評価され、合成可能な範囲操作
Range 2.0 2/2
 Boost.Foreachと組み合わせて使っても便利。
using namespace boost::adaptors;
std::map<std::string, int> m;

// mapのキーのみを操作
BOOST_FOREACH (const std::string& key, m | map_keys) {
  // something...
}

// mapの値のみを操作
BOOST_FOREACH (const int value, m | map_values) {
  // なにか・・・
}


  RangeライブラリとしてはOvenも強力なのでそちらもチェックしてください!
Filesystem v3
 pathの日本語対応等。
 stringとwstringの両方を使用するためにオーバーロードが
 必要なくなったり。

#define BOOST_FILESYSTEM_VERSION 3
#include <boost/filesystem.hpp>

void foo(const boost::filesystem::path& path) {}

int main()
{
    foo("english");
    foo(L"日本語"); // v2ではエラー
}
Polygon
    平面多角形(2D)のアルゴリズムを提供するライブラリ。
    以下は、三角形の内外判定。
#include <boost/polygon/polygon.hpp>
namespace polygon = boost::polygon;

int main()
{
    const std::vector<polygon::point_data<int>> ptrs = {
        {0, 0}, {10, 0}, {10, 10}
    };
    const polygon::polygon_data<int> poly(ptrs.begin(), ptrs.end());

     // 点が三角形の内側にあるか
     const polygon::point_data<int> p(3, 3);
     assert(polygon::contains(poly, p));
}
Meta State Machine(MSM) 1/2
 新たな状態マシンライブラリ。状態遷移表を直接記述する。
namespace msm = boost::msm;
struct Active : msm::front::state<> {};
struct Stopped : msm::front::state<> {};
struct StartStopEvent {};
struct ResetEvent {};

struct StopWatch_ : msm::front::state_machine_def<StopWatch_> {
    typedef Stopped initial_state;
    struct transition_table : boost::mpl::vector<
//           Start    Event           Next
        _row<Active, StartStopEvent, Stopped>,
        _row<Active, ResetEvent,      Stopped>,
        _row<Stopped, StartStopEvent, Active>
    > {};
};

typedef msm::back::state_machine<StopWatch_> StopWatch;
Meta State Machine(MSM) 2/2
    新たな状態マシンライブラリ。状態遷移表を直接記述する。



int main()
{
    StopWatch watch;

     watch.start();
     watch.process_event(StartStopEvent());   //   stop   ->   run
     watch.process_event(StartStopEvent());   //   run    ->   stop
     watch.process_event(StartStopEvent());   //   stop   ->   run
     watch.process_event(ResetEvent());       //   run    ->   stop
}
まとめ(?)


• 1.44.0になってライブラリがかなり充実しまし
  た。

• とくにRange 2.0はプログラミングスタイルを変
  えるほどのライブラリなのでオススメです。

Contenu connexe

Tendances

PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)Satoshi Yamada
 
DTrace for biginners part(2)
DTrace for biginners part(2)DTrace for biginners part(2)
DTrace for biginners part(2)Shoji Haraguchi
 
Rユーザのためのspark入門
Rユーザのためのspark入門Rユーザのためのspark入門
Rユーザのためのspark入門Shintaro Fukushima
 
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014Takashi J OZAKI
 
Pg14_sql_standard_function_body
Pg14_sql_standard_function_bodyPg14_sql_standard_function_body
Pg14_sql_standard_function_bodykasaharatt
 
PostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろうPostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろうkasaharatt
 
20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)Hiromu Shioya
 
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~Miki Shimogai
 
Away3D 4.1 パーティクル入門
Away3D 4.1 パーティクル入門Away3D 4.1 パーティクル入門
Away3D 4.1 パーティクル入門Yasunobu Ikeda
 
20140531 JPUGしくみ+アプリケーション分科会 勉強会資料
20140531 JPUGしくみ+アプリケーション分科会 勉強会資料20140531 JPUGしくみ+アプリケーション分科会 勉強会資料
20140531 JPUGしくみ+アプリケーション分科会 勉強会資料kasaharatt
 
SQLチューニング入門 入門編
SQLチューニング入門 入門編SQLチューニング入門 入門編
SQLチューニング入門 入門編Miki Shimogai
 
Easy caching and logging package using annotation in Python
Easy caching and logging package using annotation in PythonEasy caching and logging package using annotation in Python
Easy caching and logging package using annotation in PythonYasunori Horikoshi
 
Postgre sql9.3 newlockmode_and_etc
Postgre sql9.3 newlockmode_and_etcPostgre sql9.3 newlockmode_and_etc
Postgre sql9.3 newlockmode_and_etckasaharatt
 
Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理Takeshi Arabiki
 

Tendances (20)

PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)
 
DTrace for biginners part(2)
DTrace for biginners part(2)DTrace for biginners part(2)
DTrace for biginners part(2)
 
Rユーザのためのspark入門
Rユーザのためのspark入門Rユーザのためのspark入門
Rユーザのためのspark入門
 
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014
Deep Learningと他の分類器をRで比べてみよう in Japan.R 2014
 
Rの高速化
Rの高速化Rの高速化
Rの高速化
 
Pg14_sql_standard_function_body
Pg14_sql_standard_function_bodyPg14_sql_standard_function_body
Pg14_sql_standard_function_body
 
PostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろうPostgreSQLの関数属性を知ろう
PostgreSQLの関数属性を知ろう
 
20071030
2007103020071030
20071030
 
20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)20090107 Postgre Sqlチューニング(Sql編)
20090107 Postgre Sqlチューニング(Sql編)
 
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
PostgreSQLクエリ実行の基礎知識 ~Explainを読み解こう~
 
Away3D 4.1 パーティクル入門
Away3D 4.1 パーティクル入門Away3D 4.1 パーティクル入門
Away3D 4.1 パーティクル入門
 
20140531 JPUGしくみ+アプリケーション分科会 勉強会資料
20140531 JPUGしくみ+アプリケーション分科会 勉強会資料20140531 JPUGしくみ+アプリケーション分科会 勉強会資料
20140531 JPUGしくみ+アプリケーション分科会 勉強会資料
 
R-hpc-1 TokyoR#11
R-hpc-1 TokyoR#11R-hpc-1 TokyoR#11
R-hpc-1 TokyoR#11
 
SQLチューニング入門 入門編
SQLチューニング入門 入門編SQLチューニング入門 入門編
SQLチューニング入門 入門編
 
Mock and patch
Mock and patchMock and patch
Mock and patch
 
Easy caching and logging package using annotation in Python
Easy caching and logging package using annotation in PythonEasy caching and logging package using annotation in Python
Easy caching and logging package using annotation in Python
 
Postgre sql9.3 newlockmode_and_etc
Postgre sql9.3 newlockmode_and_etcPostgre sql9.3 newlockmode_and_etc
Postgre sql9.3 newlockmode_and_etc
 
Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理
 
Subprocess no susume
Subprocess no susumeSubprocess no susume
Subprocess no susume
 
Rakuten tech conf
Rakuten tech confRakuten tech conf
Rakuten tech conf
 

En vedette

Presentation for business [offical edition]
Presentation for business [offical edition] Presentation for business [offical edition]
Presentation for business [offical edition] Woanchyin Chew
 
Health OER Activities at the University of Michigan
Health OER Activities at the University of MichiganHealth OER Activities at the University of Michigan
Health OER Activities at the University of Michiganstopol
 
Presentation for audiences
Presentation for audiencesPresentation for audiences
Presentation for audiencesCPugh2005
 
Planificacion de la Producción en industrias de alimentos
Planificacion de la Producción en industrias de alimentosPlanificacion de la Producción en industrias de alimentos
Planificacion de la Producción en industrias de alimentosDiana Coello
 
Felt - Front end load testing
Felt - Front end load testingFelt - Front end load testing
Felt - Front end load testingSamuel Vandamme
 
الاسرار الخفيه للوندوز Xp
الاسرار الخفيه للوندوز Xp الاسرار الخفيه للوندوز Xp
الاسرار الخفيه للوندوز Xp maldawly
 
Beams chair gallery
Beams chair galleryBeams chair gallery
Beams chair galleryEAJYDESIGN
 
Colorado’s Skiing Opportunities
Colorado’s Skiing Opportunities Colorado’s Skiing Opportunities
Colorado’s Skiing Opportunities Scott Gelbard
 
Midem Workshop 2010
Midem Workshop 2010Midem Workshop 2010
Midem Workshop 2010kluger
 
Xp祭りに行ってきた
Xp祭りに行ってきたXp祭りに行ってきた
Xp祭りに行ってきたAkira Suenami
 

En vedette (12)

Boost tour 1.60.0 merge
Boost tour 1.60.0 mergeBoost tour 1.60.0 merge
Boost tour 1.60.0 merge
 
Presentation for business [offical edition]
Presentation for business [offical edition] Presentation for business [offical edition]
Presentation for business [offical edition]
 
International Information Architects Seattle Talk
International Information Architects Seattle Talk International Information Architects Seattle Talk
International Information Architects Seattle Talk
 
Health OER Activities at the University of Michigan
Health OER Activities at the University of MichiganHealth OER Activities at the University of Michigan
Health OER Activities at the University of Michigan
 
Presentation for audiences
Presentation for audiencesPresentation for audiences
Presentation for audiences
 
Planificacion de la Producción en industrias de alimentos
Planificacion de la Producción en industrias de alimentosPlanificacion de la Producción en industrias de alimentos
Planificacion de la Producción en industrias de alimentos
 
Felt - Front end load testing
Felt - Front end load testingFelt - Front end load testing
Felt - Front end load testing
 
الاسرار الخفيه للوندوز Xp
الاسرار الخفيه للوندوز Xp الاسرار الخفيه للوندوز Xp
الاسرار الخفيه للوندوز Xp
 
Beams chair gallery
Beams chair galleryBeams chair gallery
Beams chair gallery
 
Colorado’s Skiing Opportunities
Colorado’s Skiing Opportunities Colorado’s Skiing Opportunities
Colorado’s Skiing Opportunities
 
Midem Workshop 2010
Midem Workshop 2010Midem Workshop 2010
Midem Workshop 2010
 
Xp祭りに行ってきた
Xp祭りに行ってきたXp祭りに行ってきた
Xp祭りに行ってきた
 

Similaire à Boost tour 1_44_0

Boost.B-tree introduction
Boost.B-tree introductionBoost.B-tree introduction
Boost.B-tree introductionTakayuki Goto
 
C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)
C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)
C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)Hiro H.
 
ひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指すひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指すAromaBlack
 
ラボユース最終成果報告会(Web公開版)
ラボユース最終成果報告会(Web公開版)ラボユース最終成果報告会(Web公開版)
ラボユース最終成果報告会(Web公開版)Shinichi Awamoto
 
Continuation with Boost.Context
Continuation with Boost.ContextContinuation with Boost.Context
Continuation with Boost.ContextAkira Takahashi
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPAkira Takahashi
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方Satoshi Nagayasu
 
Jetpack datastore入門
Jetpack datastore入門Jetpack datastore入門
Jetpack datastore入門furusin
 
[東京] JapanSharePointGroup 勉強会 #2
[東京] JapanSharePointGroup 勉強会 #2[東京] JapanSharePointGroup 勉強会 #2
[東京] JapanSharePointGroup 勉強会 #2Atsuo Yamasaki
 
Apache Camel Netty component
Apache Camel Netty componentApache Camel Netty component
Apache Camel Netty componentssogabe
 
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作ったdigitalghost
 
ラズパイでデバイスドライバを作ってみた。
ラズパイでデバイスドライバを作ってみた。ラズパイでデバイスドライバを作ってみた。
ラズパイでデバイスドライバを作ってみた。Kazuki Onishi
 
データベース11 - データベースとプログラム
データベース11 - データベースとプログラムデータベース11 - データベースとプログラム
データベース11 - データベースとプログラムKenta Oku
 
5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.com5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.comKenta Tsuji
 

Similaire à Boost tour 1_44_0 (20)

Boost.B-tree introduction
Boost.B-tree introductionBoost.B-tree introduction
Boost.B-tree introduction
 
C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)
C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)
C++のSTLのコンテナ型を概観する @ Ohotech 特盛 #10(2014.8.30)
 
Boost Fusion Library
Boost Fusion LibraryBoost Fusion Library
Boost Fusion Library
 
ひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指すひのきのぼうだけで全クリ目指す
ひのきのぼうだけで全クリ目指す
 
ラボユース最終成果報告会(Web公開版)
ラボユース最終成果報告会(Web公開版)ラボユース最終成果報告会(Web公開版)
ラボユース最終成果報告会(Web公開版)
 
Boost Tour 1.50.0
Boost Tour 1.50.0Boost Tour 1.50.0
Boost Tour 1.50.0
 
Continuation with Boost.Context
Continuation with Boost.ContextContinuation with Boost.Context
Continuation with Boost.Context
 
Replace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JPReplace Output Iterator and Extend Range JP
Replace Output Iterator and Extend Range JP
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方
 
Map
MapMap
Map
 
Jetpack datastore入門
Jetpack datastore入門Jetpack datastore入門
Jetpack datastore入門
 
[東京] JapanSharePointGroup 勉強会 #2
[東京] JapanSharePointGroup 勉強会 #2[東京] JapanSharePointGroup 勉強会 #2
[東京] JapanSharePointGroup 勉強会 #2
 
Junit4
Junit4Junit4
Junit4
 
Apache Camel Netty component
Apache Camel Netty componentApache Camel Netty component
Apache Camel Netty component
 
Boost tour 1_40_0
Boost tour 1_40_0Boost tour 1_40_0
Boost tour 1_40_0
 
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
拡張可能でprintfっぽい書式指定ができて書式指定文字列と引数をコンパイル時に検証できる文字列フォーマット関数を作った
 
01 php7
01   php701   php7
01 php7
 
ラズパイでデバイスドライバを作ってみた。
ラズパイでデバイスドライバを作ってみた。ラズパイでデバイスドライバを作ってみた。
ラズパイでデバイスドライバを作ってみた。
 
データベース11 - データベースとプログラム
データベース11 - データベースとプログラムデータベース11 - データベースとプログラム
データベース11 - データベースとプログラム
 
5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.com5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.com
 

Plus de Akira Takahashi (20)

Cpp20 overview language features
Cpp20 overview language featuresCpp20 overview language features
Cpp20 overview language features
 
Cppmix 02
Cppmix 02Cppmix 02
Cppmix 02
 
Cppmix 01
Cppmix 01Cppmix 01
Cppmix 01
 
Modern C++ Learning
Modern C++ LearningModern C++ Learning
Modern C++ Learning
 
cpprefjp documentation
cpprefjp documentationcpprefjp documentation
cpprefjp documentation
 
C++1z draft
C++1z draftC++1z draft
C++1z draft
 
Boost tour 1_61_0 merge
Boost tour 1_61_0 mergeBoost tour 1_61_0 merge
Boost tour 1_61_0 merge
 
Boost tour 1_61_0
Boost tour 1_61_0Boost tour 1_61_0
Boost tour 1_61_0
 
error handling using expected
error handling using expectederror handling using expected
error handling using expected
 
Boost tour 1.60.0
Boost tour 1.60.0Boost tour 1.60.0
Boost tour 1.60.0
 
Boost container feature
Boost container featureBoost container feature
Boost container feature
 
Boost Tour 1_58_0 merge
Boost Tour 1_58_0 mergeBoost Tour 1_58_0 merge
Boost Tour 1_58_0 merge
 
Boost Tour 1_58_0
Boost Tour 1_58_0Boost Tour 1_58_0
Boost Tour 1_58_0
 
C++14 solve explicit_default_constructor
C++14 solve explicit_default_constructorC++14 solve explicit_default_constructor
C++14 solve explicit_default_constructor
 
C++14 enum hash
C++14 enum hashC++14 enum hash
C++14 enum hash
 
Multi paradigm design
Multi paradigm designMulti paradigm design
Multi paradigm design
 
Start Concurrent
Start ConcurrentStart Concurrent
Start Concurrent
 
Programmer mind
Programmer mindProgrammer mind
Programmer mind
 
Boost.Study 14 Opening
Boost.Study 14 OpeningBoost.Study 14 Opening
Boost.Study 14 Opening
 
Executors and schedulers
Executors and schedulersExecutors and schedulers
Executors and schedulers
 

Boost tour 1_44_0

  • 1. Boostライブラリ一周の旅 ver.1.44.0 高橋晶(Akira Takahashi) ブログ:「Faith and Brave – C++で遊ぼう」 http://d.hatena.ne.jp/faith_and_brave/ Boost.勉強会#2 2010/09/11(Sat)
  • 3. 本日紹介するライブラリ 1. Property Tree 2. Uuid 3. Range 2.0 4. Filesystem v3 5. Polygon 6. Meta State Machine(MSM)
  • 5. Property Tree 2/4 XMLの読込、要素、属性の取得。 XMLパーサーにはRapidXmlを採用している。 <root> <elem attr="World"> Hello using namespace boost::property_tree; </elem> </root> ptree pt; read_xml("test.xml", pt, xml_parser::trim_whitespace); // 要素の取得 const string& elem = pt.get<string>("root.elem"); // 属性の取得 : <xmlattr>という特殊な要素名を介してアクセスする const string& attr = pt.get<string>("root.elem.<xmlattr>.attr");
  • 6. Property Tree 3/4 JSONの読込、データの取得。 { "Data": { "Value": 314, "Str": "Hello" } using namespace boost::property_tree; } ptree pt; read_json("test.json", pt); const int value = pt.get<int>("Data.Value"); const string& str = pt.get<string>("Data.Str");
  • 7. Property Tree 4/4 iniの読込、データの取得。 [Data] Value = 314 Str = Hello using namespace boost::property_tree; ptree pt; read_ini("test.ini", pt); const int value = pt.get<int>("Data.Value"); const string& str = pt.get<string>("Data.Str");
  • 8. Uuid ユニークIDの生成。 COMとか、分散環境での情報の識別とかで 使われることが多い。 using namespace boost::uuids; // 擬似乱数生成器でのUUID生成。デフォルトはmt19937 uuid u1 = random_generator()(); // 文字列からUUID生成 uuid u2 = string_generator()("0123456789abcdef0123456789abcdef"); cout << u1 << endl; cout << u2 << endl; 31951f08-5512-4942-99ce-ae2f19351b82 01234567-89ab-cdef-0123-456789abcdef
  • 9. Range2.0 1/2 ユーティリティ程度だったBoost.Rangeに、 RangeアルゴリズムとRangeアダプタを拡張。 std::vector<int> v; // Rangeアルゴリズム : イテレータの組ではなく範囲を渡す boost::sort(v); boost::for_each(v, f); // Rangeアダプタ using namespace boost::adaptors; boost::for_each(v | filtered(p) | transformed(conv), f); Rangeアルゴリズム : STLアルゴリズムのRange版 Rangeアダプタ : 遅延評価され、合成可能な範囲操作
  • 10. Range 2.0 2/2 Boost.Foreachと組み合わせて使っても便利。 using namespace boost::adaptors; std::map<std::string, int> m; // mapのキーのみを操作 BOOST_FOREACH (const std::string& key, m | map_keys) { // something... } // mapの値のみを操作 BOOST_FOREACH (const int value, m | map_values) { // なにか・・・ } RangeライブラリとしてはOvenも強力なのでそちらもチェックしてください!
  • 11. Filesystem v3 pathの日本語対応等。 stringとwstringの両方を使用するためにオーバーロードが 必要なくなったり。 #define BOOST_FILESYSTEM_VERSION 3 #include <boost/filesystem.hpp> void foo(const boost::filesystem::path& path) {} int main() { foo("english"); foo(L"日本語"); // v2ではエラー }
  • 12. Polygon 平面多角形(2D)のアルゴリズムを提供するライブラリ。 以下は、三角形の内外判定。 #include <boost/polygon/polygon.hpp> namespace polygon = boost::polygon; int main() { const std::vector<polygon::point_data<int>> ptrs = { {0, 0}, {10, 0}, {10, 10} }; const polygon::polygon_data<int> poly(ptrs.begin(), ptrs.end()); // 点が三角形の内側にあるか const polygon::point_data<int> p(3, 3); assert(polygon::contains(poly, p)); }
  • 13. Meta State Machine(MSM) 1/2 新たな状態マシンライブラリ。状態遷移表を直接記述する。 namespace msm = boost::msm; struct Active : msm::front::state<> {}; struct Stopped : msm::front::state<> {}; struct StartStopEvent {}; struct ResetEvent {}; struct StopWatch_ : msm::front::state_machine_def<StopWatch_> { typedef Stopped initial_state; struct transition_table : boost::mpl::vector< // Start Event Next _row<Active, StartStopEvent, Stopped>, _row<Active, ResetEvent, Stopped>, _row<Stopped, StartStopEvent, Active> > {}; }; typedef msm::back::state_machine<StopWatch_> StopWatch;
  • 14. Meta State Machine(MSM) 2/2 新たな状態マシンライブラリ。状態遷移表を直接記述する。 int main() { StopWatch watch; watch.start(); watch.process_event(StartStopEvent()); // stop -> run watch.process_event(StartStopEvent()); // run -> stop watch.process_event(StartStopEvent()); // stop -> run watch.process_event(ResetEvent()); // run -> stop }
  • 15. まとめ(?) • 1.44.0になってライブラリがかなり充実しまし た。 • とくにRange 2.0はプログラミングスタイルを変 えるほどのライブラリなのでオススメです。