Click here to Skip to main content
15,878,953 members
Articles / Desktop Programming / MFC

The SBJ MVC Framework - The Design View, Responding to Model Change

Rate me:
Please Sign up or sign in to vote.
4.87/5 (22 votes)
19 Mar 2009CPOL13 min read 80.3K   2.4K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
//------------------------------------------------------------------------------
// $Workfile: Controller.cpp $
// $Header: /SbjDev/SbjCore/Controller.cpp 7     11/12/08 2:22p Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 7 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "Controller.h"
#include "UndoRedoMgr.h"


#include <map>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

namespace SbjCore
{
	namespace Mvc
	{
		// ControllerImpl ///////////////////////////////////////////////

		struct ControllerImpl
		{
			std::map<UINT, MessageHandler*> theHandlerMap;
			HANDLE hItem;
			

			ControllerImpl() :
				hItem(NULL)
			{
			}

			virtual ~ControllerImpl()
			{
				try 
				{
					theHandlerMap.clear();
				}
				catch (...)
				{
					ASSERT(FALSE); // we want to know about it 
				}
			}

		};

		// Controller ///////////////////////////////////////////////
		
		IMPLEMENT_DYNCREATE(Controller, CObject)		
		
		Controller::Controller() :
			m_pImpl(new ControllerImpl)
		{
		}

		Controller::~Controller()
		{
			try 
			{
				delete m_pImpl;
			}
			catch (...)
			{
				ASSERT(FALSE);
			}

		}
		
		/// calls virtual OnInitialize()
		void Controller::Initialize()
		{
			OnInitialize();
		}

		/// performs Controller specific initialization. The default does nothing.
		void Controller::OnInitialize()
		{
			// nothing
		}
		

		// AddHandler adds a MessageHandler for nID to the map
		void Controller::AddHandler(
			UINT nID,			
			MessageHandler* p)	
		{
			MessageHandler* pPrev = GetHandler(nID);
			
			p->Initialize(this, pPrev);

			m_pImpl->theHandlerMap[nID] = p;
		}

		// AddHandler adds a MessageHandler for a range of IDs to the map
		void Controller::AddHandler(
			UINT nFirstID,		
			UINT nLastID,		
			MessageHandler* p)	
		{
			ASSERT(nFirstID < nLastID);

			for (UINT nIndex = nFirstID; nIndex <= nLastID; nIndex++)
			{
				AddHandler(nIndex, p);
			}
		}

		/// RemoveHandler removes the MessageHandler for nID
		void Controller::RemoveHandler(UINT nID)
		{
			m_pImpl->theHandlerMap.erase(nID);
		}

		/// RemoveHandler removes the MessageHandler for a range of IDs
		void Controller::RemoveHandler(
			UINT nFirstID,	
			UINT nLastID)	
		{
			ASSERT(nFirstID < nLastID);

			for (UINT nIndex = nFirstID; nIndex <= nLastID; nIndex++)
			{
				RemoveHandler(nIndex);
			}
		}

		///	GetHandler returns the stored handler for nID
		MessageHandler *Controller::GetHandler(UINT nID) const
		{
			MessageHandler* p = dynamic_cast<MessageHandler*>(m_pImpl->theHandlerMap[nID]);
			return p;
		}

	}
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/Controller.cpp $
// 
// 7     11/12/08 2:22p Steve
// Finished Generalization of Model  v2.0.1
// 
// 6     10/14/08 1:12p Steve
// Implemented Deletes

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
SBJ
United States United States
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.

Comments and Discussions