Click here to Skip to main content
15,892,768 members
Articles / Programming Languages / C++

A new way to implement Delegate in C++

Rate me:
Please Sign up or sign in to vote.
4.88/5 (28 votes)
25 May 2007CPOL11 min read 185.2K   918   110  
Solving issues with some current implementations of Delegate in C++
// <http://www.boost.org/doc/html/boost/bad_function_call.html>
struct bad_function_call : public std::runtime_error
{
	bad_function_call() : std::runtime_error("call to empty delegate") {}
};

// <http://www.boost.org/libs/ptr_container/doc/reference.html#class-heap-clone-allocator>
struct heap_clone_allocator
{
	template<typename _T>
		static _T* allocate_clone(const _T& r ) throw()
	{
		return new _T(r);
	}

	template<typename _T>
		static void deallocate_clone(const _T* r ) throw()
	{
		// intentionally complex
		// refer to <http://www.boost.org/libs/utility/checked_delete.html>
		typedef char type_must_be_complete[ sizeof(_T)? 1: -1 ];
		(void) sizeof(type_must_be_complete);

		delete r;
	}
};

// <http://www.boost.org/libs/ptr_container/doc/reference.html#class-view-clone-allocator>
struct view_clone_allocator
{
	template<typename _T>
		static _T* allocate_clone(const _T& r) throw()
	{	return const_cast<_T*>(&r); }

	static void deallocate_clone(void*)  throw() {}
};

struct com_autoref_clone_allocator
{
	template<typename _T>
		static _T* allocate_clone(const _T& r) throw()
	{
		r.AddRef();
		return const_cast<_T*>(&r);
	}

	template<typename _T>
		static void deallocate_clone(const _T* r ) throw()
	{
		r->Release();
	}
};

template<class clone_allocator_type>
struct clone_option
{
	typedef clone_allocator_type clone_allocator;

	bool clone_for_first_time;
	clone_option(bool clone_for_first_time = true) throw()
		: clone_for_first_time(clone_for_first_time)
	{
	}

	operator bool() const throw()
	{
		return this->clone_for_first_time;
	}
};

struct delegate_root
{
public:

	// is null?
	bool empty() const throw()
	{
		return strategy().is_empty();
	}

	~delegate_root() throw()
	{
		strategy().object_free();
	}

	////////////////////////////////////////////////////////////////////////
	// comparison
	////////////////////////////////////////////////////////////////////////

	int compare(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy());
	}

	bool operator==(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy()) == 0;
	}

	bool operator!=(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy()) != 0;
	}

	bool operator>(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy()) > 0;
	}

	bool operator>=(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy()) >= 0;
	}

	bool operator<(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy()) < 0;
	}

	bool operator<=(const delegate_root& _other) const throw()
	{
		return strategy_root::compare(this->strategy(), _other.strategy()) <= 0;
	}

protected:

	class _never_exist_class_;
	typedef _never_exist_class_ clear_type;

	struct strategy_root
	{
		// is null?
		virtual bool is_empty() const throw() { return false; }
		// clone object
		virtual void on_clone_object(void**) const throw() = 0;
		// free object
		virtual void on_free_object(void*) const throw() = 0;

		_never_exist_class_* m_object_ptr;
		union greatest_pointer_type
		{
			typedef void (*pointer_to_function)();
			typedef void (_never_exist_class_::*pointer_to_method)();
			char unused1[sizeof(pointer_to_function)];
			char unused2[sizeof(pointer_to_method)];
		}	m_fn;

		void init_null() throw()
		{
			COMPILE_TIME_ASSERT(OFFSET_OF(strategy_root,m_fn) > OFFSET_OF(strategy_root,m_object_ptr));
			memset(&m_object_ptr, 0,
				OFFSET_OF(strategy_root,m_fn) + sizeof(m_fn) - OFFSET_OF(strategy_root,m_object_ptr));
		}

		template<typename TFunction>
			void init_function(TFunction fn) throw()
		{
			m_object_ptr = 0;
			COMPILE_TIME_ASSERT(sizeof(m_fn)>=sizeof(fn));
			reinterpret_cast<TFunction&>(m_fn) = fn;
			// fill redundant bytes with ZERO
			memset((char*)&m_fn + sizeof(fn), 0, sizeof(m_fn) - sizeof(fn));
		}

		template<class _T, typename TMethod> void init_method(_T* p, TMethod fn, bool init_clone_object)
		{
			// translate pointer-to-member to its large form
			class _another_never_exist_class_;
			typedef void (_another_never_exist_class_::*large_pointer_to_method)();
			typedef	void (_T::*small_pointer_to_method)();

			union
			{
				small_pointer_to_method _small_pointer;
				TMethod _fn;
			};
			union
			{
				large_pointer_to_method _large_pointer;
				greatest_pointer_type _place_holder;
			};

			class _another_never_exist_class_ : public _T {};

			COMPILE_TIME_ASSERT(
				(sizeof(_large_pointer)==sizeof(_place_holder)) &&
				(sizeof(_small_pointer)==sizeof(_fn)));

			_fn = fn;
			_large_pointer = _small_pointer;
			m_fn = _place_holder;

			// clone object
			m_object_ptr = reinterpret_cast<_never_exist_class_*>(
				static_cast<_another_never_exist_class_*>(p));
			if(init_clone_object)
			{
				this->object_clone();
			}
		}

		static int compare(const strategy_root& _left,
			const strategy_root& _right) throw()
		{
			COMPILE_TIME_ASSERT(OFFSET_OF(strategy_root,m_fn)
				==(OFFSET_OF(strategy_root,m_object_ptr) + sizeof(_left.m_object_ptr)));

			return memcmp(&(_left.m_object_ptr), &(_right.m_object_ptr),
				sizeof(_left.m_object_ptr) + sizeof(_left.m_fn));
		}

		void object_clone() throw()
		{
			if(0 != this->m_object_ptr)
			{
				this->on_clone_object(&reinterpret_cast<void*&>(m_object_ptr));
			}
		}
		void object_free() throw()
		{
			if(0 != this->m_object_ptr)
			{
				this->on_free_object(m_object_ptr);
			}
		}
	};

	struct clone_option_never_clone : public clone_option<view_clone_allocator>
	{
		clone_option_never_clone() throw()
			: clone_option<view_clone_allocator>(false)
		{
		}

		struct strategy : public strategy_root
		{
			virtual void on_clone_object(void** pp) const throw()
			{
				// Do nothing
			}
			virtual void on_free_object(void* p) const throw()
			{
				// Do nothing
			}
		};
	};

	template<class object_type, typename clone_allocator_type>
	struct clone_option_rebind : public clone_option<clone_allocator_type>
	{
		clone_option_rebind(bool clone_for_first_time) throw()
			: clone_option<clone_allocator_type>(clone_for_first_time)
		{
		}

		struct strategy : public strategy_root
		{
			// clone object
			virtual void on_clone_object(void** pp) const throw()
			{
				*pp = clone_allocator_type::allocate_clone(*reinterpret_cast<object_type*>(*pp));
			}
			// free object
			virtual void on_free_object(void* p) const throw()
			{
				clone_allocator_type::deallocate_clone(reinterpret_cast<object_type*>(p));
			}
		};
	};

	strategy_root const& strategy() const throw()
	{
		return *reinterpret_cast<strategy_root const*>(&m_strategy);
	}

	strategy_root& strategy() throw()
	{
		return *reinterpret_cast<strategy_root*>(&m_strategy);
	}

	struct strategy_place_holder
	{
		char unused[sizeof(strategy_root)]; 
	}	m_strategy;
};

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
Global Cybersoft (Vietnam)
Vietnam Vietnam
Quynh Nguyen is a Vietnamese who has worked for 7 years in Software Outsourcing area. Currently, he works for Global Cybersoft (Vietnam) Ltd. as a Project Manager in Factory Automation division.

In the first day learning C language in university, he had soon switched to Assembly language because he was not able to understand why people cannot get address of a constant as with a variable. With that stupid starting, he had spent a lot of his time with Assembly language during the time he was in university.

Now he is interesting in Software Development Process, Software Architecture and Design Pattern… He especially indulges in highly concurrent software.

Comments and Discussions