Click here to Skip to main content
15,891,840 members
Articles / Programming Languages / C++/CLI

Creating Singleton Objects using Visual C++

Rate me:
Please Sign up or sign in to vote.
4.44/5 (23 votes)
8 Jan 2001CPOL 219.3K   279   73  
This article discusses a Creational Pattern called Singleton and explains different approaches for implementing Singleton pattern using a Visual C++ example.
// MsgHndlr.h : interface of the CMsgHndlr class
//
/////////////////////////////////////////////////////////////////////////////

// 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.

#ifndef _MESSAGEHANDLER
#define _MESSAGEHANDLER

#include <afxtempl.h>
#include "SmtClnr.h"

// List of predefined constants

#ifndef SUCCESS
#define SUCCESS 0
#endif

#ifndef FAILURE
#define FAILURE -1
#endif

// User message to send notification

#define WM_HANDLE_MESSAGE	WM_USER + 1000

// Structure used to send WM_HANDLE_MESSAGE messages

typedef struct tagMSGPACKET
{
	CString	m_csMessage;
	INT		m_nType;
	DWORD		m_dwAppData;
} MSGPACKET, * LPMSGPACKET, ** LPPMSGPACKET;

// List of message types

#define INFO_TYPE			1
#define WARNING_TYPE		2
#define ERROR_TYPE		3

// Class declaration for CMsgHndlr

class CMsgHndlr : public CObject
{
	// Destructor
	public :
		virtual ~CMsgHndlr();

	// Creation - Message Handler Singleton Object
	public :
		DECLARE_DYNCREATE( CMsgHndlr )
		static CMsgHndlr * GetMsgHndlr( LPCSTR = NULL );
		static CMsgHndlr * GetMsgHndlr( CRuntimeClass * = NULL );

	// Method(s) to get information about registered message handlers
	public :
		static INT		GetRegCount();
		static BOOL		IsHndlrRegistered( LPCSTR );

	// Services - Message Handling method(s) that can be redefined by derived class
	public :
		virtual	INT HandleMessage( LPCSTR, INT = INFO_TYPE, DWORD = 0 ); 

	// Services - Windows Notification method(s)
	public :
		INT		SetNotifyWindow( CWnd * );
		CWnd *	GetNotifyWindow();

	protected :
		// Protected constructor
		CMsgHndlr();

	private :
		// Private Copy constructor and Assignment operator, to hide it from clients
		CMsgHndlr( const CMsgHndlr & );
		CMsgHndlr & operator = ( const CMsgHndlr & );

	// Attributes
	protected :
		static CMsgHndlr			* m_pMsgHndlr;
		CWnd							* m_pNotifyWnd;

	// Attributes
	private :
		static CSmartCleaner		m_SmartCleaner;
};
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
Kulathu Sarma is working as a Technology Manager for GoldAvenue, a company based in Geneva, Switzerland and responsible for supporting e-business initiatives of GoldAvenue in B2B and B2C Sectors. He has been programming in C/C++ for 9 years and actively working on Patterns for the past 5 years.

Comments and Discussions