⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msghndlr.cpp

📁 设计模式之singleton模式例程
💻 CPP
字号:
// MsgHndlr.cpp - Implementation of CMsgHndlr class

// This class is responsible for, 
// 1. Creating the required message handler
// 2. Making sure that only one message handler ( Singleton Object ) exists at any given time
// 3. Notifying the window attached when HandleMessage method is called

// NOTE : Two GetMsgHndlr methods are provided.  They return the requested message handler 
// objects to the client.  First one takes RuntimeClass information, to
// create the message handler object and the other takes the registered Key
// and creates the message handler object.

// Written by : T. Kulathu Sarma

// This file is part of Singleton application to demonstrate the concept of Singleton classes.
// This file is provided "as is" with no expressed or implied warranty.

#include "stdafx.h"
#include "MsgHndlr.h"
#include "MsgHndlrReg.h"

// Register CMsgHndlr agent in the message handler registry
static CMsgHndlrRegistry< CMsgHndlr > gMsgHndlr( RUNTIME_CLASS( CMsgHndlr ), "MSGHNDLR" );

IMPLEMENT_DYNCREATE( CMsgHndlr, CObject )

// Initialize static data member of the class

CMsgHndlr		* CMsgHndlr::m_pMsgHndlr = NULL;
CSmartCleaner	CMsgHndlr::m_SmartCleaner;

// CMsgHndlr - Implementation

CMsgHndlr::CMsgHndlr()
{
	// Step 1 - Set Notify window pointer to NULL
	m_pNotifyWnd	= NULL;
	// Step 2 - Set this pointer to smart cleaner
	m_SmartCleaner.SetObject( this );
}

CMsgHndlr::~CMsgHndlr()
{
	// Step 1 - Set m_pMsgHndlr to NULL when the object is deleted
	CMsgHndlr::m_pMsgHndlr = NULL;
	// Step 2 - Set NULL pointer to smart cleaner
	m_SmartCleaner.SetObject( NULL );
}

INT CMsgHndlr::SetNotifyWindow( CWnd * pNotifyWnd )
{
	// Step 1 - Set the member variable m_pNotifyWnd to the parameter passed
	m_pNotifyWnd = pNotifyWnd;
	return SUCCESS;
}

CWnd * CMsgHndlr::GetNotifyWindow()
{
	// Step 1 - Return the member variable m_pNotifyWnd
	return m_pNotifyWnd;
}

// NOTE :
// 1. When a new message handler with a different key is required, caller should delete
// the exisiting message handler ( if it has one ), before calling this method. 

CMsgHndlr * CMsgHndlr::GetMsgHndlr( LPCSTR lpszKey )
{
	CRuntimeClass * pRuntimeClass = NULL;

	// Step 1 - Get runtime class from the object registry, for the given key ( if provided )
	if( lpszKey != NULL && lpszKey[ 0 ] != '\0' )
	{
		// Step 2 - Return NULL, in case of wrong key
		if( CObjRegistry::GetRuntimeClass( lpszKey, pRuntimeClass ) == FALSE )
		{
			return NULL;
		}
	}
	// Step 3 - Return the message handler to the caller using GetMsgHndlr( CRuntimeClass * )
	return GetMsgHndlr( pRuntimeClass );
}

// NOTE :
// 1. This method returns the message handler object, if one is already created and
// belongs to the right runtime class.
// 2. When creating a new object, a check, is made to ensure that requested class "is a"
// message handler class, before the object is created.
// 3. When a new message handler of different runtime class is required, caller should delete
// the exisiting message handler ( if it has one ), before calling this method. 

CMsgHndlr * CMsgHndlr::GetMsgHndlr( CRuntimeClass * pRuntimeClass )
{
	// Step 1 - Return message handler object, if it is already created 
	if( m_pMsgHndlr != NULL )
	{
		return m_pMsgHndlr;
	}

	// Step 2 - Get the runtime class using which message handler object
	// has to be created
	CRuntimeClass * pMsgHndlrClass = pRuntimeClass;

	if( pMsgHndlrClass == NULL )
	{
		pMsgHndlrClass = RUNTIME_CLASS( CMsgHndlr );
	}
	else
	{
		// Step 3 - Check the class type before creating an object
		CRuntimeClass * pBaseClass = RUNTIME_CLASS( CMsgHndlr );
		
		if( pMsgHndlrClass->IsDerivedFrom( pBaseClass ) == FALSE )
		{
			return NULL;
		}
	}

	// Step 4 - Create Object using the Run time class information
	m_pMsgHndlr = ( CMsgHndlr * ) pMsgHndlrClass->CreateObject();

	// Step 5 - Return pointer to the object to the calling code
	return m_pMsgHndlr;
}

INT CMsgHndlr::GetRegCount()
{
	// Step 1 - Return the registered count from object registry
	return CObjRegistry::GetCount();
}

BOOL CMsgHndlr::IsHndlrRegistered( LPCSTR lpszKey )
{
	// Step 1 - Declare a dummy run time class
	CRuntimeClass * pRuntimeClass = NULL;

	// Step 2 - Return the value returned by GetRuntimeClass of Object Registry
	return CObjRegistry::GetRuntimeClass( lpszKey, pRuntimeClass );
}

// HandleMessage notifies the window with the message packet containing
// the message, its type and the application data
INT CMsgHndlr::HandleMessage( LPCSTR lpszMessage, INT nType, DWORD dwAppData )
{
	// Step 1 - Check if window pointer is valid
	if( m_pNotifyWnd == NULL )
	{
		return FAILURE;
	}
	// Step 2 - Check if window handle is valid
	if( IsWindow( m_pNotifyWnd->m_hWnd ) == FALSE )
	{
		return FAILURE;
	}

	MSGPACKET	MsgPacket;

	// Step 3 - Form the message packet
	MsgPacket.m_nType			= nType;
	MsgPacket.m_csMessage	= lpszMessage;
	MsgPacket.m_dwAppData	= dwAppData;
	// Step 4 - Send the message to the notification window
	m_pNotifyWnd->SendMessage( WM_HANDLE_MESSAGE, 0, ( LPARAM ) & MsgPacket );
	return SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -