Click here to Skip to main content
15,894,106 members
Articles / Programming Languages / Objective C

Dynamic Class Factory

Rate me:
Please Sign up or sign in to vote.
4.38/5 (19 votes)
18 Nov 2018CPOL3 min read 91.3K   1.4K   29  
An article about class factory with dynamic subscription / auto registration
// ******************************************************************************
// *
// * Copyright (c) 2003,2004 by Mirec Miskufovic (mirec(dot)m(at)gmx(dot)com).
// * All rights reserved.
// * Permission to use and modify (commercial or non-commercial) is hereby granted subject to inclusion of this copyright notice.
// *
// * It is not allowed to republish the source code so that it is accessible to the public without first obtaining the copyright owner's permission.
// *
// ******************************************************************************
//
// Title        : ClassFactory.h
// Description  : interface for the CClassFactory class
// Created      : 19/08/2003 16:46:18
// Author       : M.Miskufovic
//
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

#if !defined(AFX_CLASSFACTORY_H__CA6330BC_AF6C_460C_AED0_0E76E9D96D55__INCLUDED_)
#define AFX_CLASSFACTORY_H__CA6330BC_AF6C_460C_AED0_0E76E9D96D55__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <map>

template <typename _Key, typename _Base, typename _Predicator = std::less<_Key> >
class CClassFactory
{
public:
	CClassFactory() {};
	~CClassFactory() {};

	typedef _Base* (*CreatorFunction) (void);
	typedef std::map<_Key, CreatorFunction, _Predicator> _mapFactory;

	// called at the beginning of execution to register creation functions
	static _Key RegisterCreatorFunction(_Key idKey, CreatorFunction classCreator)
	{
		get_mapFactory()->insert(std::pair<_Key, CreatorFunction>(idKey, classCreator));
		return idKey;
	}

	// Tries to create instance based on the key
	// using creator function (if provided)
	static _Base* CreateInstance(_Key idKey)
	{
		_mapFactory::iterator it = get_mapFactory()->find(idKey);
		if (it != get_mapFactory()->end())
		{
			if (it->second)
			{
				return it->second();
			}
		}
		return NULL;
	}

protected:
	// Map where the construction info is stored.
	// To prevent inserting into map before initialisation takes place,
	// actual map is a static member, so it will be initialised
	// at the first call
	static _mapFactory * get_mapFactory()
	{
		static _mapFactory m_sMapFactory;
		return &m_sMapFactory;
	}
};

#endif // !defined(AFX_CLASSFACTORY_H__CA6330BC_AF6C_460C_AED0_0E76E9D96D55__INCLUDED_)

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
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions