■boost 1.64.0 | Top |
boostはC++で使用できるオープンソースライブラリのこと。
Visual Studioでboostを使用する
Visual Studio 2017 Community での環境構築の手順を示す。 1.boostライブラリをダウンロードする。
boostの公式サイト
Downloadページへのリンクがあるのでそこからライブラリ一式をダウンロードする。2.解凍後、任意のフォルダに置く。ここでは下記においた。
D:\Program Files\boost\boost_1_64_03.環境変数に下記を追加する。
変数名:BOOST_ROOT
変数値:D:\Program Files\boost\boost_1_64_04.Visual Studio で 「Win32 コンソール アプリケーション」で新規作成する。
5.「ソリューション エクスプローラー」上で右クリックし、「C/C++」の「全般」の「追加のインクルード ディレクトリ」に下記を追加する。
$(BOOST_ROOT);
PropertyTree
プロパティツリーは、任意のネストされたツリー状のデータ構造を解析するライブラリである。
XMLだけではなく、INIやJSONも解析できるらしい。Microsoft純正のXmlLiteと異なりXPathで解析できるため使いやすい。と思ったが、コーディングしてみると結構組みづらい。
#include "stdafx.h" #include <iostream> #include <string> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> using namespace std; using namespace boost::property_tree; // Ctrl + F5 でデバッグするとコンソールが表示されたままとなり標準出力を確認できる int main() { // 空の property tree object を作成 ptree pt; // XMLファイルを読み込みproperty treeにセット read_xml("BackGround.dae", pt); // *************************************************************************** // textデータ取得 // *************************************************************************** // get_optionalメソッドを使用するとエラーの有無によって分岐できるようになる // なおautoは型推論( C++ 11 ) if (auto str = pt.get_optional<std::string>("COLLADA.asset.contributor.author")){ std::cout << str.get() << std::endl; } else { std::cout << "author is nothing" << std::endl; } // *************************************************************************** // 属性値取得 // *************************************************************************** if (auto str = pt.get_optional<std::string>("COLLADA.library_geometries.geometry.mesh.vertices.input.<xmlattr>.semantic")) { std::cout << str.get() << std::endl; } else { std::cout << "semantic is nothing" << std::endl; } // *************************************************************************** // foreach( C++ 11 )を使用するとヒットしたすべての要素を取得 // *************************************************************************** // get_childメソッドを使用すると子要素を取得できる for (auto e : pt.get_child("COLLADA.library_geometries.geometry.mesh.triangles")) { // タグ名で絞込み if (e.first == "input"){ // 属性名で絞込み if (auto str = e.second.get_optional<std::string>("<xmlattr>.semantic")) { cout << str.get() << endl; } else { cout << "semantic is nothing" << endl; } } } // *************************************************************************** // 数値型にキャスト // *************************************************************************** if (auto str = pt.get_optional<int>("COLLADA.library_geometries.geometry.mesh.triangles.<xmlattr>.count")) { std::cout << str.get() * 10 << std::endl; } else { std::cout << "count is nothing" << std::endl; } return 0; }
boost:PropertyTree