いまさらながらのC++でりげーと


あー。だってもう複雑なのはイヤです
引数固定でもいいよね
削除できないけどいいよね

template <class Sender>
struct Delegate
{
	typedef boost::function<void (Sender&)> FunctionType;
	std::vector<FunctionType> functionList_;
	
	Delegate& operator+=(const FunctionType& function)
	{
		functionList_.push_back(function);
		return *this;
	}
	void operator()(Sender& sender) const
	{
		std::for_each(functionList_.begin(),functionList_.end()
			,boost::bind(&FunctionType::operator(),_1,sender));
	}
};

struct Application
{
	...
	Delegate<Application> onInitialized_;
	void Initialize()
	{
		...
		onInitialized_(*this);
	}
	...
};


で、イベント用の関数はこんな感じで登録
メンバ関数もね

void OnInitialized(Application& sender)
{
	MessageBox(0,_T("Initialized"),_T(""),MB_OK);
}

struct AppEvent
{
	void OnInitialized(Application& sender)
	{
		MessageBox(0,_T("AppEvent Initialized"),_T(""),MB_OK);
	}
};

int APIENTRY _tWinMain(...)
{
	Application app;

	app.onInitialized_+=OnInitialized;

	AppEvent appEvent;
	app.onInitialized_+=boost::bind(&AppEvent::OnInitialized,&appEvent,_1);

	...
	app.Initialize(_T("testApp")); //ここで呼ばれる
	...
}


シンプルなのがいいよいいよ
boostすごい