Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Easy Profiler - Compile-time Profiler for C++

, 14 Nov 2009 Apache
Easily instrument your code, visualize, interpret results, track optimization, compare and decide.
Deploy.zip
Collector
Collector.dll
Collector.lib
Observer
Chart Provider.dll
CLRAdapter.dll
Collector.dll
Observer.exe
Misc.zip
Easy Profiler.chm
Ressources
analyze.ico
analyzeExport16.ico
Book1.xlsx
ChartDesign.ico
chartDesign16.ico
ChartFormat16.ico
compare.ico
compare16.ico
Export.ico
Export16.ico
fonts.ico
front.png
Home.ico
Outlook.ico
Project1.ico
SmallButtons.ico
Support.ico
Support16.ico
TREE.ico
Source.zip
Chart Provider
app.ico
Chart Provider.vcproj.STANDARD.Administrateur.user
Release
CLRAdapter
CLRAdapter.def
CLRAdapter.vcproj.STANDARD.Administrateur.user
Release
res
Profiler.vcproj.STANDARD.Administrateur.user
Release
Back
Observer.vcproj.STANDARD.Administrateur.user
Release
res
analyze.bmp
analyzeExport16.bmp
buttons.bmp
ChartDesign.bmp
chartDesign16.bmp
ChartDesignLarge.bmp
ChartFormat.bmp
ChartFormatLarge.bmp
ChartLayout.bmp
ChartLayoutLarge.bmp
ChartMenu.bmp
compare.bmp
Copie de main.bmp
Export.bmp
Export16.bmp
filelarge.bmp
filesmall.bmp
main.bmp
nav_large.bmp
nav_large_hc.bmp
Observer.ico
ObserverDoc.ico
pages.bmp
pages_hc.bmp
pages_small.bmp
pages_small_hc.bmp
Project3.ico
Project4.ico
Project6.ico
Support.bmp
Support16.bmp
Toolbar.bmp
Toolbar256.bmp
treeSmall.bmp
WriteFont.bmp
writelarge.bmp
writesmall.bmp
TipOfTheDay
xHTML
Output
Chart Provider.dll
CLRAdapter.dll
CLRAdapter.lib
Collector.dll
Collector.lib
Observer.exe
Setup
Setup.vdproj
//
//  Generic Class Factory
//  (C) Robert A. T. K�ldy <kaldy@matfyz.cz>
//  last updated on 17.4.2004


#if !defined(FACTORY_H)
#define FACTORY_H

#pragma warning(disable: 4786)

#include <map>
#include <exception>
#include <utility>
#include <typeinfo.h>


#define DECLARE_CLASS_FACTORY(factory)							\
	extern class_factory<void, int, empty_tag> factory;

#define DECLARE_CLASS_FACTORY_EX(Root, Key, Tag, factory)		\
	extern class_factory<Root, Key, Tag> factory;

#define DECLARE_CLASS_FACTORY_EXT(Root, factory)		\
	extern class_factory<Root, int, empty_tag> factory;

#define IMPLEMENT_CLASS_FACTORY(factory)						\
	class_factory<void, int, empty_tag> factory;				\
	int class_factory<void, int, empty_tag>::initial_lock;

#define IMPLEMENT_CLASS_FACTORY_EX(Root, Key, Tag, factory)		\
	class_factory<Root, Key, Tag> factory;						\
	int class_factory<Root, Key, Tag>::initial_lock;

#define IMPLEMENT_CLASS_FACTORY_EXT(Root, factory)		\
	class_factory<Root, int, empty_tag> factory;						\
	int class_factory<Root, int, empty_tag>::initial_lock;

#define REGISTER_CLASS(factory, class_name, key)				\
	class_factory_wrapper _register_class_##class_name((factory.register_class((class_name*)0, key), 0));

#define REGISTER_TAGGED_CLASS(factory, class_name, key, tag)	\
	class_factory_wrapper _register_class_##class_name((factory.register_class((class_name*)0, key, tag), 0));

#define SET_TAG_PROPERTY(factory, class_name, property, value)	\
	class_factory_wrapper _set_tag_property_##class_name##property((factory.tag_ref(typeid(class_name)).property = value, 0));


class not_registered : public std::exception
{
public:
	virtual const char* what() const
	{
		return "Class/key not registered";
	}
};

template<class Root, class T>
class class_creator
{
public:
	static Root* create()
	{
		return new T;
	}
};

template<class Root, class Key, class Tag>
class class_info
{
public:
	Key key;
	Tag tag;
	Root* (*create)();
};

class empty_tag
{};

class class_factory_wrapper
{
public:
	class_factory_wrapper(int)								{}
};


template<class Root, class Key = int, class Tag = empty_tag>
class class_factory
{
	typename typedef class_info<Root, Key, Tag> class_info_type;
	typename typedef std::map<const type_info*, class_info_type>::iterator classes_iterator;
	typename typedef std::map<Key, class_info_type*>::iterator key_map_iterator;

	class_factory(const class_factory<Root, Key, Tag>& rhs)	{}

public:
	class_factory()	
	{
		unlock();
	}
	~class_factory()
	{
		delete classes;
		delete key_map;
	}

	const Key key(const type_info& ti) throw(not_registered)
	{
		unlock();
		classes_iterator i = classes->find(&ti);
		if (i == classes->end())
			throw not_registered();
		return (*i).second.key;
	}
	const Tag& tag(const type_info& ti) throw(not_registered)
	{
		unlock();
		classes_iterator i = classes->find(&ti);
		if (i == classes->end())
			throw not_registered();
		return (*i).second.tag;
	}
	const Tag& tag(const Key key) throw(not_registered)
	{
		unlock();
		key_map_iterator i = key_map->find(key);
		if (i == key_map->end())
			throw not_registered();
		return (*i).second->tag;
	}
	Tag& tag_ref(const type_info& ti)
	{
		unlock();
		return (*classes)[&ti].tag;
	}
	Root* create(const Key key) throw(not_registered)
	{
		unlock();
		key_map_iterator i = key_map->find(key);
		if (i == key_map->end())
			throw not_registered();
		return (*i).second->create();
	}

	template<class T>
	void register_class(const T* t, const Key key)
	{
		unlock();
		class_info_type& info = (*classes)[&typeid(T)];
		info.key = key;
		info.create = class_creator<Root, T>::create;
		key_map->insert(std::make_pair<Key, class_info_type*>(key, &info));
	}
	template<class T>
	void register_class(const T* t, const Key key, const Tag tag)
	{
		unlock();
		class_info_type& info = (*classes)[typeid(T)];
		info.key = key;
		info.tag = tag;
		info.create = class_creator<Root, T>::create;
		key_map->insert(std::make_pair<Key, class_info_type*>(key, &info));
	}

private:
	void unlock()
	{
		if (initial_lock == 0)
		{
			classes = new std::map<const type_info*, class_info_type>;
			key_map = new std::map<Key, class_info_type*>;
			initial_lock = 1;
		}
	}

	static int initial_lock;
	std::map<const type_info*, class_info_type>* classes;
	std::map<Key, class_info_type*>* key_map; 
};


#endif	// FACTORY_H

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 Apache License, Version 2.0

Share

About the Author

Ahmed Charfeddine
Technical Lead
Tunisia Tunisia
Services:
http://www.pushframework.com/?page_id=890
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411028.1 | Last Updated 14 Nov 2009
Article Copyright 2009 by Ahmed Charfeddine
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid