2011-08-01から1ヶ月間の記事一覧

Visual Studio で Python を動かす

Visual Studio 上で Python を動作する事が出来る『Python Tools for Visual Studio』を試してみました。 Visual Studio Express Edition だと動かないらしいので、今回は、VS Shell を使用しました。 VS Shell Python Tools for Visual Studio わたしの環境…

Python のソース内に記述した C++ のソースを渡す

Index.parse に C++ のソースを文字列として渡すことが出来ます。 [main.py] import sys import clang.cindex source = """\ #include <string> int main(){ } """ index = clang.cindex.index.create() tree = index.parse("input.cpp", unsaved_files = [ ("input.</string>…

clang を使用してインクルードファイルの出力

そんなわけで、clang の libclang と cindex.py を使用した簡単な C++ のコード解析です。 とりあえず、インクルードしているヘッダーファイルの出力とか。 [Python ソース] import sys import clang.cindex target = "main.cpp" index = clang.cindex.Index…

glut で、文字列の描画

文字列の描画って普通にできたんですね…知りませんでした。 [コード] void render_string(float x, float y, std::string const& str){ float z = -1.0f; glRasterPos3f(x, y, z); boost::for_each(str, [&](char c){ glutBitmapCharacter(GLUT_BITMAP_HELVE…

clang のちょっと賢いエラーメッセージ

clang のエラーメッセージで面白いものがあったので、載せておきます。 [ソース] template<tyepname T> // typo struct hoge{}; int main(){ doubelw d = 0.0; // typo return 0; } これは double と typename を typo してしまってコンパイルエラーになる例なんですが c</tyepname>…

Boost.Phoenix で、Match 文つくった

欲しいのでつくりました。 まぁ定数以外も使える switch-case 文ですね。 構文はこんな感じ。 match_(expression)[ case_(value)[ statement ], case_(value)[ statement ], ... default_[ statement ] ] 上から順に評価されて、expression == value の場合…

Boost.MPL に対応させた Variadic Templates のラッパを書いた

以前、Variadic Templates のパラメータを mpl::vector に変換する処理を書いたんですが、今回は、直接 Boost.MPL に拡張する形で書いてみました。 満たしているコンセプトは、 Forward Sequence Front Extensible Sequence Back Extensible Sequence Forwar…

Boost.Phoenix で、ユーザ側で定義した関数を呼び出すためのアダプタ

boost::phoenix::function を使用して定義することも出来ますが、Boost.Phoenix に用意されているアダプタを使用すればもっと簡単に定義することが出来ます。 BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY 引数のない関数をアダプトします。 namespace demo{ int n…

Boost.Phoenix で、std::map の remove_if を書いてみた

Boost.Range を思いつく前に書いたボツネタ。 iterator を回して直接、値を削除しています。 Boost.Phoenix に first と second が欲しいですね。 [ソース] #include <boost/phoenix.hpp> #include <boost/typeof/typeof.hpp> #include <map> #include <string> #include <iostream> template<typename T> typename T::first_type first_impl(</typename></iostream></string></map></boost/typeof/typeof.hpp></boost/phoenix.hpp>…

std::map で remove_if をする

いろいろと考えていたんですが Boost.Range の filter でそれっぽく処理することが出来ました。 [ソース] #include <boost/range/algorithm/for_each.hpp> #include <boost/range/adaptor/filtered.hpp> #include <boost/typeof/typeof.hpp> #include <boost/bind.hpp> #include <map> #include <string> #include <iostream> bool over_three(std::pair</iostream></string></map></boost/bind.hpp></boost/typeof/typeof.hpp></boost/range/adaptor/filtered.hpp></boost/range/algorithm/for_each.hpp>

Vim の開発環境

現状の Vim の開発環境に関してのまとめなど。 他の Vimmer の開発環境がを見てみたかったので自分から晒してみるなど。 [主な用途] テキストエディタ プログラミングのコーディング、コンパイル、実行(主に C++) [主力プラグイン5つ] unite.vim バッファや…

neocomplcache-clang

結構前なんですが、neocomplcache に clang_complete が取り込まれた『neocomplcache-clang』が作られたので、その紹介です。 neocomplcache-clang では、clang_complete と neocomplcache が競合してしまう問題が解消されており、neocomplcache 上で、clang…

Boost.Phoenix で、const でない関数オブジェクトを bind する

そのまま渡したらエラーになりました。 phx::ref でラップする必要があるみたいです。 [ソース] #include <boost/phoenix.hpp> #include <iostream> struct disp{ disp() : count(1){} void operator ()(){ for(int i = 0 ; i < count ; ++i){ std::cout << "homu"; } std::cout << "\n"; </iostream></boost/phoenix.hpp>…

Boost.Phoenix で、FizzBuzz

いくつか手法はありますが、まぁ何も考えずにガリガリ書くとこんな感じです。 [ソース] #include <boost/phoenix.hpp> #include <iostream> #include <boost/lexical_cast.hpp> int main(){ namespace phx = boost::phoenix; using phx::arg_names::arg1; using phx::local_names::_i; phx::let(_i = phx::val(1) )</boost/lexical_cast.hpp></iostream></boost/phoenix.hpp>…

Boost.Phoenix で、Boost.Lexical_cast を使う

もうちょっと複雑なコードになるかと思いましたが、function を使えばそんなに難しくありませんでした。 関数の戻り値型をどうするかちょっと悩みましたが、元々関数オブジェクトなので、boost::result_of で取得出来るんですね。 [ソース] #include <boost/phoenix.hpp> #inclu</boost/phoenix.hpp>…

int 型から Boost.MPL の Sequence に変換する

mpl::int_<4241> → mpl::vector_c<int, 4, 2, 4, 1>みたいな感じですね。 [ソース] #include <boost/mpl/vector.hpp> #include <boost/mpl/push_front.hpp> #include <boost/mpl/int.hpp> #include <boost/mpl/divides.hpp> #include <boost/mpl/modulus.hpp> #include <boost/mpl/less_equal.hpp> namespace mpl = boost::mpl; template…</boost/mpl/less_equal.hpp></boost/mpl/modulus.hpp></boost/mpl/divides.hpp></boost/mpl/int.hpp></boost/mpl/push_front.hpp></boost/mpl/vector.hpp></int,>

テンプレートテンプレートパラメータに Boost.MPL のラムダ式を渡す

以下のようにテンプレートテンプレートパラメータを受け取るテンプレートクラスがあるとします。 template< typename T, typename U, template<typename, typename> class Pred = boost::is_same > struct equal{ typedef Pred<T, U> type; }; // OK BOOST_MPL_ASSERT(( equal<int, int> )); // O</int,></t,></typename,>…

static_cast を使用して、オーバーロードされている関数の取得

static_cast を使用して取得出来たんですね……。 [ソース] #include <iostream> void func(int a){ std::cout << a << std::endl; } void func(float a){ std::cout << a << std::endl; } void func(float a, int b){ std::cout << a << ":" << b << std::endl; } int m</iostream>…

Boost.Variant の apply_visitor に2つ以上渡す

こんな使い方も出来たんですね、知りませんでした。 [ソース] #include <boost/variant.hpp> #include <boost/lexical_cast.hpp> #include <string> typedef boost::variant<int, float, std::string> object_type; struct equal : boost::static_visitor<bool>{ bool operator ()(float a, int b) const{ return a == static_cast<float>(b); } bool o…</float></bool></int,></string></boost/lexical_cast.hpp></boost/variant.hpp>

Boost.Phoenix lambda のスコープ内で、スコープ外の変数を参照する

let と同じで、変数に引数を代入して使用します。 [ソース] #include <boost/phoenix.hpp> #include <iostream> int main(){ namespace phx = boost::phoenix; using phx::arg_names::arg1; using phx::arg_names::arg2; using phx::local_names::_a; int array[] = {0, 1, 2, 3, 4}; // </iostream></boost/phoenix.hpp>…

Boost.Signals2 を Boost.Parameter を使用して定義する

Boost.Signals2 の signal は、以下の様に複数のテンプレート引数が存在します。 template<typename Signature, // Function type R (T1, T2, ..., TN) typename Combiner = boost::signals2::optional_last_value<R>, typename Group = int, typename GroupCompare = std::less<Group>, typename SlotFunction = boost::function<Signature>, typenam…</signature></group></typename>

Boost.Signals2 で呼ばれる関数に優先順位を付ける

signal::connect の第一引数に優先順位(デフォルトは int 型)を渡すことで呼び出し順を設定することが出来ます。 [ソース] #include <boost/signals2/signal.hpp> #include <boost/lambda/lambda.hpp> #include <iostream> int main(){ namespace lambda = boost::lambda; boost::signals2::signal<void()> sig; sig.connect(1, std:</void()></iostream></boost/lambda/lambda.hpp></boost/signals2/signal.hpp>…

vim script で、list をラップ

STL っぽく簡単に list をラップしてみた。 まぁこれぐらいなら素直に組み込み関数を使ったほうがいいのかな。 こういうのは需要があるのだろうか。 [ソース] function! s:class_array(...) " 初期化 let self = {} " プロパティ let self.data = a:000 " メ…

vim script で、クラスっぽい定義

まぁこの手のネタは、あちこちで語り尽くされているとは思うんですが。 特に深くつっこむつもりはなかったんですが、書き始めたら止まらなくなっちまった。 vim script にはクラスがないんですが、辞書に関数が定義できるので、クラスオブジェクトっぽく記述…

Boost.Phoenix Scope-lambda

Boost.Phoenix の lambda は、式の中に式を書く時に使用します。 使い方は以下のとおり。 [ソース] #include <boost/phoenix.hpp> #include <boost/range/begin.hpp> #include <boost/range/end.hpp> #include <algorithm> #include <iostream> #include <vector> struct for_each_impl{ typedef void result_type; template<typename C, typename F> void op…</typename></vector></iostream></algorithm></boost/range/end.hpp></boost/range/begin.hpp></boost/phoenix.hpp>

Boost.Phoenix で再帰処理 その2

アンドキュメントですが、Boost.Phoenix に this_ が用意されていて、こんな事が出来るみたいです。 [ソース] #include <boost/phoenix.hpp> #include <boost/phoenix/scope/this.hpp> #include <iostream> int main(){ namespace phx = boost::phoenix; using phx::arg_names::arg1; phx::if_(arg1 <= 0)[ std::cout << </iostream></boost/phoenix/scope/this.hpp></boost/phoenix.hpp>…

boost-geometry-render

Boost.Geometry で遊ぶならやっぱり GUI でテストしたいよね! って、ことで、glut を使用して、簡単な Boost.Geometry の描画用ライブラリをつくりました。 基本的に Boost.Geometry で描画するだけの事を考えているので、その他の事は特に何もしていません…

Boost.Phoenix で、__stdcall 付き関数の評価

Boost.Phoenix の bind は、__stdcall 等が付いた関数に対応していないので、Boost.Bind でラップする必要があります。 [ソース] #define BOOST_BIND_ENABLE_STDCALL #include <boost/phoenix.hpp> #include <boost/bind.hpp> #include <iostream> void __stdcall disp(int n){ std::cout << n << std::end</iostream></boost/bind.hpp></boost/phoenix.hpp>…

Boost.Phoenix で、2つ以上の式を呼び出す

Boost.Phoenix では、phoenix::bind と カンマ演算子を使用して、2つ以上の式を呼び出すことが出来ます。 [ソース] #include <boost/phoenix.hpp> #include <boost/range/irange.hpp> #include <boost/range/algorithm/for_each.hpp> #include <iostream> void disp(int n){ std::cout << n << std::endl; } int main(){ namespace phx = boost::phoenix</iostream></boost/range/algorithm/for_each.hpp></boost/range/irange.hpp></boost/phoenix.hpp>…

Boost.Phoenix で再帰処理

Boost.Phoenix で再帰です。 書き方は、Boost.Lambda でのやり方と殆ど同じですね。 自分を呼び出すために予め自分を用意しておく必要があります。 [ソース] #include <boost/phoenix/core.hpp> #include <boost/phoenix/operator.hpp> #include <boost/phoenix/bind.hpp> #include <boost/function.hpp> #include <iostream> int main(){ namespace phx = boost::phoenix</iostream></boost/function.hpp></boost/phoenix/bind.hpp></boost/phoenix/operator.hpp></boost/phoenix/core.hpp>…