「トップページ/CPP」の編集履歴(バックアップ)一覧はこちら
トップページ/CPP - (2007/11/19 (月) 16:05:58) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
|&big(){C++いろいろ}|
#contents()
*開発環境
GCC
Boost
----
*16進文字列を数値にする
string str = "0xcdef";
int a = 0;
try {
a = boost::lexical_cast<int>(str);
}
catch(boost::bad_lexical_cast&) {
;
}
----
*構造体メンバの先頭からのオフセットを得る
#include <stddef.h>
size_t offsetof( type, member);
----
*コールバックの実装(Boost)
Runメソッドを呼ぶと、testEventに登録された関数が呼ばれる。
testEventメンバをpublicにせず、SetTestEventメソッド経由にする理由は、外部からイベントハンドラを呼ぶことが出来てしまうからである。SetTestEventメソッドの引数にNULLを渡すと、イベントハンドラをクリアする事が出来る。
// 呼び出し元
class aa {
// イベントハンドラ(イベント発生時に呼び出す関数のアドレス)
protected: boost::function<string (int a,int b)> testEvent;
// 内部からイベントハンドラを呼び出す為のラッパー
protected: virtual string OnTestEvent(int a, int b) {
string retstr;
if (testEvent!=NULL)
retstr=testEvent(a,b);
return retstr;
}
// イベントハンドラを登録する
public: void SetTestEvent(boost::function<string (int a,int b)> func) {
testEvent = func;
}
public: void Run(void) {
cout << "[" << OnTestEvent(5,8) << "]" << endl;
}
}
// 呼び出し先
class bb {
protected: string TestEventFunc(int a, int b) {
return boost::io::str(boost::format("a+b=%d") % a+b);
}
}
//
int main(int, char**) {
aa AA;
bb BB;
AA.SetTestEvent( boost::bind(&bb::TestEventFunc, &BB, _1, _2);
AA.Run();
return 0;
}
|&big(){C++いろいろ}|
#contents()
*開発環境
GCC
Boost
----
*整数の絶対値
inline Int32 fastabs(Int32 a) {
Int32 m = a >> 31;
return (a ^ m) - m;
}
*2整数のmax, min
inline Int32 fastmax(Int32 a, Int32 b) {
Int32 t = (a-b);
return a - (t & (t >> 31));
}
inline Int32 fastmin(Int32 a, Int32 b) {
Int32 t = (a-b);
return b + (t & (t >> 31));
}
----
*16進文字列を数値にする
string str = "0xcdef";
int a = 0;
try {
a = boost::lexical_cast<int>(str);
}
catch(boost::bad_lexical_cast&) {
;
}
----
*構造体メンバの先頭からのオフセットを得る
#include <stddef.h>
size_t offsetof( type, member);
----
*コールバックの実装(Boost)
Runメソッドを呼ぶと、testEventに登録された関数が呼ばれる。
testEventメンバをpublicにせず、SetTestEventメソッド経由にする理由は、外部からイベントハンドラを呼ぶことが出来てしまうからである。SetTestEventメソッドの引数にNULLを渡すと、イベントハンドラをクリアする事が出来る。
// 呼び出し元
class aa {
// イベントハンドラ(イベント発生時に呼び出す関数のアドレス)
protected: boost::function<string (int a,int b)> testEvent;
// 内部からイベントハンドラを呼び出す為のラッパー
protected: virtual string OnTestEvent(int a, int b) {
string retstr;
if (testEvent!=NULL)
retstr=testEvent(a,b);
return retstr;
}
// イベントハンドラを登録する
public: void SetTestEvent(boost::function<string (int a,int b)> func) {
testEvent = func;
}
public: void Run(void) {
cout << "[" << OnTestEvent(5,8) << "]" << endl;
}
}
// 呼び出し先
class bb {
protected: string TestEventFunc(int a, int b) {
return boost::io::str(boost::format("a+b=%d") % a+b);
}
}
//
int main(int, char**) {
aa AA;
bb BB;
AA.SetTestEvent( boost::bind(&bb::TestEventFunc, &BB, _1, _2);
AA.Run();
return 0;
}