ぼく用あれこれまとめ

boostめも

最終更新:

Bot(ページ名リンク)

- view
管理者のみ編集可
#include <iostream>
#include <tchar.h>
#include <Windows.h>

#include <fstream>

#include <boost\shared_ptr.hpp>

//
// メッセージインターフェイス
//
class IMsg
{
public:
	virtual	~IMsg()				= 0
	{
		;
	}
	virtual unsigned int GetMsgType() const	= 0;
};

//
// 文字列付きのメッセージ
//
class MsgString : public IMsg
{
public:
	[[explicit]] MsgString(unsigned int msgName, const TCHAR * str)
		: m_ident(msgName), m_string(str)
	{
		;
	}

	~MsgString()
	{
		;
	}
	unsigned int	GetMsgType() const { return m_ident; }
	const TCHAR	*	GetString() const { return m_string; }

private:
	unsigned int const	m_ident;
	const TCHAR *		m_string;
};

typedef boost::shared_ptr<IMsg> IMsgPtr;
typedef boost::shared_ptr<MsgString> MsgStrPtr;

enum
{
	WRITE_A_LOG_FILE
};


int main()
{
	std::ofstream ofs(_T("test.txt"), std::ios::app);
	static const int buffer_size = 48;

	TCHAR buffer[buffer_size];
	_stprintf_s( buffer, buffer_size, _T("Eureka Seven : %d"), 7);

	//
	// MsgStr1
	//
	MsgStrPtr msgStr1 = MsgStrPtr( new MsgString(WRITE_A_LOG_FILE, buffer ));
	std::cout << msgStr1->GetString() << std::endl;
	ofs << msgStr1->GetString() << std::endl;

	//
	// MsgStrPtr -> IMsgPtr (up-cast)
	//
	IMsgPtr msg = msgStr1;

	//
	// IMsgPtr -> MsgStrPtr2 (down-cast)
	//	
	MsgStrPtr msgStr2 = boost::dynamic_pointer_cast<MsgString>( msg );

	std::cout << msgStr2->GetString() << std::endl;
	ofs << msgStr2->GetString() << std::endl;

	//std::cout << buffer << std::endl;
	while(1){};
	return 0;
}
記事メニュー
ウィキ募集バナー