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

quickrun の outputter を自分好みに拡張してみた

vim

quickrun には、すでに便利な outputter がいくつが用意されているんですが、細かく設定したい場合、既存の outputter を拡張した方が楽かなー、と思い書いてみました。 既存のものを拡張するだけなので割と簡単ですね。 [オレオレ outputter] " :QuickRun -…

Template Aliases でハマった

Template Aliases でハマったので覚え書き。 まーしょっぱいエラーですね。 次のコードはエラーになります。 [ソース] #include <boost/mpl/has_xxx.hpp> #include <boost/mpl/identity.hpp> #include <boost/mpl/if.hpp> namespace mpl = boost::mpl; BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type); template<typename T> using T_value_type =</typename></boost/mpl/if.hpp></boost/mpl/identity.hpp></boost/mpl/has_xxx.hpp>…

C++0x で関数ポインタ型を定義

alias declaration(Template Aliases) が実装されていれば using を使用して、わかりやすく関数ポインタ型を定義できるので試してみたました。 [ソース] #include <boost/mpl/assert.hpp> #include <boost/type_traits/is_same.hpp> #include <iostream> using func_type = int(*)(int, int); // typedef int(*func_type)(int</iostream></boost/type_traits/is_same.hpp></boost/mpl/assert.hpp>…

C++ and C++'0x Support in Clang

が更新されたらしいので、『template aliases』と『In-declaration member initialization』を試してみました。 C++ and C++'0x Support in Clang 他にも更新されたのがあるかも。 [ソース] #include <iostream> #include <boost/array.hpp> template<typename T, typename U> struct is_same{ static bool const</typename></boost/array.hpp></iostream>…

Boost.Ratio の定義を float っぽく記述する

float というか、実数ですね。 あくまでも、それっぽく記述するだけなので、中身は Boost.Ratio です。 まぁ書きやすくはなるんじゃないでしょうか。 [ソース] #include <boost/mpl/assert.hpp> #include <boost/mpl/equal_to.hpp> #include <boost/mpl/vector.hpp> #include <boost/mpl/for_each.hpp> #include <boost/mpl/transform.hpp> #include <boost/ratio/mpl/times.hpp> #include …</boost/ratio/mpl/times.hpp></boost/mpl/transform.hpp></boost/mpl/for_each.hpp></boost/mpl/vector.hpp></boost/mpl/equal_to.hpp></boost/mpl/assert.hpp>

C++ で、Ruby っぽいべき乗計算

C++

Ruby では以下のコードで、べき乗の計算を行うことが出来ます。 puts 2 ** 3 # 8 と、いうことで、C++ でも書いてみました。 [ソース] #include <cmath> #include <iostream> namespace detail{ template<typename T> struct pow_impl{ pow_impl(T const& t) : exponent_(t){} struct expo</typename></iostream></cmath>…

autoload に定義されている関数を呼びやすくする

autoload に以下のような定義を書くことで、ユーザコードから定義されている関数を呼び出す事が出来ます。 [./autoload/accel/functional.vim] " accel#functional は、ディレクトリ構成と同じにする必要がある function! accel#functional#plus(a, b) retur…

VimScript その後

最終的にこんな感じになりました。 本当に誰得……。 [使い方] " 実装部は下記参照 function! s:minus(a, b) return a:a - a:b endfunction " s:apply で関数を評価する echo s:apply("s:minus", 5, 2) " 3 let joint = {} function! joint.apply(a, b) return…

C++ で、Model-View-Controller とか

C++

結構適当。 MVC 自体うろ覚えなので、ところどころ間違っているかも。 あんまり Pluggable じゃない気もする。 [ソース] #include <iostream> #include <boost/signals2.hpp> template<typename T> struct hp_view1{ void operator()(T const& model){ view(model); } void view(T const& model){ std::</typename></boost/signals2.hpp></iostream>…

VimScript で、bind とか

部分適用的な処理とか。 すごく… C++ ライクです……。 [ソース] function! s:placeholders(args_no) let func = {"args_no" : a:args_no} function! func.apply(...) dict return a:000[self.args_no] endfunction return func endfunction let _1 = s:placeh…

要素が違う list を for 文で回す

help に普通に載っていました。 for 文の行末に unlet すればいいみたいです。 [ソース] function! s:print(list) for var in a:list echo var unlet var endfor endfunction call s:print(["hoge", 1, 3.14, [1, 2, 3]]) [出力] hoge 1 3.14 [1, 2, 3] [参…

unite.vim でシステムで関連付けされたファイルオープンをする

vim

と、いう電波を受信したので書いてみました。 Windows だと unite-action に start を追加するとかそんな感じですね。 多分これで動くと思います。 [.vimrc] " unite.vim に action を追加する " unite-action start let start = { \ 'description' : 'start…

可変長引数を可変長関数へ渡す

call 使えば簡単に行えました。 a:000 で、可変長引数のリストが取得できるのがイイネ。 [ソース] function! s:apply(func, ...) return call(a:func, a:000) endfunction function! s:print(...) for var in a:000 echo var endfor endfunction function! s…

Vim Script の関数への参照とか

実はそんなに知らなかったので、個人的なまとめ。 call なんてあったのか。 詳細は、:help Funcref 辺りで。 [ソース] function! s:plus(a, b) return a:a + a:b endfunction echo s:plus(3, 1) let Plus_func = function("s:plus") echo Plus_func(2, 1) ec…

Vim Script で、apply と lambda

もはや誰得なのかが分からない。 関数型言語はあまり分からないんですが、こんな感じなんでしょうかねぇ。 個人的にやりたい事は出来た気はする。 [ソース] " --------------------------------------------------------- function! s:has_apply(dict) try r…

Vim Script で、fold 書いてみた

実装自体はそんなに難しくない。 需要があるかは分からない。 [ソース] function! s:fold(list, state, forwardOp) let l:op = a:state for value in a:list let l:op = a:forwardOp(l:op, value) endfor return l:op endfunction function! s:plus(a, b) re…

Vim Script で、fold の続き

とりあえず、ここまでがんばった。 ついでに fold の引数順も変更。 特に意味はないけど。 [ソース] " 各演算子を辞書型で定義 function! s:operator(op) let func = {"op" : a:op} function! func.call(a, b) dict return eval(a:a . self.op . a:b) endfun…

Boost.Spirit.Qi で文字列のパース

{文字列}={値} の様に文字列が含まれている構文をパースする場合、 +qi::char_ >> '=' >> qi::int_ では、パースに失敗します。 +qi::char_ が、'=' にマッチしないよう設定する必要があるので、 +(qi::char_ - '=') >> '=' >> qi::int_ これでパースに成功…

Boost.Spirit.Qi の Parser を保持

[ソース] #include <boost/spirit/include/qi.hpp> #include <boost/fusion/include/vector.hpp> #include <boost/fusion/include/io.hpp> namespace qi = boost::spirit::qi; namespace fusion = boost::fusion; template <typename P> void test_parser(char const* input, P const& p, bool full_match = true){ using boos…</typename></boost/fusion/include/io.hpp></boost/fusion/include/vector.hpp></boost/spirit/include/qi.hpp>

Vim で開いているウィンドウをスキップしながらバッファを切り替える

バッファを切り替える際に、すでにウィンドウが開いているならば、スキップして切り替えるとかそんな感じ。 複数ウィンドウを開いている状態で、:bnext で切り替えると、すでに開いているウィンドウも切り替えるのでちょっと無駄かなーと。 Quickfix とか、…

Boost.Spirit.Qi の test_parser

Qi の Reference に書かれている test_parser の実装はこれかな? [ソース] #include <boost/spirit/include/qi.hpp> #include <iostream> template <typename P> void test_parser(char const* input, P const& p, bool full_match = true){ using boost::spirit::qi::parse; char const* f(input); char const*</typename></iostream></boost/spirit/include/qi.hpp>…

Boost.Spirit.Qi はじめました

はじめてみました。 [ソース] #include <iostream> #include <vector> #include <string> #include <boost/spirit/include/qi.hpp> #include <boost/range/algorithm/for_each.hpp> #include <boost/fusion/include/adapt_struct.hpp> #include <boost/fusion/include/io.hpp> namespace qi = boost::spirit::qi; namespace fusion = bo…</boost/fusion/include/io.hpp></boost/fusion/include/adapt_struct.hpp></boost/range/algorithm/for_each.hpp></boost/spirit/include/qi.hpp></string></vector></iostream>

Boost 1.47.0 がリリース

Version 1.47.0 リリースノート

Boost.Phoenix と Boost.Spirit.Qi の併用

次のコードはコンパイルエラーになります。 [エラーコード] #include <boost/phoenix.hpp> #include <boost/spirit/include/qi.hpp> int main(){ return 0; } を使用する場合、 の include 前に #define BOOST_SPIRIT_USE_PHOENIX_V3 1 を定義しておく必要があるみたいです。 [修正] #include <boost/phoenix.hpp> // <boost/spirit/include/qi.hpp> を include</boost/spirit/include/qi.hpp></boost/phoenix.hpp></boost/spirit/include/qi.hpp></boost/phoenix.hpp>…

TwitVim が複数のアカウントに対応

vim

TwitVim 0.7.0 からアカウントの切り替えが出来るようになりました。 ブラウザ上で任意のアカウントにログインした状態で :SetLoginTwitter で複数のアカウントを設定することが出来ます。 :SwitchLoginTwitter で、使用するアカウントを切り替える事が出来…

BOOST_PP_ITERATE で遊んでみた

巷では、空前絶後の Boost.PP ブームらしいですね。 え、知らない? STL のコンテナを初期化する initial_values の可変長引数を実装してみました。 [main.cpp] #include <boost/range/algorithm/for_each.hpp> #include <boost/lambda/lambda.hpp> #include <iostream> #include <vector> #include <string> #include "initial_values.hpp" int main(</string></vector></iostream></boost/lambda/lambda.hpp></boost/range/algorithm/for_each.hpp>…

BOOST_PP で、範囲の初期化

ふと、思いついたので書いてみた。 mpl::range_c や、boost::irange とかそんな感じ。 こういうのは、プリプロセッサが楽ですね。 まぁ特にプリプロセッサで書く意味もない気がしますが。 [ソース] #include <boost/preprocessor/repeat_from_to.hpp> #define INITIALIZER_RANGE_DECL(z, n, text) n,</boost/preprocessor/repeat_from_to.hpp>…

Boost.Raito で、コンパイル時に Sqrt 計算

とかやってました。 最初に言い訳しておくと、桁の切り捨てをものすごく適当にしているので精度はそんなによくないです。 もうちょっといいやり方があると思うけども…ぐぬぬ……。 あとコンパイラによると思いますが、コンパイルが、Booooost ってレベルじゃね…

Boost.Ratio + Boost.MPL で遊んでみた

Boost 1.47.0 が待ちきれなかったので、適当にここら辺から落としてきて遊んでみました。 まぁ、Boost.Ratio + Boost.MPL を試しただけなのであんまり面白いものではありませんが…。 [ソース] #define BOOST_RATIO_EXTENSIONS #include <boost/mpl/assert.hpp> #include <boost/mpl/int.hpp> #include <boost/mpl/multiplies.hpp></boost/mpl/multiplies.hpp></boost/mpl/int.hpp></boost/mpl/assert.hpp>…