Click here to Skip to main content
15,881,812 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 184.3K   918   110  
Solving issues with some current implementations of Delegate in C++
#pragma once

///////////////////////////////////////////////////////////////////////////////////
// COMPILE_TIME_JOIN macro
// Identical to BOOST_JOIN

#define COMPILE_TIME_JOIN(X, Y)		COMPILE_TIME_JOIN1(X, Y)
#define COMPILE_TIME_JOIN1(X, Y)	COMPILE_TIME_JOIN2(X, Y)
#define COMPILE_TIME_JOIN2(X, Y)	X##Y

///////////////////////////////////////////////////////////////////////////////////
// COMPILE_TIME_ASSERT macro

namespace compile_time_util
{

template<bool = true> class assert_class
{
public:
	class private_class_if_failed{};
};

template<> class assert_class<false>
{
private:
	class private_class_if_failed{};
};

}

#if defined(_MSC_VER)
#define COMPILE_TIME_ASSERT(expr) class COMPILE_TIME_JOIN(COMPILE_TIME_JOIN(noname_struct_,__LINE__), __COUNTER__) : public compile_time_util::assert_class<(bool)(expr)>::private_class_if_failed {}
#else
#define COMPILE_TIME_ASSERT(expr) class COMPILE_TIME_JOIN(noname_struct_,__LINE__) : public compile_time_util::assert_class<(bool)(expr)>::private_class_if_failed {}
#endif


///////////////////////////////////////////////////////////////////////////////////
// OFFSET_OF macro
// A workaround to avoid problem with standard 'offsetof' when compiling with G++

#if defined(__GNUG__)
#define GCC_OFFSETOF_KEYWORD __offsetof__
#else
#define GCC_OFFSETOF_KEYWORD
#endif

#define OFFSET_OF(TYPE, MEMBER)							\
	(GCC_OFFSETOF_KEYWORD (reinterpret_cast <size_t>	\
	(&reinterpret_cast <const volatile char &>			\
	(reinterpret_cast<TYPE *> (1)->MEMBER))) - 1)

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