Date:2010-06-18 03:09:00 (4 years 3 months ago)
Author:Maarten ter Huurne
Commit:39f01f763e05a308c0acdd1ffef55f5b8ffb4090
Message:Convert source files to UNIX newlines.

Most of the files were already using LF (UNIX) newlines but some were using CRLF (Windows) newlines.
Files: src/FastDelegate.h (1 diff)
src/cpu.cpp (4 diffs)
src/cpu.h (1 diff)
src/jz4740.h (1 diff)
src/selectordetector.cpp (1 diff)
src/selectordetector.h (1 diff)
src/sfontplus.h (1 diff)

Change Details

src/FastDelegate.h
1// FastDelegate.h
2// Efficient delegates in C++ that generate only two lines of asm code!
3// Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp
4//
5// - Don Clugston, Mar 2004.
6// Major contributions were made by Jody Hagins.
7// History:
8// 24-Apr-04 1.0 * Submitted to CodeProject.
9// 28-Apr-04 1.1 * Prevent most unsafe uses of evil static function hack.
10// * Improved syntax for horrible_cast (thanks Paul Bludov).
11// * Tested on Metrowerks MWCC and Intel ICL (IA32)
12// * Compiled, but not run, on Comeau C++ and Intel Itanium ICL.
13// 27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5
14// * Now works on /clr "managed C++" code on VC7, VC7.1
15// * Comeau C++ now compiles without warnings.
16// * Prevent the virtual inheritance case from being used on
17// VC6 and earlier, which generate incorrect code.
18// * Improved warning and error messages. Non-standard hacks
19// now have compile-time checks to make them safer.
20// * implicit_cast used instead of static_cast in many cases.
21// * If calling a const member function, a const class pointer can be used.
22// * MakeDelegate() global helper function added to simplify pass-by-value.
23// * Added fastdelegate.clear()
24// 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates)
25// 30-Oct-04 1.3 * Support for (non-void) return values.
26// * No more workarounds in client code!
27// MSVC and Intel now use a clever hack invented by John Dlugosz:
28// - The FASTDELEGATEDECLARE workaround is no longer necessary.
29// - No more warning messages for VC6
30// * Less use of macros. Error messages should be more comprehensible.
31// * Added include guards
32// * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
33// * Now tested on VS 2005 Express Beta, PGI C++
34// 24-Dec-04 1.4 * Added DelegateMemento, to allow collections of disparate delegates.
35// * <,>,<=,>= comparison operators to allow storage in ordered containers.
36// * Substantial reduction of code size, especially the 'Closure' class.
37// * Standardised all the compiler-specific workarounds.
38// * MFP conversion now works for CodePlay (but not yet supported in the full code).
39// * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1
40// * New syntax: FastDelegate< int (char *, double) >.
41// 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric).
42// * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium.
43// 30-Mar-05 1.5 * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())"
44// * Fully supported by CodePlay VectorC
45// * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC!
46// * More optimal assignment,== and != operators for static function pointers.
47
48#ifndef FASTDELEGATE_H
49#define FASTDELEGATE_H
50#if _MSC_VER > 1000
51#pragma once
52#endif // _MSC_VER > 1000
53
54#include <memory.h> // to allow <,> comparisons
55
56////////////////////////////////////////////////////////////////////////////////
57// Configuration options
58//
59////////////////////////////////////////////////////////////////////////////////
60
61// Uncomment the following #define for optimally-sized delegates.
62// In this case, the generated asm code is almost identical to the code you'd get
63// if the compiler had native support for delegates.
64// It will not work on systems where sizeof(dataptr) < sizeof(codeptr).
65// Thus, it will not work for DOS compilers using the medium model.
66// It will also probably fail on some DSP systems.
67#define FASTDELEGATE_USESTATICFUNCTIONHACK
68
69// Uncomment the next line to allow function declarator syntax.
70// It is automatically enabled for those compilers where it is known to work.
71//#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
72
73////////////////////////////////////////////////////////////////////////////////
74// Compiler identification for workarounds
75//
76////////////////////////////////////////////////////////////////////////////////
77
78// Compiler identification. It's not easy to identify Visual C++ because
79// many vendors fraudulently define Microsoft's identifiers.
80#if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__)
81#define FASTDLGT_ISMSVC
82
83#if (_MSC_VER <1300) // Many workarounds are required for VC6.
84#define FASTDLGT_VC6
85#pragma warning(disable:4786) // disable this ridiculous warning
86#endif
87
88#endif
89
90// Does the compiler uses Microsoft's member function pointer structure?
91// If so, it needs special treatment.
92// Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's
93// identifier, _MSC_VER. We need to filter Metrowerks out.
94#if defined(_MSC_VER) && !defined(__MWERKS__)
95#define FASTDLGT_MICROSOFT_MFP
96
97#if !defined(__VECTOR_C)
98// CodePlay doesn't have the __single/multi/virtual_inheritance keywords
99#define FASTDLGT_HASINHERITANCE_KEYWORDS
100#endif
101#endif
102
103// Does it allow function declarator syntax? The following compilers are known to work:
104#if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1
105#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
106#endif
107
108// Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use.
109#if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__)
110#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
111#endif
112
113// It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too.
114#if defined (__MWERKS__)
115#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
116#endif
117
118#ifdef __GNUC__ // Workaround GCC bug #8271
119    // At present, GCC doesn't recognize constness of MFPs in templates
120#define FASTDELEGATE_GCC_BUG_8271
121#endif
122
123
124
125////////////////////////////////////////////////////////////////////////////////
126// General tricks used in this code
127//
128// (a) Error messages are generated by typdefing an array of negative size to
129// generate compile-time errors.
130// (b) Warning messages on MSVC are generated by declaring unused variables, and
131// enabling the "variable XXX is never used" warning.
132// (c) Unions are used in a few compiler-specific cases to perform illegal casts.
133// (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to
134// (char *) first to ensure that the correct number of *bytes* are added.
135//
136////////////////////////////////////////////////////////////////////////////////
137// Helper templates
138//
139////////////////////////////////////////////////////////////////////////////////
140
141
142namespace fastdelegate {
143namespace detail { // we'll hide the implementation details in a nested namespace.
144
145// implicit_cast< >
146// I believe this was originally going to be in the C++ standard but
147// was left out by accident. It's even milder than static_cast.
148// I use it instead of static_cast<> to emphasize that I'm not doing
149// anything nasty.
150// Usage is identical to static_cast<>
151template <class OutputClass, class InputClass>
152inline OutputClass implicit_cast(InputClass input){
153    return input;
154}
155
156// horrible_cast< >
157// This is truly evil. It completely subverts C++'s type system, allowing you
158// to cast from any class to any other class. Technically, using a union
159// to perform the cast is undefined behaviour (even in C). But we can see if
160// it is OK by checking that the union is the same size as each of its members.
161// horrible_cast<> should only be used for compiler-specific workarounds.
162// Usage is identical to reinterpret_cast<>.
163
164// This union is declared outside the horrible_cast because BCC 5.5.1
165// can't inline a function with a nested class, and gives a warning.
166template <class OutputClass, class InputClass>
167union horrible_union{
168    OutputClass out;
169    InputClass in;
170};
171
172template <class OutputClass, class InputClass>
173inline OutputClass horrible_cast(const InputClass input){
174    horrible_union<OutputClass, InputClass> u;
175    // Cause a compile-time error if in, out and u are not the same size.
176    // If the compile fails here, it means the compiler has peculiar
177    // unions which would prevent the cast from working.
178    typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u)
179        && sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];
180    u.in = input;
181    return u.out;
182}
183
184////////////////////////////////////////////////////////////////////////////////
185// Workarounds
186//
187////////////////////////////////////////////////////////////////////////////////
188
189// Backwards compatibility: This macro used to be necessary in the virtual inheritance
190// case for Intel and Microsoft. Now it just forward-declares the class.
191#define FASTDELEGATEDECLARE(CLASSNAME) class CLASSNAME;
192
193// Prevent use of the static function hack with the DOS medium model.
194#ifdef __MEDIUM__
195#undef FASTDELEGATE_USESTATICFUNCTIONHACK
196#endif
197
198// DefaultVoid - a workaround for 'void' templates in VC6.
199//
200// (1) VC6 and earlier do not allow 'void' as a default template argument.
201// (2) They also doesn't allow you to return 'void' from a function.
202//
203// Workaround for (1): Declare a dummy type 'DefaultVoid' which we use
204// when we'd like to use 'void'. We convert it into 'void' and back
205// using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>.
206// Workaround for (2): On VC6, the code for calling a void function is
207// identical to the code for calling a non-void function in which the
208// return value is never used, provided the return value is returned
209// in the EAX register, rather than on the stack.
210// This is true for most fundamental types such as int, enum, void *.
211// Const void * is the safest option since it doesn't participate
212// in any automatic conversions. But on a 16-bit compiler it might
213// cause extra code to be generated, so we disable it for all compilers
214// except for VC6 (and VC5).
215#ifdef FASTDLGT_VC6
216// VC6 workaround
217typedef const void * DefaultVoid;
218#else
219// On any other compiler, just use a normal void.
220typedef void DefaultVoid;
221#endif
222
223// Translate from 'DefaultVoid' to 'void'.
224// Everything else is unchanged
225template <class T>
226struct DefaultVoidToVoid { typedef T type; };
227
228template <>
229struct DefaultVoidToVoid<DefaultVoid> { typedef void type; };
230
231// Translate from 'void' into 'DefaultVoid'
232// Everything else is unchanged
233template <class T>
234struct VoidToDefaultVoid { typedef T type; };
235
236template <>
237struct VoidToDefaultVoid<void> { typedef DefaultVoid type; };
238
239
240
241////////////////////////////////////////////////////////////////////////////////
242// Fast Delegates, part 1:
243//
244// Conversion of member function pointer to a standard form
245//
246////////////////////////////////////////////////////////////////////////////////
247
248// GenericClass is a fake class, ONLY used to provide a type.
249// It is vitally important that it is never defined, so that the compiler doesn't
250// think it can optimize the invocation. For example, Borland generates simpler
251// code if it knows the class only uses single inheritance.
252
253// Compilers using Microsoft's structure need to be treated as a special case.
254#ifdef FASTDLGT_MICROSOFT_MFP
255
256#ifdef FASTDLGT_HASINHERITANCE_KEYWORDS
257    // For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP
258    // (4 bytes), even when the /vmg option is used. Declaring an empty class
259    // would give 16 byte pointers in this case....
260    class __single_inheritance GenericClass;
261#endif
262    // ...but for Codeplay, an empty class *always* gives 4 byte pointers.
263    // If compiled with the /clr option ("managed C++"), the JIT compiler thinks
264    // it needs to load GenericClass before it can call any of its functions,
265    // (compiles OK but crashes at runtime!), so we need to declare an
266    // empty class to make it happy.
267    // Codeplay and VC4 can't cope with the unknown_inheritance case either.
268    class GenericClass {};
269#else
270    class GenericClass;
271#endif
272
273// The size of a single inheritance member function pointer.
274const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)());
275
276// SimplifyMemFunc< >::Convert()
277//
278// A template function that converts an arbitrary member function pointer into the
279// simplest possible form of member function pointer, using a supplied 'this' pointer.
280// According to the standard, this can be done legally with reinterpret_cast<>.
281// For (non-standard) compilers which use member function pointers which vary in size
282// depending on the class, we need to use knowledge of the internal structure of a
283// member function pointer, as used by the compiler. Template specialization is used
284// to distinguish between the sizes. Because some compilers don't support partial
285// template specialisation, I use full specialisation of a wrapper struct.
286
287// general case -- don't know how to convert it. Force a compile failure
288template <int N>
289struct SimplifyMemFunc {
290    template <class X, class XFuncType, class GenericMemFuncType>
291    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
292        GenericMemFuncType &bound_func) {
293        // Unsupported member function type -- force a compile failure.
294        // (it's illegal to have a array with negative size).
295        typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100];
296        return 0;
297    }
298};
299
300// For compilers where all member func ptrs are the same size, everything goes here.
301// For non-standard compilers, only single_inheritance classes go here.
302template <>
303struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE> {
304    template <class X, class XFuncType, class GenericMemFuncType>
305    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
306            GenericMemFuncType &bound_func) {
307#if defined __DMC__
308        // Digital Mars doesn't allow you to cast between abitrary PMF's,
309        // even though the standard says you can. The 32-bit compiler lets you
310        // static_cast through an int, but the DOS compiler doesn't.
311        bound_func = horrible_cast<GenericMemFuncType>(function_to_bind);
312#else
313        bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind);
314#endif
315        return reinterpret_cast<GenericClass *>(pthis);
316    }
317};
318
319////////////////////////////////////////////////////////////////////////////////
320// Fast Delegates, part 1b:
321//
322// Workarounds for Microsoft and Intel
323//
324////////////////////////////////////////////////////////////////////////////////
325
326
327// Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay),
328// need to be treated as a special case.
329#ifdef FASTDLGT_MICROSOFT_MFP
330
331// We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1)
332// at the start of each function for extra safety, but VC6 seems to ICE
333// intermittently if you do this inside a template.
334
335// __multiple_inheritance classes go here
336// Nasty hack for Microsoft and Intel (IA32 and Itanium)
337template<>
338struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) > {
339    template <class X, class XFuncType, class GenericMemFuncType>
340    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
341        GenericMemFuncType &bound_func) {
342        // We need to use a horrible_cast to do this conversion.
343        // In MSVC, a multiple inheritance member pointer is internally defined as:
344        union {
345            XFuncType func;
346            struct {
347                GenericMemFuncType funcaddress; // points to the actual member function
348                int delta; // #BYTES to be added to the 'this' pointer
349            }s;
350        } u;
351        // Check that the horrible_cast will work
352        typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1];
353        u.func = function_to_bind;
354        bound_func = u.s.funcaddress;
355        return reinterpret_cast<GenericClass *>(reinterpret_cast<char *>(pthis) + u.s.delta);
356    }
357};
358
359// virtual inheritance is a real nuisance. It's inefficient and complicated.
360// On MSVC and Intel, there isn't enough information in the pointer itself to
361// enable conversion to a closure pointer. Earlier versions of this code didn't
362// work for all cases, and generated a compile-time error instead.
363// But a very clever hack invented by John M. Dlugosz solves this problem.
364// My code is somewhat different to his: I have no asm code, and I make no
365// assumptions about the calling convention that is used.
366
367// In VC++ and ICL, a virtual_inheritance member pointer
368// is internally defined as:
369struct MicrosoftVirtualMFP {
370    void (GenericClass::*codeptr)(); // points to the actual member function
371    int delta; // #bytes to be added to the 'this' pointer
372    int vtable_index; // or 0 if no virtual inheritance
373};
374// The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the
375// m_codeptr member is *always* called, regardless of the values of the other
376// members. (This is *not* true for other compilers, eg GCC, which obtain the
377// function address from the vtable if a virtual function is being called).
378// Dlugosz's trick is to make the codeptr point to a probe function which
379// returns the 'this' pointer that was used.
380
381// Define a generic class that uses virtual inheritance.
382// It has a trival member function that returns the value of the 'this' pointer.
383struct GenericVirtualClass : virtual public GenericClass
384{
385    typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
386    GenericVirtualClass * GetThis() { return this; }
387};
388
389// __virtual_inheritance classes go here
390template <>
391struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2*sizeof(int) >
392{
393
394    template <class X, class XFuncType, class GenericMemFuncType>
395    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
396        GenericMemFuncType &bound_func) {
397        union {
398            XFuncType func;
399            GenericClass* (X::*ProbeFunc)();
400            MicrosoftVirtualMFP s;
401        } u;
402        u.func = function_to_bind;
403        bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr);
404        union {
405            GenericVirtualClass::ProbePtrType virtfunc;
406            MicrosoftVirtualMFP s;
407        } u2;
408        // Check that the horrible_cast<>s will work
409        typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)
410            && sizeof(function_to_bind)==sizeof(u.ProbeFunc)
411            && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
412   // Unfortunately, taking the address of a MF prevents it from being inlined, so
413   // this next line can't be completely optimised away by the compiler.
414        u2.virtfunc = &GenericVirtualClass::GetThis;
415        u.s.codeptr = u2.s.codeptr;
416        return (pthis->*u.ProbeFunc)();
417    }
418};
419
420#if (_MSC_VER <1300)
421
422// Nasty hack for Microsoft Visual C++ 6.0
423// unknown_inheritance classes go here
424// There is a compiler bug in MSVC6 which generates incorrect code in this case!!
425template <>
426struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
427{
428    template <class X, class XFuncType, class GenericMemFuncType>
429    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
430        GenericMemFuncType &bound_func) {
431        // There is an apalling but obscure compiler bug in MSVC6 and earlier:
432        // vtable_index and 'vtordisp' are always set to 0 in the
433        // unknown_inheritance case!
434        // This means that an incorrect function could be called!!!
435        // Compiling with the /vmg option leads to potentially incorrect code.
436        // This is probably the reason that the IDE has a user interface for specifying
437        // the /vmg option, but it is disabled - you can only specify /vmg on
438        // the command line. In VC1.5 and earlier, the compiler would ICE if it ever
439        // encountered this situation.
440        // It is OK to use the /vmg option if /vmm or /vms is specified.
441
442        // Fortunately, the wrong function is only called in very obscure cases.
443        // It only occurs when a derived class overrides a virtual function declared
444        // in a virtual base class, and the member function
445        // points to the *Derived* version of that function. The problem can be
446        // completely averted in 100% of cases by using the *Base class* for the
447        // member fpointer. Ie, if you use the base class as an interface, you'll
448        // stay out of trouble.
449        // Occasionally, you might want to point directly to a derived class function
450        // that isn't an override of a base class. In this case, both vtable_index
451        // and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated.
452        // We can generate correct code in this case. To prevent an incorrect call from
453        // ever being made, on MSVC6 we generate a warning, and call a function to
454        // make the program crash instantly.
455        typedef char ERROR_VC6CompilerBug[-100];
456        return 0;
457    }
458};
459
460
461#else
462
463// Nasty hack for Microsoft and Intel (IA32 and Itanium)
464// unknown_inheritance classes go here
465// This is probably the ugliest bit of code I've ever written. Look at the casts!
466// There is a compiler bug in MSVC6 which prevents it from using this code.
467template <>
468struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
469{
470    template <class X, class XFuncType, class GenericMemFuncType>
471    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
472            GenericMemFuncType &bound_func) {
473        // The member function pointer is 16 bytes long. We can't use a normal cast, but
474        // we can use a union to do the conversion.
475        union {
476            XFuncType func;
477            // In VC++ and ICL, an unknown_inheritance member pointer
478            // is internally defined as:
479            struct {
480                GenericMemFuncType m_funcaddress; // points to the actual member function
481                int delta; // #bytes to be added to the 'this' pointer
482                int vtordisp; // #bytes to add to 'this' to find the vtable
483                int vtable_index; // or 0 if no virtual inheritance
484            } s;
485        } u;
486        // Check that the horrible_cast will work
487        typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1];
488        u.func = function_to_bind;
489        bound_func = u.s.funcaddress;
490        int virtual_delta = 0;
491        if (u.s.vtable_index) { // Virtual inheritance is used
492            // First, get to the vtable.
493            // It is 'vtordisp' bytes from the start of the class.
494            const int * vtable = *reinterpret_cast<const int *const*>(
495                reinterpret_cast<const char *>(pthis) + u.s.vtordisp );
496
497            // 'vtable_index' tells us where in the table we should be looking.
498            virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
499                reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
500        }
501        // The int at 'virtual_delta' gives us the amount to add to 'this'.
502        // Finally we can add the three components together. Phew!
503        return reinterpret_cast<GenericClass *>(
504            reinterpret_cast<char *>(pthis) + u.s.delta + virtual_delta);
505    };
506};
507#endif // MSVC 7 and greater
508
509#endif // MS/Intel hacks
510
511} // namespace detail
512
513////////////////////////////////////////////////////////////////////////////////
514// Fast Delegates, part 2:
515//
516// Define the delegate storage, and cope with static functions
517//
518////////////////////////////////////////////////////////////////////////////////
519
520// DelegateMemento -- an opaque structure which can hold an arbitary delegate.
521// It knows nothing about the calling convention or number of arguments used by
522// the function pointed to.
523// It supplies comparison operators so that it can be stored in STL collections.
524// It cannot be set to anything other than null, nor invoked directly:
525// it must be converted to a specific delegate.
526
527// Implementation:
528// There are two possible implementations: the Safe method and the Evil method.
529// DelegateMemento - Safe version
530//
531// This implementation is standard-compliant, but a bit tricky.
532// A static function pointer is stored inside the class.
533// Here are the valid values:
534// +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
535// | 0 | 0 | 0 | Empty |
536// | !=0 |(dontcare)| Invoker | Static function|
537// | 0 | !=0 | !=0* | Method call |
538// +--------------------+----------+------------+----------------+
539// * For Metrowerks, this can be 0. (first virtual function in a
540// single_inheritance class).
541// When stored stored inside a specific delegate, the 'dontcare' entries are replaced
542// with a reference to the delegate itself. This complicates the = and == operators
543// for the delegate class.
544
545// DelegateMemento - Evil version
546//
547// For compilers where data pointers are at least as big as code pointers, it is
548// possible to store the function pointer in the this pointer, using another
549// horrible_cast. In this case the DelegateMemento implementation is simple:
550// +--pThis --+-- pMemFunc-+-- Meaning---------------------+
551// | 0 | 0 | Empty |
552// | !=0 | !=0* | Static function or method call|
553// +----------+------------+-------------------------------+
554// * For Metrowerks, this can be 0. (first virtual function in a
555// single_inheritance class).
556// Note that the Sun C++ and MSVC documentation explicitly state that they
557// support static_cast between void * and function pointers.
558
559class DelegateMemento {
560protected:
561    // the data is protected, not private, because many
562    // compilers have problems with template friends.
563    typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP.
564    GenericMemFuncType m_pFunction;
565    detail::GenericClass *m_pthis;
566
567#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
568    typedef void (*GenericFuncPtr)(); // arbitrary code pointer
569    GenericFuncPtr m_pStaticFunction;
570#endif
571
572public:
573#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
574    DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {};
575    void clear() {
576        m_pthis=0; m_pFunction=0; m_pStaticFunction=0;
577    }
578#else
579    DelegateMemento() : m_pFunction(0), m_pthis(0) {};
580    void clear() { m_pthis=0; m_pFunction=0; }
581#endif
582public:
583#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
584    inline bool IsEqual (const DelegateMemento &x) const{
585        // We have to cope with the static function pointers as a special case
586        if (m_pFunction!=x.m_pFunction) return false;
587        // the static function ptrs must either both be equal, or both be 0.
588        if (m_pStaticFunction!=x.m_pStaticFunction) return false;
589        if (m_pStaticFunction!=0) return m_pthis==x.m_pthis;
590        else return true;
591    }
592#else // Evil Method
593    inline bool IsEqual (const DelegateMemento &x) const{
594        return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction;
595    }
596#endif
597    // Provide a strict weak ordering for DelegateMementos.
598    inline bool IsLess(const DelegateMemento &right) const {
599        // deal with static function pointers first
600#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
601        if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0)
602                return m_pStaticFunction < right.m_pStaticFunction;
603#endif
604        if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis;
605    // There are no ordering operators for member function pointers,
606    // but we can fake one by comparing each byte. The resulting ordering is
607    // arbitrary (and compiler-dependent), but it permits storage in ordered STL containers.
608        return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
609
610    }
611    // BUGFIX (Mar 2005):
612    // We can't just compare m_pFunction because on Metrowerks,
613    // m_pFunction can be zero even if the delegate is not empty!
614    inline bool operator ! () const // Is it bound to anything?
615    { return m_pthis==0 && m_pFunction==0; }
616    inline bool empty() const // Is it bound to anything?
617    { return m_pthis==0 && m_pFunction==0; }
618public:
619    DelegateMemento & operator = (const DelegateMemento &right) {
620        SetMementoFrom(right);
621        return *this;
622    }
623    inline bool operator <(const DelegateMemento &right) {
624        return IsLess(right);
625    }
626    inline bool operator >(const DelegateMemento &right) {
627        return right.IsLess(*this);
628    }
629    DelegateMemento (const DelegateMemento &right) :
630        m_pFunction(right.m_pFunction), m_pthis(right.m_pthis)
631#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
632        , m_pStaticFunction (right.m_pStaticFunction)
633#endif
634        {}
635protected:
636    void SetMementoFrom(const DelegateMemento &right) {
637        m_pFunction = right.m_pFunction;
638        m_pthis = right.m_pthis;
639#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
640        m_pStaticFunction = right.m_pStaticFunction;
641#endif
642    }
643};
644
645
646// ClosurePtr<>
647//
648// A private wrapper class that adds function signatures to DelegateMemento.
649// It's the class that does most of the actual work.
650// The signatures are specified by:
651// GenericMemFunc: must be a type of GenericClass member function pointer.
652// StaticFuncPtr: must be a type of function pointer with the same signature
653// as GenericMemFunc.
654// UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6
655// where it never returns void (returns DefaultVoid instead).
656
657// An outer class, FastDelegateN<>, handles the invoking and creates the
658// necessary typedefs.
659// This class does everything else.
660
661namespace detail {
662
663template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr>
664class ClosurePtr : public DelegateMemento {
665public:
666    // These functions are for setting the delegate to a member function.
667
668    // Here's the clever bit: we convert an arbitrary member function into a
669    // standard form. XMemFunc should be a member function of class X, but I can't
670    // enforce that here. It needs to be enforced by the wrapper class.
671    template < class X, class XMemFunc >
672    inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) {
673        m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
674            ::Convert(pthis, function_to_bind, m_pFunction);
675#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
676        m_pStaticFunction = 0;
677#endif
678    }
679    // For const member functions, we only need a const class pointer.
680    // Since we know that the member function is const, it's safe to
681    // remove the const qualifier from the 'this' pointer with a const_cast.
682    // VC6 has problems if we just overload 'bindmemfunc', so we give it a different name.
683    template < class X, class XMemFunc>
684    inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) {
685        m_pthis= SimplifyMemFunc< sizeof(function_to_bind) >
686            ::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction);
687#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
688        m_pStaticFunction = 0;
689#endif
690    }
691#ifdef FASTDELEGATE_GCC_BUG_8271 // At present, GCC doesn't recognize constness of MFPs in templates
692    template < class X, class XMemFunc>
693    inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) {
694        bindconstmemfunc(pthis, function_to_bind);
695#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
696        m_pStaticFunction = 0;
697#endif
698    }
699#endif
700    // These functions are required for invoking the stored function
701    inline GenericClass *GetClosureThis() const { return m_pthis; }
702    inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); }
703
704// There are a few ways of dealing with static function pointers.
705// There's a standard-compliant, but tricky method.
706// There's also a straightforward hack, that won't work on DOS compilers using the
707// medium memory model. It's so evil that I can't recommend it, but I've
708// implemented it anyway because it produces very nice asm code.
709
710#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
711
712// ClosurePtr<> - Safe version
713//
714// This implementation is standard-compliant, but a bit tricky.
715// I store the function pointer inside the class, and the delegate then
716// points to itself. Whenever the delegate is copied, these self-references
717// must be transformed, and this complicates the = and == operators.
718public:
719    // The next two functions are for operator ==, =, and the copy constructor.
720    // We may need to convert the m_pthis pointers, so that
721    // they remain as self-references.
722    template< class DerivedClass >
723    inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) {
724        SetMementoFrom(x);
725        if (m_pStaticFunction!=0) {
726            // transform self references...
727            m_pthis=reinterpret_cast<GenericClass *>(pParent);
728        }
729    }
730    // For static functions, the 'static_function_invoker' class in the parent
731    // will be called. The parent then needs to call GetStaticFunction() to find out
732    // the actual function to invoke.
733    template < class DerivedClass, class ParentInvokerSig >
734    inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
735                StaticFuncPtr function_to_bind ) {
736        if (function_to_bind==0) { // cope with assignment to 0
737            m_pFunction=0;
738        } else {
739            bindmemfunc(pParent, static_function_invoker);
740        }
741        m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind);
742    }
743    inline UnvoidStaticFuncPtr GetStaticFunction() const {
744        return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction);
745    }
746#else
747
748// ClosurePtr<> - Evil version
749//
750// For compilers where data pointers are at least as big as code pointers, it is
751// possible to store the function pointer in the this pointer, using another
752// horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and
753// speeds up comparison and assignment. If C++ provided direct language support
754// for delegates, they would produce asm code that was almost identical to this.
755// Note that the Sun C++ and MSVC documentation explicitly state that they
756// support static_cast between void * and function pointers.
757
758    template< class DerivedClass >
759    inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) {
760        SetMementoFrom(right);
761    }
762    // For static functions, the 'static_function_invoker' class in the parent
763    // will be called. The parent then needs to call GetStaticFunction() to find out
764    // the actual function to invoke.
765    // ******** EVIL, EVIL CODE! *******
766    template < class DerivedClass, class ParentInvokerSig>
767    inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
768                StaticFuncPtr function_to_bind) {
769        if (function_to_bind==0) { // cope with assignment to 0
770            m_pFunction=0;
771        } else {
772           // We'll be ignoring the 'this' pointer, but we need to make sure we pass
773           // a valid value to bindmemfunc().
774            bindmemfunc(pParent, static_function_invoker);
775        }
776
777        // WARNING! Evil hack. We store the function in the 'this' pointer!
778        // Ensure that there's a compilation failure if function pointers
779        // and data pointers have different sizes.
780        // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
781        typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1];
782        m_pthis = horrible_cast<GenericClass *>(function_to_bind);
783        // MSVC, SunC++ and DMC accept the following (non-standard) code:
784// m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind));
785        // BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long
786// m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind));
787    }
788    // ******** EVIL, EVIL CODE! *******
789    // This function will be called with an invalid 'this' pointer!!
790    // We're just returning the 'this' pointer, converted into
791    // a function pointer!
792    inline UnvoidStaticFuncPtr GetStaticFunction() const {
793        // Ensure that there's a compilation failure if function pointers
794        // and data pointers have different sizes.
795        // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
796        typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1];
797        return horrible_cast<UnvoidStaticFuncPtr>(this);
798    }
799#endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
800
801    // Does the closure contain this static function?
802    inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){
803        if (funcptr==0) return empty();
804    // For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary
805    // value that is not equal to any valid function pointer.
806        else return funcptr==reinterpret_cast<StaticFuncPtr>(GetStaticFunction());
807    }
808};
809
810
811} // namespace detail
812
813////////////////////////////////////////////////////////////////////////////////
814// Fast Delegates, part 3:
815//
816// Wrapper classes to ensure type safety
817//
818////////////////////////////////////////////////////////////////////////////////
819
820
821// Once we have the member function conversion templates, it's easy to make the
822// wrapper classes. So that they will work with as many compilers as possible,
823// the classes are of the form
824// FastDelegate3<int, char *, double>
825// They can cope with any combination of parameters. The max number of parameters
826// allowed is 8, but it is trivial to increase this limit.
827// Note that we need to treat const member functions seperately.
828// All this class does is to enforce type safety, and invoke the delegate with
829// the correct list of parameters.
830
831// Because of the weird rule about the class of derived member function pointers,
832// you sometimes need to apply a downcast to the 'this' pointer.
833// This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below.
834// If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction,
835// without this trick you'd need to write:
836// MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction);
837// but with the trick you can write
838// MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction);
839
840// RetType is the type the compiler uses in compiling the template. For VC6,
841// it cannot be void. DesiredRetType is the real type which is returned from
842// all of the functions. It can be void.
843
844// Implicit conversion to "bool" is achieved using the safe_bool idiom,
845// using member data pointers (MDP). This allows "if (dg)..." syntax
846// Because some compilers (eg codeplay) don't have a unique value for a zero
847// MDP, an extra padding member is added to the SafeBool struct.
848// Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so
849// in that case the static function constructor is not made explicit; this
850// allows "if (dg==0) ..." to compile.
851
852//N=0
853template<class RetType=detail::DefaultVoid>
854class FastDelegate0 {
855private:
856    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
857    typedef DesiredRetType (*StaticFunctionPtr)();
858    typedef RetType (*UnvoidStaticFunctionPtr)();
859    typedef RetType (detail::GenericClass::*GenericMemFn)();
860    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
861    ClosureType m_Closure;
862public:
863    // Typedefs to aid generic programming
864    typedef FastDelegate0 type;
865
866    // Construction and comparison functions
867    FastDelegate0() { clear(); }
868    FastDelegate0(const FastDelegate0 &x) {
869        m_Closure.CopyFrom(this, x.m_Closure); }
870    void operator = (const FastDelegate0 &x) {
871        m_Closure.CopyFrom(this, x.m_Closure); }
872    bool operator ==(const FastDelegate0 &x) const {
873        return m_Closure.IsEqual(x.m_Closure); }
874    bool operator !=(const FastDelegate0 &x) const {
875        return !m_Closure.IsEqual(x.m_Closure); }
876    bool operator <(const FastDelegate0 &x) const {
877        return m_Closure.IsLess(x.m_Closure); }
878    bool operator >(const FastDelegate0 &x) const {
879        return x.m_Closure.IsLess(m_Closure); }
880    // Binding to non-const member functions
881    template < class X, class Y >
882    FastDelegate0(Y *pthis, DesiredRetType (X::* function_to_bind)() ) {
883        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
884    template < class X, class Y >
885    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)()) {
886        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
887    // Binding to const member functions.
888    template < class X, class Y >
889    FastDelegate0(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) {
890        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
891    template < class X, class Y >
892    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) {
893        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
894    // Static functions. We convert them into a member function call.
895    // This constructor also provides implicit conversion
896    FastDelegate0(DesiredRetType (*function_to_bind)() ) {
897        bind(function_to_bind); }
898    // for efficiency, prevent creation of a temporary
899    void operator = (DesiredRetType (*function_to_bind)() ) {
900        bind(function_to_bind); }
901    inline void bind(DesiredRetType (*function_to_bind)()) {
902        m_Closure.bindstaticfunc(this, &FastDelegate0::InvokeStaticFunction,
903            function_to_bind); }
904    // Invoke the delegate
905    RetType operator() () const {
906    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(); }
907    // Implicit conversion to "bool" using the safe_bool idiom
908private:
909    typedef struct SafeBoolStruct {
910        int a_data_pointer_to_this_is_0_on_buggy_compilers;
911        StaticFunctionPtr m_nonzero;
912    } UselessTypedef;
913    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
914public:
915    operator unspecified_bool_type() const {
916        return empty()? 0: &SafeBoolStruct::m_nonzero;
917    }
918    // necessary to allow ==0 to work despite the safe_bool idiom
919    inline bool operator==(StaticFunctionPtr funcptr) {
920        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
921    inline bool operator!=(StaticFunctionPtr funcptr) {
922        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
923    inline bool operator ! () const { // Is it bound to anything?
924            return !m_Closure; }
925    inline bool empty() const {
926            return !m_Closure; }
927    void clear() { m_Closure.clear();}
928    // Conversion to and from the DelegateMemento storage class
929    const DelegateMemento & GetMemento() { return m_Closure; }
930    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
931
932private: // Invoker for static functions
933    RetType InvokeStaticFunction() const {
934    return (*(m_Closure.GetStaticFunction()))(); }
935};
936
937//N=1
938template<class Param1, class RetType=detail::DefaultVoid>
939class FastDelegate1 {
940private:
941    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
942    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1);
943    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1);
944    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1);
945    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
946    ClosureType m_Closure;
947public:
948    // Typedefs to aid generic programming
949    typedef FastDelegate1 type;
950
951    // Construction and comparison functions
952    FastDelegate1() { clear(); }
953    FastDelegate1(const FastDelegate1 &x) {
954        m_Closure.CopyFrom(this, x.m_Closure); }
955    void operator = (const FastDelegate1 &x) {
956        m_Closure.CopyFrom(this, x.m_Closure); }
957    bool operator ==(const FastDelegate1 &x) const {
958        return m_Closure.IsEqual(x.m_Closure); }
959    bool operator !=(const FastDelegate1 &x) const {
960        return !m_Closure.IsEqual(x.m_Closure); }
961    bool operator <(const FastDelegate1 &x) const {
962        return m_Closure.IsLess(x.m_Closure); }
963    bool operator >(const FastDelegate1 &x) const {
964        return x.m_Closure.IsLess(m_Closure); }
965    // Binding to non-const member functions
966    template < class X, class Y >
967    FastDelegate1(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) ) {
968        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
969    template < class X, class Y >
970    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1)) {
971        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
972    // Binding to const member functions.
973    template < class X, class Y >
974    FastDelegate1(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) {
975        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
976    template < class X, class Y >
977    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) {
978        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
979    // Static functions. We convert them into a member function call.
980    // This constructor also provides implicit conversion
981    FastDelegate1(DesiredRetType (*function_to_bind)(Param1 p1) ) {
982        bind(function_to_bind); }
983    // for efficiency, prevent creation of a temporary
984    void operator = (DesiredRetType (*function_to_bind)(Param1 p1) ) {
985        bind(function_to_bind); }
986    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1)) {
987        m_Closure.bindstaticfunc(this, &FastDelegate1::InvokeStaticFunction,
988            function_to_bind); }
989    // Invoke the delegate
990    RetType operator() (Param1 p1) const {
991    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1); }
992    // Implicit conversion to "bool" using the safe_bool idiom
993private:
994    typedef struct SafeBoolStruct {
995        int a_data_pointer_to_this_is_0_on_buggy_compilers;
996        StaticFunctionPtr m_nonzero;
997    } UselessTypedef;
998    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
999public:
1000    operator unspecified_bool_type() const {
1001        return empty()? 0: &SafeBoolStruct::m_nonzero;
1002    }
1003    // necessary to allow ==0 to work despite the safe_bool idiom
1004    inline bool operator==(StaticFunctionPtr funcptr) {
1005        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1006    inline bool operator!=(StaticFunctionPtr funcptr) {
1007        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1008    inline bool operator ! () const { // Is it bound to anything?
1009            return !m_Closure; }
1010    inline bool empty() const {
1011            return !m_Closure; }
1012    void clear() { m_Closure.clear();}
1013    // Conversion to and from the DelegateMemento storage class
1014    const DelegateMemento & GetMemento() { return m_Closure; }
1015    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1016
1017private: // Invoker for static functions
1018    RetType InvokeStaticFunction(Param1 p1) const {
1019    return (*(m_Closure.GetStaticFunction()))(p1); }
1020};
1021
1022//N=2
1023template<class Param1, class Param2, class RetType=detail::DefaultVoid>
1024class FastDelegate2 {
1025private:
1026    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1027    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2);
1028    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2);
1029    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2);
1030    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1031    ClosureType m_Closure;
1032public:
1033    // Typedefs to aid generic programming
1034    typedef FastDelegate2 type;
1035
1036    // Construction and comparison functions
1037    FastDelegate2() { clear(); }
1038    FastDelegate2(const FastDelegate2 &x) {
1039        m_Closure.CopyFrom(this, x.m_Closure); }
1040    void operator = (const FastDelegate2 &x) {
1041        m_Closure.CopyFrom(this, x.m_Closure); }
1042    bool operator ==(const FastDelegate2 &x) const {
1043        return m_Closure.IsEqual(x.m_Closure); }
1044    bool operator !=(const FastDelegate2 &x) const {
1045        return !m_Closure.IsEqual(x.m_Closure); }
1046    bool operator <(const FastDelegate2 &x) const {
1047        return m_Closure.IsLess(x.m_Closure); }
1048    bool operator >(const FastDelegate2 &x) const {
1049        return x.m_Closure.IsLess(m_Closure); }
1050    // Binding to non-const member functions
1051    template < class X, class Y >
1052    FastDelegate2(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) ) {
1053        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1054    template < class X, class Y >
1055    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2)) {
1056        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1057    // Binding to const member functions.
1058    template < class X, class Y >
1059    FastDelegate2(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) {
1060        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1061    template < class X, class Y >
1062    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) {
1063        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1064    // Static functions. We convert them into a member function call.
1065    // This constructor also provides implicit conversion
1066    FastDelegate2(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) {
1067        bind(function_to_bind); }
1068    // for efficiency, prevent creation of a temporary
1069    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) {
1070        bind(function_to_bind); }
1071    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2)) {
1072        m_Closure.bindstaticfunc(this, &FastDelegate2::InvokeStaticFunction,
1073            function_to_bind); }
1074    // Invoke the delegate
1075    RetType operator() (Param1 p1, Param2 p2) const {
1076    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2); }
1077    // Implicit conversion to "bool" using the safe_bool idiom
1078private:
1079    typedef struct SafeBoolStruct {
1080        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1081        StaticFunctionPtr m_nonzero;
1082    } UselessTypedef;
1083    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1084public:
1085    operator unspecified_bool_type() const {
1086        return empty()? 0: &SafeBoolStruct::m_nonzero;
1087    }
1088    // necessary to allow ==0 to work despite the safe_bool idiom
1089    inline bool operator==(StaticFunctionPtr funcptr) {
1090        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1091    inline bool operator!=(StaticFunctionPtr funcptr) {
1092        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1093    inline bool operator ! () const { // Is it bound to anything?
1094            return !m_Closure; }
1095    inline bool empty() const {
1096            return !m_Closure; }
1097    void clear() { m_Closure.clear();}
1098    // Conversion to and from the DelegateMemento storage class
1099    const DelegateMemento & GetMemento() { return m_Closure; }
1100    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1101
1102private: // Invoker for static functions
1103    RetType InvokeStaticFunction(Param1 p1, Param2 p2) const {
1104    return (*(m_Closure.GetStaticFunction()))(p1, p2); }
1105};
1106
1107//N=3
1108template<class Param1, class Param2, class Param3, class RetType=detail::DefaultVoid>
1109class FastDelegate3 {
1110private:
1111    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1112    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
1113    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
1114    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3);
1115    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1116    ClosureType m_Closure;
1117public:
1118    // Typedefs to aid generic programming
1119    typedef FastDelegate3 type;
1120
1121    // Construction and comparison functions
1122    FastDelegate3() { clear(); }
1123    FastDelegate3(const FastDelegate3 &x) {
1124        m_Closure.CopyFrom(this, x.m_Closure); }
1125    void operator = (const FastDelegate3 &x) {
1126        m_Closure.CopyFrom(this, x.m_Closure); }
1127    bool operator ==(const FastDelegate3 &x) const {
1128        return m_Closure.IsEqual(x.m_Closure); }
1129    bool operator !=(const FastDelegate3 &x) const {
1130        return !m_Closure.IsEqual(x.m_Closure); }
1131    bool operator <(const FastDelegate3 &x) const {
1132        return m_Closure.IsLess(x.m_Closure); }
1133    bool operator >(const FastDelegate3 &x) const {
1134        return x.m_Closure.IsLess(m_Closure); }
1135    // Binding to non-const member functions
1136    template < class X, class Y >
1137    FastDelegate3(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
1138        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1139    template < class X, class Y >
1140    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
1141        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1142    // Binding to const member functions.
1143    template < class X, class Y >
1144    FastDelegate3(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
1145        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1146    template < class X, class Y >
1147    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
1148        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1149    // Static functions. We convert them into a member function call.
1150    // This constructor also provides implicit conversion
1151    FastDelegate3(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
1152        bind(function_to_bind); }
1153    // for efficiency, prevent creation of a temporary
1154    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
1155        bind(function_to_bind); }
1156    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
1157        m_Closure.bindstaticfunc(this, &FastDelegate3::InvokeStaticFunction,
1158            function_to_bind); }
1159    // Invoke the delegate
1160    RetType operator() (Param1 p1, Param2 p2, Param3 p3) const {
1161    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3); }
1162    // Implicit conversion to "bool" using the safe_bool idiom
1163private:
1164    typedef struct SafeBoolStruct {
1165        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1166        StaticFunctionPtr m_nonzero;
1167    } UselessTypedef;
1168    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1169public:
1170    operator unspecified_bool_type() const {
1171        return empty()? 0: &SafeBoolStruct::m_nonzero;
1172    }
1173    // necessary to allow ==0 to work despite the safe_bool idiom
1174    inline bool operator==(StaticFunctionPtr funcptr) {
1175        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1176    inline bool operator!=(StaticFunctionPtr funcptr) {
1177        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1178    inline bool operator ! () const { // Is it bound to anything?
1179            return !m_Closure; }
1180    inline bool empty() const {
1181            return !m_Closure; }
1182    void clear() { m_Closure.clear();}
1183    // Conversion to and from the DelegateMemento storage class
1184    const DelegateMemento & GetMemento() { return m_Closure; }
1185    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1186
1187private: // Invoker for static functions
1188    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3) const {
1189    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3); }
1190};
1191
1192//N=4
1193template<class Param1, class Param2, class Param3, class Param4, class RetType=detail::DefaultVoid>
1194class FastDelegate4 {
1195private:
1196    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1197    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
1198    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
1199    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
1200    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1201    ClosureType m_Closure;
1202public:
1203    // Typedefs to aid generic programming
1204    typedef FastDelegate4 type;
1205
1206    // Construction and comparison functions
1207    FastDelegate4() { clear(); }
1208    FastDelegate4(const FastDelegate4 &x) {
1209        m_Closure.CopyFrom(this, x.m_Closure); }
1210    void operator = (const FastDelegate4 &x) {
1211        m_Closure.CopyFrom(this, x.m_Closure); }
1212    bool operator ==(const FastDelegate4 &x) const {
1213        return m_Closure.IsEqual(x.m_Closure); }
1214    bool operator !=(const FastDelegate4 &x) const {
1215        return !m_Closure.IsEqual(x.m_Closure); }
1216    bool operator <(const FastDelegate4 &x) const {
1217        return m_Closure.IsLess(x.m_Closure); }
1218    bool operator >(const FastDelegate4 &x) const {
1219        return x.m_Closure.IsLess(m_Closure); }
1220    // Binding to non-const member functions
1221    template < class X, class Y >
1222    FastDelegate4(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
1223        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1224    template < class X, class Y >
1225    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
1226        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1227    // Binding to const member functions.
1228    template < class X, class Y >
1229    FastDelegate4(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
1230        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1231    template < class X, class Y >
1232    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
1233        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1234    // Static functions. We convert them into a member function call.
1235    // This constructor also provides implicit conversion
1236    FastDelegate4(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
1237        bind(function_to_bind); }
1238    // for efficiency, prevent creation of a temporary
1239    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
1240        bind(function_to_bind); }
1241    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
1242        m_Closure.bindstaticfunc(this, &FastDelegate4::InvokeStaticFunction,
1243            function_to_bind); }
1244    // Invoke the delegate
1245    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
1246    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4); }
1247    // Implicit conversion to "bool" using the safe_bool idiom
1248private:
1249    typedef struct SafeBoolStruct {
1250        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1251        StaticFunctionPtr m_nonzero;
1252    } UselessTypedef;
1253    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1254public:
1255    operator unspecified_bool_type() const {
1256        return empty()? 0: &SafeBoolStruct::m_nonzero;
1257    }
1258    // necessary to allow ==0 to work despite the safe_bool idiom
1259    inline bool operator==(StaticFunctionPtr funcptr) {
1260        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1261    inline bool operator!=(StaticFunctionPtr funcptr) {
1262        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1263    inline bool operator ! () const { // Is it bound to anything?
1264            return !m_Closure; }
1265    inline bool empty() const {
1266            return !m_Closure; }
1267    void clear() { m_Closure.clear();}
1268    // Conversion to and from the DelegateMemento storage class
1269    const DelegateMemento & GetMemento() { return m_Closure; }
1270    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1271
1272private: // Invoker for static functions
1273    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
1274    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4); }
1275};
1276
1277//N=5
1278template<class Param1, class Param2, class Param3, class Param4, class Param5, class RetType=detail::DefaultVoid>
1279class FastDelegate5 {
1280private:
1281    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1282    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
1283    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
1284    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
1285    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1286    ClosureType m_Closure;
1287public:
1288    // Typedefs to aid generic programming
1289    typedef FastDelegate5 type;
1290
1291    // Construction and comparison functions
1292    FastDelegate5() { clear(); }
1293    FastDelegate5(const FastDelegate5 &x) {
1294        m_Closure.CopyFrom(this, x.m_Closure); }
1295    void operator = (const FastDelegate5 &x) {
1296        m_Closure.CopyFrom(this, x.m_Closure); }
1297    bool operator ==(const FastDelegate5 &x) const {
1298        return m_Closure.IsEqual(x.m_Closure); }
1299    bool operator !=(const FastDelegate5 &x) const {
1300        return !m_Closure.IsEqual(x.m_Closure); }
1301    bool operator <(const FastDelegate5 &x) const {
1302        return m_Closure.IsLess(x.m_Closure); }
1303    bool operator >(const FastDelegate5 &x) const {
1304        return x.m_Closure.IsLess(m_Closure); }
1305    // Binding to non-const member functions
1306    template < class X, class Y >
1307    FastDelegate5(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
1308        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1309    template < class X, class Y >
1310    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
1311        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1312    // Binding to const member functions.
1313    template < class X, class Y >
1314    FastDelegate5(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
1315        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1316    template < class X, class Y >
1317    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
1318        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1319    // Static functions. We convert them into a member function call.
1320    // This constructor also provides implicit conversion
1321    FastDelegate5(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
1322        bind(function_to_bind); }
1323    // for efficiency, prevent creation of a temporary
1324    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
1325        bind(function_to_bind); }
1326    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
1327        m_Closure.bindstaticfunc(this, &FastDelegate5::InvokeStaticFunction,
1328            function_to_bind); }
1329    // Invoke the delegate
1330    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
1331    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5); }
1332    // Implicit conversion to "bool" using the safe_bool idiom
1333private:
1334    typedef struct SafeBoolStruct {
1335        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1336        StaticFunctionPtr m_nonzero;
1337    } UselessTypedef;
1338    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1339public:
1340    operator unspecified_bool_type() const {
1341        return empty()? 0: &SafeBoolStruct::m_nonzero;
1342    }
1343    // necessary to allow ==0 to work despite the safe_bool idiom
1344    inline bool operator==(StaticFunctionPtr funcptr) {
1345        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1346    inline bool operator!=(StaticFunctionPtr funcptr) {
1347        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1348    inline bool operator ! () const { // Is it bound to anything?
1349            return !m_Closure; }
1350    inline bool empty() const {
1351            return !m_Closure; }
1352    void clear() { m_Closure.clear();}
1353    // Conversion to and from the DelegateMemento storage class
1354    const DelegateMemento & GetMemento() { return m_Closure; }
1355    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1356
1357private: // Invoker for static functions
1358    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
1359    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5); }
1360};
1361
1362//N=6
1363template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType=detail::DefaultVoid>
1364class FastDelegate6 {
1365private:
1366    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1367    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
1368    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
1369    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
1370    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1371    ClosureType m_Closure;
1372public:
1373    // Typedefs to aid generic programming
1374    typedef FastDelegate6 type;
1375
1376    // Construction and comparison functions
1377    FastDelegate6() { clear(); }
1378    FastDelegate6(const FastDelegate6 &x) {
1379        m_Closure.CopyFrom(this, x.m_Closure); }
1380    void operator = (const FastDelegate6 &x) {
1381        m_Closure.CopyFrom(this, x.m_Closure); }
1382    bool operator ==(const FastDelegate6 &x) const {
1383        return m_Closure.IsEqual(x.m_Closure); }
1384    bool operator !=(const FastDelegate6 &x) const {
1385        return !m_Closure.IsEqual(x.m_Closure); }
1386    bool operator <(const FastDelegate6 &x) const {
1387        return m_Closure.IsLess(x.m_Closure); }
1388    bool operator >(const FastDelegate6 &x) const {
1389        return x.m_Closure.IsLess(m_Closure); }
1390    // Binding to non-const member functions
1391    template < class X, class Y >
1392    FastDelegate6(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
1393        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1394    template < class X, class Y >
1395    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
1396        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1397    // Binding to const member functions.
1398    template < class X, class Y >
1399    FastDelegate6(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
1400        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1401    template < class X, class Y >
1402    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
1403        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1404    // Static functions. We convert them into a member function call.
1405    // This constructor also provides implicit conversion
1406    FastDelegate6(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
1407        bind(function_to_bind); }
1408    // for efficiency, prevent creation of a temporary
1409    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
1410        bind(function_to_bind); }
1411    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
1412        m_Closure.bindstaticfunc(this, &FastDelegate6::InvokeStaticFunction,
1413            function_to_bind); }
1414    // Invoke the delegate
1415    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
1416    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6); }
1417    // Implicit conversion to "bool" using the safe_bool idiom
1418private:
1419    typedef struct SafeBoolStruct {
1420        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1421        StaticFunctionPtr m_nonzero;
1422    } UselessTypedef;
1423    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1424public:
1425    operator unspecified_bool_type() const {
1426        return empty()? 0: &SafeBoolStruct::m_nonzero;
1427    }
1428    // necessary to allow ==0 to work despite the safe_bool idiom
1429    inline bool operator==(StaticFunctionPtr funcptr) {
1430        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1431    inline bool operator!=(StaticFunctionPtr funcptr) {
1432        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1433    inline bool operator ! () const { // Is it bound to anything?
1434            return !m_Closure; }
1435    inline bool empty() const {
1436            return !m_Closure; }
1437    void clear() { m_Closure.clear();}
1438    // Conversion to and from the DelegateMemento storage class
1439    const DelegateMemento & GetMemento() { return m_Closure; }
1440    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1441
1442private: // Invoker for static functions
1443    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
1444    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6); }
1445};
1446
1447//N=7
1448template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType=detail::DefaultVoid>
1449class FastDelegate7 {
1450private:
1451    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1452    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
1453    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
1454    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
1455    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1456    ClosureType m_Closure;
1457public:
1458    // Typedefs to aid generic programming
1459    typedef FastDelegate7 type;
1460
1461    // Construction and comparison functions
1462    FastDelegate7() { clear(); }
1463    FastDelegate7(const FastDelegate7 &x) {
1464        m_Closure.CopyFrom(this, x.m_Closure); }
1465    void operator = (const FastDelegate7 &x) {
1466        m_Closure.CopyFrom(this, x.m_Closure); }
1467    bool operator ==(const FastDelegate7 &x) const {
1468        return m_Closure.IsEqual(x.m_Closure); }
1469    bool operator !=(const FastDelegate7 &x) const {
1470        return !m_Closure.IsEqual(x.m_Closure); }
1471    bool operator <(const FastDelegate7 &x) const {
1472        return m_Closure.IsLess(x.m_Closure); }
1473    bool operator >(const FastDelegate7 &x) const {
1474        return x.m_Closure.IsLess(m_Closure); }
1475    // Binding to non-const member functions
1476    template < class X, class Y >
1477    FastDelegate7(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
1478        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1479    template < class X, class Y >
1480    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
1481        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1482    // Binding to const member functions.
1483    template < class X, class Y >
1484    FastDelegate7(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
1485        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1486    template < class X, class Y >
1487    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
1488        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1489    // Static functions. We convert them into a member function call.
1490    // This constructor also provides implicit conversion
1491    FastDelegate7(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
1492        bind(function_to_bind); }
1493    // for efficiency, prevent creation of a temporary
1494    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
1495        bind(function_to_bind); }
1496    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
1497        m_Closure.bindstaticfunc(this, &FastDelegate7::InvokeStaticFunction,
1498            function_to_bind); }
1499    // Invoke the delegate
1500    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
1501    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7); }
1502    // Implicit conversion to "bool" using the safe_bool idiom
1503private:
1504    typedef struct SafeBoolStruct {
1505        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1506        StaticFunctionPtr m_nonzero;
1507    } UselessTypedef;
1508    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1509public:
1510    operator unspecified_bool_type() const {
1511        return empty()? 0: &SafeBoolStruct::m_nonzero;
1512    }
1513    // necessary to allow ==0 to work despite the safe_bool idiom
1514    inline bool operator==(StaticFunctionPtr funcptr) {
1515        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1516    inline bool operator!=(StaticFunctionPtr funcptr) {
1517        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1518    inline bool operator ! () const { // Is it bound to anything?
1519            return !m_Closure; }
1520    inline bool empty() const {
1521            return !m_Closure; }
1522    void clear() { m_Closure.clear();}
1523    // Conversion to and from the DelegateMemento storage class
1524    const DelegateMemento & GetMemento() { return m_Closure; }
1525    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1526
1527private: // Invoker for static functions
1528    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
1529    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7); }
1530};
1531
1532//N=8
1533template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType=detail::DefaultVoid>
1534class FastDelegate8 {
1535private:
1536    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1537    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
1538    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
1539    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
1540    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1541    ClosureType m_Closure;
1542public:
1543    // Typedefs to aid generic programming
1544    typedef FastDelegate8 type;
1545
1546    // Construction and comparison functions
1547    FastDelegate8() { clear(); }
1548    FastDelegate8(const FastDelegate8 &x) {
1549        m_Closure.CopyFrom(this, x.m_Closure); }
1550    void operator = (const FastDelegate8 &x) {
1551        m_Closure.CopyFrom(this, x.m_Closure); }
1552    bool operator ==(const FastDelegate8 &x) const {
1553        return m_Closure.IsEqual(x.m_Closure); }
1554    bool operator !=(const FastDelegate8 &x) const {
1555        return !m_Closure.IsEqual(x.m_Closure); }
1556    bool operator <(const FastDelegate8 &x) const {
1557        return m_Closure.IsLess(x.m_Closure); }
1558    bool operator >(const FastDelegate8 &x) const {
1559        return x.m_Closure.IsLess(m_Closure); }
1560    // Binding to non-const member functions
1561    template < class X, class Y >
1562    FastDelegate8(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) {
1563        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1564    template < class X, class Y >
1565    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
1566        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1567    // Binding to const member functions.
1568    template < class X, class Y >
1569    FastDelegate8(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const) {
1570        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1571    template < class X, class Y >
1572    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const) {
1573        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1574    // Static functions. We convert them into a member function call.
1575    // This constructor also provides implicit conversion
1576    FastDelegate8(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) {
1577        bind(function_to_bind); }
1578    // for efficiency, prevent creation of a temporary
1579    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) {
1580        bind(function_to_bind); }
1581    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
1582        m_Closure.bindstaticfunc(this, &FastDelegate8::InvokeStaticFunction,
1583            function_to_bind); }
1584    // Invoke the delegate
1585    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const {
1586    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7, p8); }
1587    // Implicit conversion to "bool" using the safe_bool idiom
1588private:
1589    typedef struct SafeBoolStruct {
1590        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1591        StaticFunctionPtr m_nonzero;
1592    } UselessTypedef;
1593    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1594public:
1595    operator unspecified_bool_type() const {
1596        return empty()? 0: &SafeBoolStruct::m_nonzero;
1597    }
1598    // necessary to allow ==0 to work despite the safe_bool idiom
1599    inline bool operator==(StaticFunctionPtr funcptr) {
1600        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1601    inline bool operator!=(StaticFunctionPtr funcptr) {
1602        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1603    inline bool operator ! () const { // Is it bound to anything?
1604            return !m_Closure; }
1605    inline bool empty() const {
1606            return !m_Closure; }
1607    void clear() { m_Closure.clear();}
1608    // Conversion to and from the DelegateMemento storage class
1609    const DelegateMemento & GetMemento() { return m_Closure; }
1610    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1611
1612private: // Invoker for static functions
1613    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const {
1614    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7, p8); }
1615};
1616
1617
1618////////////////////////////////////////////////////////////////////////////////
1619// Fast Delegates, part 4:
1620//
1621// FastDelegate<> class (Original author: Jody Hagins)
1622// Allows boost::function style syntax like:
1623// FastDelegate< double (int, long) >
1624// instead of:
1625// FastDelegate2< int, long, double >
1626//
1627////////////////////////////////////////////////////////////////////////////////
1628
1629#ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
1630
1631// Declare FastDelegate as a class template. It will be specialized
1632// later for all number of arguments.
1633template <typename Signature>
1634class FastDelegate;
1635
1636//N=0
1637// Specialization to allow use of
1638// FastDelegate< R ( ) >
1639// instead of
1640// FastDelegate0 < R >
1641template<typename R>
1642class FastDelegate< R ( ) >
1643  // Inherit from FastDelegate0 so that it can be treated just like a FastDelegate0
1644  : public FastDelegate0 < R >
1645{
1646public:
1647  // Make using the base type a bit easier via typedef.
1648  typedef FastDelegate0 < R > BaseType;
1649
1650  // Allow users access to the specific type of this delegate.
1651  typedef FastDelegate SelfType;
1652
1653  // Mimic the base class constructors.
1654  FastDelegate() : BaseType() { }
1655
1656  template < class X, class Y >
1657  FastDelegate(Y * pthis,
1658    R (X::* function_to_bind)( ))
1659    : BaseType(pthis, function_to_bind) { }
1660
1661  template < class X, class Y >
1662  FastDelegate(const Y *pthis,
1663      R (X::* function_to_bind)( ) const)
1664    : BaseType(pthis, function_to_bind)
1665  { }
1666
1667  FastDelegate(R (*function_to_bind)( ))
1668    : BaseType(function_to_bind) { }
1669  void operator = (const BaseType &x) {
1670        *static_cast<BaseType*>(this) = x; }
1671};
1672
1673//N=1
1674// Specialization to allow use of
1675// FastDelegate< R ( Param1 ) >
1676// instead of
1677// FastDelegate1 < Param1, R >
1678template<typename R, class Param1>
1679class FastDelegate< R ( Param1 ) >
1680  // Inherit from FastDelegate1 so that it can be treated just like a FastDelegate1
1681  : public FastDelegate1 < Param1, R >
1682{
1683public:
1684  // Make using the base type a bit easier via typedef.
1685  typedef FastDelegate1 < Param1, R > BaseType;
1686
1687  // Allow users access to the specific type of this delegate.
1688  typedef FastDelegate SelfType;
1689
1690  // Mimic the base class constructors.
1691  FastDelegate() : BaseType() { }
1692
1693  template < class X, class Y >
1694  FastDelegate(Y * pthis,
1695    R (X::* function_to_bind)( Param1 p1 ))
1696    : BaseType(pthis, function_to_bind) { }
1697
1698  template < class X, class Y >
1699  FastDelegate(const Y *pthis,
1700      R (X::* function_to_bind)( Param1 p1 ) const)
1701    : BaseType(pthis, function_to_bind)
1702  { }
1703
1704  FastDelegate(R (*function_to_bind)( Param1 p1 ))
1705    : BaseType(function_to_bind) { }
1706  void operator = (const BaseType &x) {
1707        *static_cast<BaseType*>(this) = x; }
1708};
1709
1710//N=2
1711// Specialization to allow use of
1712// FastDelegate< R ( Param1, Param2 ) >
1713// instead of
1714// FastDelegate2 < Param1, Param2, R >
1715template<typename R, class Param1, class Param2>
1716class FastDelegate< R ( Param1, Param2 ) >
1717  // Inherit from FastDelegate2 so that it can be treated just like a FastDelegate2
1718  : public FastDelegate2 < Param1, Param2, R >
1719{
1720public:
1721  // Make using the base type a bit easier via typedef.
1722  typedef FastDelegate2 < Param1, Param2, R > BaseType;
1723
1724  // Allow users access to the specific type of this delegate.
1725  typedef FastDelegate SelfType;
1726
1727  // Mimic the base class constructors.
1728  FastDelegate() : BaseType() { }
1729
1730  template < class X, class Y >
1731  FastDelegate(Y * pthis,
1732    R (X::* function_to_bind)( Param1 p1, Param2 p2 ))
1733    : BaseType(pthis, function_to_bind) { }
1734
1735  template < class X, class Y >
1736  FastDelegate(const Y *pthis,
1737      R (X::* function_to_bind)( Param1 p1, Param2 p2 ) const)
1738    : BaseType(pthis, function_to_bind)
1739  { }
1740
1741  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2 ))
1742    : BaseType(function_to_bind) { }
1743  void operator = (const BaseType &x) {
1744        *static_cast<BaseType*>(this) = x; }
1745};
1746
1747//N=3
1748// Specialization to allow use of
1749// FastDelegate< R ( Param1, Param2, Param3 ) >
1750// instead of
1751// FastDelegate3 < Param1, Param2, Param3, R >
1752template<typename R, class Param1, class Param2, class Param3>
1753class FastDelegate< R ( Param1, Param2, Param3 ) >
1754  // Inherit from FastDelegate3 so that it can be treated just like a FastDelegate3
1755  : public FastDelegate3 < Param1, Param2, Param3, R >
1756{
1757public:
1758  // Make using the base type a bit easier via typedef.
1759  typedef FastDelegate3 < Param1, Param2, Param3, R > BaseType;
1760
1761  // Allow users access to the specific type of this delegate.
1762  typedef FastDelegate SelfType;
1763
1764  // Mimic the base class constructors.
1765  FastDelegate() : BaseType() { }
1766
1767  template < class X, class Y >
1768  FastDelegate(Y * pthis,
1769    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ))
1770    : BaseType(pthis, function_to_bind) { }
1771
1772  template < class X, class Y >
1773  FastDelegate(const Y *pthis,
1774      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ) const)
1775    : BaseType(pthis, function_to_bind)
1776  { }
1777
1778  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ))
1779    : BaseType(function_to_bind) { }
1780  void operator = (const BaseType &x) {
1781        *static_cast<BaseType*>(this) = x; }
1782};
1783
1784//N=4
1785// Specialization to allow use of
1786// FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
1787// instead of
1788// FastDelegate4 < Param1, Param2, Param3, Param4, R >
1789template<typename R, class Param1, class Param2, class Param3, class Param4>
1790class FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
1791  // Inherit from FastDelegate4 so that it can be treated just like a FastDelegate4
1792  : public FastDelegate4 < Param1, Param2, Param3, Param4, R >
1793{
1794public:
1795  // Make using the base type a bit easier via typedef.
1796  typedef FastDelegate4 < Param1, Param2, Param3, Param4, R > BaseType;
1797
1798  // Allow users access to the specific type of this delegate.
1799  typedef FastDelegate SelfType;
1800
1801  // Mimic the base class constructors.
1802  FastDelegate() : BaseType() { }
1803
1804  template < class X, class Y >
1805  FastDelegate(Y * pthis,
1806    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ))
1807    : BaseType(pthis, function_to_bind) { }
1808
1809  template < class X, class Y >
1810  FastDelegate(const Y *pthis,
1811      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) const)
1812    : BaseType(pthis, function_to_bind)
1813  { }
1814
1815  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ))
1816    : BaseType(function_to_bind) { }
1817  void operator = (const BaseType &x) {
1818        *static_cast<BaseType*>(this) = x; }
1819};
1820
1821//N=5
1822// Specialization to allow use of
1823// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
1824// instead of
1825// FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
1826template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5>
1827class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
1828  // Inherit from FastDelegate5 so that it can be treated just like a FastDelegate5
1829  : public FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
1830{
1831public:
1832  // Make using the base type a bit easier via typedef.
1833  typedef FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > BaseType;
1834
1835  // Allow users access to the specific type of this delegate.
1836  typedef FastDelegate SelfType;
1837
1838  // Mimic the base class constructors.
1839  FastDelegate() : BaseType() { }
1840
1841  template < class X, class Y >
1842  FastDelegate(Y * pthis,
1843    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ))
1844    : BaseType(pthis, function_to_bind) { }
1845
1846  template < class X, class Y >
1847  FastDelegate(const Y *pthis,
1848      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) const)
1849    : BaseType(pthis, function_to_bind)
1850  { }
1851
1852  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ))
1853    : BaseType(function_to_bind) { }
1854  void operator = (const BaseType &x) {
1855        *static_cast<BaseType*>(this) = x; }
1856};
1857
1858//N=6
1859// Specialization to allow use of
1860// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
1861// instead of
1862// FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
1863template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6>
1864class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
1865  // Inherit from FastDelegate6 so that it can be treated just like a FastDelegate6
1866  : public FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
1867{
1868public:
1869  // Make using the base type a bit easier via typedef.
1870  typedef FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > BaseType;
1871
1872  // Allow users access to the specific type of this delegate.
1873  typedef FastDelegate SelfType;
1874
1875  // Mimic the base class constructors.
1876  FastDelegate() : BaseType() { }
1877
1878  template < class X, class Y >
1879  FastDelegate(Y * pthis,
1880    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ))
1881    : BaseType(pthis, function_to_bind) { }
1882
1883  template < class X, class Y >
1884  FastDelegate(const Y *pthis,
1885      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) const)
1886    : BaseType(pthis, function_to_bind)
1887  { }
1888
1889  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ))
1890    : BaseType(function_to_bind) { }
1891  void operator = (const BaseType &x) {
1892        *static_cast<BaseType*>(this) = x; }
1893};
1894
1895//N=7
1896// Specialization to allow use of
1897// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
1898// instead of
1899// FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
1900template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7>
1901class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
1902  // Inherit from FastDelegate7 so that it can be treated just like a FastDelegate7
1903  : public FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
1904{
1905public:
1906  // Make using the base type a bit easier via typedef.
1907  typedef FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > BaseType;
1908
1909  // Allow users access to the specific type of this delegate.
1910  typedef FastDelegate SelfType;
1911
1912  // Mimic the base class constructors.
1913  FastDelegate() : BaseType() { }
1914
1915  template < class X, class Y >
1916  FastDelegate(Y * pthis,
1917    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ))
1918    : BaseType(pthis, function_to_bind) { }
1919
1920  template < class X, class Y >
1921  FastDelegate(const Y *pthis,
1922      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) const)
1923    : BaseType(pthis, function_to_bind)
1924  { }
1925
1926  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ))
1927    : BaseType(function_to_bind) { }
1928  void operator = (const BaseType &x) {
1929        *static_cast<BaseType*>(this) = x; }
1930};
1931
1932//N=8
1933// Specialization to allow use of
1934// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) >
1935// instead of
1936// FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R >
1937template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8>
1938class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) >
1939  // Inherit from FastDelegate8 so that it can be treated just like a FastDelegate8
1940  : public FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R >
1941{
1942public:
1943  // Make using the base type a bit easier via typedef.
1944  typedef FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R > BaseType;
1945
1946  // Allow users access to the specific type of this delegate.
1947  typedef FastDelegate SelfType;
1948
1949  // Mimic the base class constructors.
1950  FastDelegate() : BaseType() { }
1951
1952  template < class X, class Y >
1953  FastDelegate(Y * pthis,
1954    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ))
1955    : BaseType(pthis, function_to_bind) { }
1956
1957  template < class X, class Y >
1958  FastDelegate(const Y *pthis,
1959      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) const)
1960    : BaseType(pthis, function_to_bind)
1961  { }
1962
1963  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ))
1964    : BaseType(function_to_bind) { }
1965  void operator = (const BaseType &x) {
1966        *static_cast<BaseType*>(this) = x; }
1967};
1968
1969
1970#endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
1971
1972////////////////////////////////////////////////////////////////////////////////
1973// Fast Delegates, part 5:
1974//
1975// MakeDelegate() helper function
1976//
1977// MakeDelegate(&x, &X::func) returns a fastdelegate of the type
1978// necessary for calling x.func() with the correct number of arguments.
1979// This makes it possible to eliminate many typedefs from user code.
1980//
1981////////////////////////////////////////////////////////////////////////////////
1982
1983// Also declare overloads of a MakeDelegate() global function to
1984// reduce the need for typedefs.
1985// We need seperate overloads for const and non-const member functions.
1986// Also, because of the weird rule about the class of derived member function pointers,
1987// implicit downcasts may need to be applied later to the 'this' pointer.
1988// That's why two classes (X and Y) appear in the definitions. Y must be implicitly
1989// castable to X.
1990
1991// Workaround for VC6. VC6 needs void return types converted into DefaultVoid.
1992// GCC 3.2 and later won't compile this unless it's preceded by 'typename',
1993// but VC6 doesn't allow 'typename' in this context.
1994// So, I have to use a macro.
1995
1996#ifdef FASTDLGT_VC6
1997#define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type
1998#else
1999#define FASTDLGT_RETTYPE RetType
2000#endif
2001
2002//N=0
2003template <class X, class Y, class RetType>
2004FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)()) {
2005    return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
2006}
2007
2008template <class X, class Y, class RetType>
2009FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)() const) {
2010    return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
2011}
2012
2013//N=1
2014template <class X, class Y, class Param1, class RetType>
2015FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1)) {
2016    return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
2017}
2018
2019template <class X, class Y, class Param1, class RetType>
2020FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1) const) {
2021    return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
2022}
2023
2024//N=2
2025template <class X, class Y, class Param1, class Param2, class RetType>
2026FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2)) {
2027    return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
2028}
2029
2030template <class X, class Y, class Param1, class Param2, class RetType>
2031FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2) const) {
2032    return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
2033}
2034
2035//N=3
2036template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
2037FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3)) {
2038    return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
2039}
2040
2041template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
2042FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3) const) {
2043    return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
2044}
2045
2046//N=4
2047template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
2048FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
2049    return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
2050}
2051
2052template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
2053FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
2054    return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
2055}
2056
2057//N=5
2058template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
2059FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
2060    return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
2061}
2062
2063template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
2064FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
2065    return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
2066}
2067
2068//N=6
2069template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
2070FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
2071    return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
2072}
2073
2074template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
2075FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
2076    return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
2077}
2078
2079//N=7
2080template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
2081FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
2082    return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
2083}
2084
2085template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
2086FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
2087    return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
2088}
2089
2090//N=8
2091template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType>
2092FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
2093    return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func);
2094}
2095
2096template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType>
2097FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const) {
2098    return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func);
2099}
2100
2101
2102 // clean up after ourselves...
2103#undef FASTDLGT_RETTYPE
2104
2105} // namespace fastdelegate
2106
2107#endif // !defined(FASTDELEGATE_H)
2108
1// FastDelegate.h
2// Efficient delegates in C++ that generate only two lines of asm code!
3// Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp
4//
5// - Don Clugston, Mar 2004.
6// Major contributions were made by Jody Hagins.
7// History:
8// 24-Apr-04 1.0 * Submitted to CodeProject.
9// 28-Apr-04 1.1 * Prevent most unsafe uses of evil static function hack.
10// * Improved syntax for horrible_cast (thanks Paul Bludov).
11// * Tested on Metrowerks MWCC and Intel ICL (IA32)
12// * Compiled, but not run, on Comeau C++ and Intel Itanium ICL.
13// 27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5
14// * Now works on /clr "managed C++" code on VC7, VC7.1
15// * Comeau C++ now compiles without warnings.
16// * Prevent the virtual inheritance case from being used on
17// VC6 and earlier, which generate incorrect code.
18// * Improved warning and error messages. Non-standard hacks
19// now have compile-time checks to make them safer.
20// * implicit_cast used instead of static_cast in many cases.
21// * If calling a const member function, a const class pointer can be used.
22// * MakeDelegate() global helper function added to simplify pass-by-value.
23// * Added fastdelegate.clear()
24// 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates)
25// 30-Oct-04 1.3 * Support for (non-void) return values.
26// * No more workarounds in client code!
27// MSVC and Intel now use a clever hack invented by John Dlugosz:
28// - The FASTDELEGATEDECLARE workaround is no longer necessary.
29// - No more warning messages for VC6
30// * Less use of macros. Error messages should be more comprehensible.
31// * Added include guards
32// * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
33// * Now tested on VS 2005 Express Beta, PGI C++
34// 24-Dec-04 1.4 * Added DelegateMemento, to allow collections of disparate delegates.
35// * <,>,<=,>= comparison operators to allow storage in ordered containers.
36// * Substantial reduction of code size, especially the 'Closure' class.
37// * Standardised all the compiler-specific workarounds.
38// * MFP conversion now works for CodePlay (but not yet supported in the full code).
39// * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1
40// * New syntax: FastDelegate< int (char *, double) >.
41// 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric).
42// * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium.
43// 30-Mar-05 1.5 * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())"
44// * Fully supported by CodePlay VectorC
45// * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC!
46// * More optimal assignment,== and != operators for static function pointers.
47
48#ifndef FASTDELEGATE_H
49#define FASTDELEGATE_H
50#if _MSC_VER > 1000
51#pragma once
52#endif // _MSC_VER > 1000
53
54#include <memory.h> // to allow <,> comparisons
55
56////////////////////////////////////////////////////////////////////////////////
57// Configuration options
58//
59////////////////////////////////////////////////////////////////////////////////
60
61// Uncomment the following #define for optimally-sized delegates.
62// In this case, the generated asm code is almost identical to the code you'd get
63// if the compiler had native support for delegates.
64// It will not work on systems where sizeof(dataptr) < sizeof(codeptr).
65// Thus, it will not work for DOS compilers using the medium model.
66// It will also probably fail on some DSP systems.
67#define FASTDELEGATE_USESTATICFUNCTIONHACK
68
69// Uncomment the next line to allow function declarator syntax.
70// It is automatically enabled for those compilers where it is known to work.
71//#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
72
73////////////////////////////////////////////////////////////////////////////////
74// Compiler identification for workarounds
75//
76////////////////////////////////////////////////////////////////////////////////
77
78// Compiler identification. It's not easy to identify Visual C++ because
79// many vendors fraudulently define Microsoft's identifiers.
80#if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__)
81#define FASTDLGT_ISMSVC
82
83#if (_MSC_VER <1300) // Many workarounds are required for VC6.
84#define FASTDLGT_VC6
85#pragma warning(disable:4786) // disable this ridiculous warning
86#endif
87
88#endif
89
90// Does the compiler uses Microsoft's member function pointer structure?
91// If so, it needs special treatment.
92// Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's
93// identifier, _MSC_VER. We need to filter Metrowerks out.
94#if defined(_MSC_VER) && !defined(__MWERKS__)
95#define FASTDLGT_MICROSOFT_MFP
96
97#if !defined(__VECTOR_C)
98// CodePlay doesn't have the __single/multi/virtual_inheritance keywords
99#define FASTDLGT_HASINHERITANCE_KEYWORDS
100#endif
101#endif
102
103// Does it allow function declarator syntax? The following compilers are known to work:
104#if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1
105#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
106#endif
107
108// Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use.
109#if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__)
110#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
111#endif
112
113// It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too.
114#if defined (__MWERKS__)
115#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
116#endif
117
118#ifdef __GNUC__ // Workaround GCC bug #8271
119    // At present, GCC doesn't recognize constness of MFPs in templates
120#define FASTDELEGATE_GCC_BUG_8271
121#endif
122
123
124
125////////////////////////////////////////////////////////////////////////////////
126// General tricks used in this code
127//
128// (a) Error messages are generated by typdefing an array of negative size to
129// generate compile-time errors.
130// (b) Warning messages on MSVC are generated by declaring unused variables, and
131// enabling the "variable XXX is never used" warning.
132// (c) Unions are used in a few compiler-specific cases to perform illegal casts.
133// (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to
134// (char *) first to ensure that the correct number of *bytes* are added.
135//
136////////////////////////////////////////////////////////////////////////////////
137// Helper templates
138//
139////////////////////////////////////////////////////////////////////////////////
140
141
142namespace fastdelegate {
143namespace detail { // we'll hide the implementation details in a nested namespace.
144
145// implicit_cast< >
146// I believe this was originally going to be in the C++ standard but
147// was left out by accident. It's even milder than static_cast.
148// I use it instead of static_cast<> to emphasize that I'm not doing
149// anything nasty.
150// Usage is identical to static_cast<>
151template <class OutputClass, class InputClass>
152inline OutputClass implicit_cast(InputClass input){
153    return input;
154}
155
156// horrible_cast< >
157// This is truly evil. It completely subverts C++'s type system, allowing you
158// to cast from any class to any other class. Technically, using a union
159// to perform the cast is undefined behaviour (even in C). But we can see if
160// it is OK by checking that the union is the same size as each of its members.
161// horrible_cast<> should only be used for compiler-specific workarounds.
162// Usage is identical to reinterpret_cast<>.
163
164// This union is declared outside the horrible_cast because BCC 5.5.1
165// can't inline a function with a nested class, and gives a warning.
166template <class OutputClass, class InputClass>
167union horrible_union{
168    OutputClass out;
169    InputClass in;
170};
171
172template <class OutputClass, class InputClass>
173inline OutputClass horrible_cast(const InputClass input){
174    horrible_union<OutputClass, InputClass> u;
175    // Cause a compile-time error if in, out and u are not the same size.
176    // If the compile fails here, it means the compiler has peculiar
177    // unions which would prevent the cast from working.
178    typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass)==sizeof(u)
179        && sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];
180    u.in = input;
181    return u.out;
182}
183
184////////////////////////////////////////////////////////////////////////////////
185// Workarounds
186//
187////////////////////////////////////////////////////////////////////////////////
188
189// Backwards compatibility: This macro used to be necessary in the virtual inheritance
190// case for Intel and Microsoft. Now it just forward-declares the class.
191#define FASTDELEGATEDECLARE(CLASSNAME) class CLASSNAME;
192
193// Prevent use of the static function hack with the DOS medium model.
194#ifdef __MEDIUM__
195#undef FASTDELEGATE_USESTATICFUNCTIONHACK
196#endif
197
198// DefaultVoid - a workaround for 'void' templates in VC6.
199//
200// (1) VC6 and earlier do not allow 'void' as a default template argument.
201// (2) They also doesn't allow you to return 'void' from a function.
202//
203// Workaround for (1): Declare a dummy type 'DefaultVoid' which we use
204// when we'd like to use 'void'. We convert it into 'void' and back
205// using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>.
206// Workaround for (2): On VC6, the code for calling a void function is
207// identical to the code for calling a non-void function in which the
208// return value is never used, provided the return value is returned
209// in the EAX register, rather than on the stack.
210// This is true for most fundamental types such as int, enum, void *.
211// Const void * is the safest option since it doesn't participate
212// in any automatic conversions. But on a 16-bit compiler it might
213// cause extra code to be generated, so we disable it for all compilers
214// except for VC6 (and VC5).
215#ifdef FASTDLGT_VC6
216// VC6 workaround
217typedef const void * DefaultVoid;
218#else
219// On any other compiler, just use a normal void.
220typedef void DefaultVoid;
221#endif
222
223// Translate from 'DefaultVoid' to 'void'.
224// Everything else is unchanged
225template <class T>
226struct DefaultVoidToVoid { typedef T type; };
227
228template <>
229struct DefaultVoidToVoid<DefaultVoid> { typedef void type; };
230
231// Translate from 'void' into 'DefaultVoid'
232// Everything else is unchanged
233template <class T>
234struct VoidToDefaultVoid { typedef T type; };
235
236template <>
237struct VoidToDefaultVoid<void> { typedef DefaultVoid type; };
238
239
240
241////////////////////////////////////////////////////////////////////////////////
242// Fast Delegates, part 1:
243//
244// Conversion of member function pointer to a standard form
245//
246////////////////////////////////////////////////////////////////////////////////
247
248// GenericClass is a fake class, ONLY used to provide a type.
249// It is vitally important that it is never defined, so that the compiler doesn't
250// think it can optimize the invocation. For example, Borland generates simpler
251// code if it knows the class only uses single inheritance.
252
253// Compilers using Microsoft's structure need to be treated as a special case.
254#ifdef FASTDLGT_MICROSOFT_MFP
255
256#ifdef FASTDLGT_HASINHERITANCE_KEYWORDS
257    // For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP
258    // (4 bytes), even when the /vmg option is used. Declaring an empty class
259    // would give 16 byte pointers in this case....
260    class __single_inheritance GenericClass;
261#endif
262    // ...but for Codeplay, an empty class *always* gives 4 byte pointers.
263    // If compiled with the /clr option ("managed C++"), the JIT compiler thinks
264    // it needs to load GenericClass before it can call any of its functions,
265    // (compiles OK but crashes at runtime!), so we need to declare an
266    // empty class to make it happy.
267    // Codeplay and VC4 can't cope with the unknown_inheritance case either.
268    class GenericClass {};
269#else
270    class GenericClass;
271#endif
272
273// The size of a single inheritance member function pointer.
274const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)());
275
276// SimplifyMemFunc< >::Convert()
277//
278// A template function that converts an arbitrary member function pointer into the
279// simplest possible form of member function pointer, using a supplied 'this' pointer.
280// According to the standard, this can be done legally with reinterpret_cast<>.
281// For (non-standard) compilers which use member function pointers which vary in size
282// depending on the class, we need to use knowledge of the internal structure of a
283// member function pointer, as used by the compiler. Template specialization is used
284// to distinguish between the sizes. Because some compilers don't support partial
285// template specialisation, I use full specialisation of a wrapper struct.
286
287// general case -- don't know how to convert it. Force a compile failure
288template <int N>
289struct SimplifyMemFunc {
290    template <class X, class XFuncType, class GenericMemFuncType>
291    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
292        GenericMemFuncType &bound_func) {
293        // Unsupported member function type -- force a compile failure.
294        // (it's illegal to have a array with negative size).
295        typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N-100];
296        return 0;
297    }
298};
299
300// For compilers where all member func ptrs are the same size, everything goes here.
301// For non-standard compilers, only single_inheritance classes go here.
302template <>
303struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE> {
304    template <class X, class XFuncType, class GenericMemFuncType>
305    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
306            GenericMemFuncType &bound_func) {
307#if defined __DMC__
308        // Digital Mars doesn't allow you to cast between abitrary PMF's,
309        // even though the standard says you can. The 32-bit compiler lets you
310        // static_cast through an int, but the DOS compiler doesn't.
311        bound_func = horrible_cast<GenericMemFuncType>(function_to_bind);
312#else
313        bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind);
314#endif
315        return reinterpret_cast<GenericClass *>(pthis);
316    }
317};
318
319////////////////////////////////////////////////////////////////////////////////
320// Fast Delegates, part 1b:
321//
322// Workarounds for Microsoft and Intel
323//
324////////////////////////////////////////////////////////////////////////////////
325
326
327// Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay),
328// need to be treated as a special case.
329#ifdef FASTDLGT_MICROSOFT_MFP
330
331// We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1)
332// at the start of each function for extra safety, but VC6 seems to ICE
333// intermittently if you do this inside a template.
334
335// __multiple_inheritance classes go here
336// Nasty hack for Microsoft and Intel (IA32 and Itanium)
337template<>
338struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) > {
339    template <class X, class XFuncType, class GenericMemFuncType>
340    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
341        GenericMemFuncType &bound_func) {
342        // We need to use a horrible_cast to do this conversion.
343        // In MSVC, a multiple inheritance member pointer is internally defined as:
344        union {
345            XFuncType func;
346            struct {
347                GenericMemFuncType funcaddress; // points to the actual member function
348                int delta; // #BYTES to be added to the 'this' pointer
349            }s;
350        } u;
351        // Check that the horrible_cast will work
352        typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)? 1 : -1];
353        u.func = function_to_bind;
354        bound_func = u.s.funcaddress;
355        return reinterpret_cast<GenericClass *>(reinterpret_cast<char *>(pthis) + u.s.delta);
356    }
357};
358
359// virtual inheritance is a real nuisance. It's inefficient and complicated.
360// On MSVC and Intel, there isn't enough information in the pointer itself to
361// enable conversion to a closure pointer. Earlier versions of this code didn't
362// work for all cases, and generated a compile-time error instead.
363// But a very clever hack invented by John M. Dlugosz solves this problem.
364// My code is somewhat different to his: I have no asm code, and I make no
365// assumptions about the calling convention that is used.
366
367// In VC++ and ICL, a virtual_inheritance member pointer
368// is internally defined as:
369struct MicrosoftVirtualMFP {
370    void (GenericClass::*codeptr)(); // points to the actual member function
371    int delta; // #bytes to be added to the 'this' pointer
372    int vtable_index; // or 0 if no virtual inheritance
373};
374// The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the
375// m_codeptr member is *always* called, regardless of the values of the other
376// members. (This is *not* true for other compilers, eg GCC, which obtain the
377// function address from the vtable if a virtual function is being called).
378// Dlugosz's trick is to make the codeptr point to a probe function which
379// returns the 'this' pointer that was used.
380
381// Define a generic class that uses virtual inheritance.
382// It has a trival member function that returns the value of the 'this' pointer.
383struct GenericVirtualClass : virtual public GenericClass
384{
385    typedef GenericVirtualClass * (GenericVirtualClass::*ProbePtrType)();
386    GenericVirtualClass * GetThis() { return this; }
387};
388
389// __virtual_inheritance classes go here
390template <>
391struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2*sizeof(int) >
392{
393
394    template <class X, class XFuncType, class GenericMemFuncType>
395    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
396        GenericMemFuncType &bound_func) {
397        union {
398            XFuncType func;
399            GenericClass* (X::*ProbeFunc)();
400            MicrosoftVirtualMFP s;
401        } u;
402        u.func = function_to_bind;
403        bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr);
404        union {
405            GenericVirtualClass::ProbePtrType virtfunc;
406            MicrosoftVirtualMFP s;
407        } u2;
408        // Check that the horrible_cast<>s will work
409        typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind)==sizeof(u.s)
410            && sizeof(function_to_bind)==sizeof(u.ProbeFunc)
411            && sizeof(u2.virtfunc)==sizeof(u2.s) ? 1 : -1];
412   // Unfortunately, taking the address of a MF prevents it from being inlined, so
413   // this next line can't be completely optimised away by the compiler.
414        u2.virtfunc = &GenericVirtualClass::GetThis;
415        u.s.codeptr = u2.s.codeptr;
416        return (pthis->*u.ProbeFunc)();
417    }
418};
419
420#if (_MSC_VER <1300)
421
422// Nasty hack for Microsoft Visual C++ 6.0
423// unknown_inheritance classes go here
424// There is a compiler bug in MSVC6 which generates incorrect code in this case!!
425template <>
426struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
427{
428    template <class X, class XFuncType, class GenericMemFuncType>
429    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
430        GenericMemFuncType &bound_func) {
431        // There is an apalling but obscure compiler bug in MSVC6 and earlier:
432        // vtable_index and 'vtordisp' are always set to 0 in the
433        // unknown_inheritance case!
434        // This means that an incorrect function could be called!!!
435        // Compiling with the /vmg option leads to potentially incorrect code.
436        // This is probably the reason that the IDE has a user interface for specifying
437        // the /vmg option, but it is disabled - you can only specify /vmg on
438        // the command line. In VC1.5 and earlier, the compiler would ICE if it ever
439        // encountered this situation.
440        // It is OK to use the /vmg option if /vmm or /vms is specified.
441
442        // Fortunately, the wrong function is only called in very obscure cases.
443        // It only occurs when a derived class overrides a virtual function declared
444        // in a virtual base class, and the member function
445        // points to the *Derived* version of that function. The problem can be
446        // completely averted in 100% of cases by using the *Base class* for the
447        // member fpointer. Ie, if you use the base class as an interface, you'll
448        // stay out of trouble.
449        // Occasionally, you might want to point directly to a derived class function
450        // that isn't an override of a base class. In this case, both vtable_index
451        // and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated.
452        // We can generate correct code in this case. To prevent an incorrect call from
453        // ever being made, on MSVC6 we generate a warning, and call a function to
454        // make the program crash instantly.
455        typedef char ERROR_VC6CompilerBug[-100];
456        return 0;
457    }
458};
459
460
461#else
462
463// Nasty hack for Microsoft and Intel (IA32 and Itanium)
464// unknown_inheritance classes go here
465// This is probably the ugliest bit of code I've ever written. Look at the casts!
466// There is a compiler bug in MSVC6 which prevents it from using this code.
467template <>
468struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3*sizeof(int) >
469{
470    template <class X, class XFuncType, class GenericMemFuncType>
471    inline static GenericClass *Convert(X *pthis, XFuncType function_to_bind,
472            GenericMemFuncType &bound_func) {
473        // The member function pointer is 16 bytes long. We can't use a normal cast, but
474        // we can use a union to do the conversion.
475        union {
476            XFuncType func;
477            // In VC++ and ICL, an unknown_inheritance member pointer
478            // is internally defined as:
479            struct {
480                GenericMemFuncType m_funcaddress; // points to the actual member function
481                int delta; // #bytes to be added to the 'this' pointer
482                int vtordisp; // #bytes to add to 'this' to find the vtable
483                int vtable_index; // or 0 if no virtual inheritance
484            } s;
485        } u;
486        // Check that the horrible_cast will work
487        typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType)==sizeof(u.s)? 1 : -1];
488        u.func = function_to_bind;
489        bound_func = u.s.funcaddress;
490        int virtual_delta = 0;
491        if (u.s.vtable_index) { // Virtual inheritance is used
492            // First, get to the vtable.
493            // It is 'vtordisp' bytes from the start of the class.
494            const int * vtable = *reinterpret_cast<const int *const*>(
495                reinterpret_cast<const char *>(pthis) + u.s.vtordisp );
496
497            // 'vtable_index' tells us where in the table we should be looking.
498            virtual_delta = u.s.vtordisp + *reinterpret_cast<const int *>(
499                reinterpret_cast<const char *>(vtable) + u.s.vtable_index);
500        }
501        // The int at 'virtual_delta' gives us the amount to add to 'this'.
502        // Finally we can add the three components together. Phew!
503        return reinterpret_cast<GenericClass *>(
504            reinterpret_cast<char *>(pthis) + u.s.delta + virtual_delta);
505    };
506};
507#endif // MSVC 7 and greater
508
509#endif // MS/Intel hacks
510
511} // namespace detail
512
513////////////////////////////////////////////////////////////////////////////////
514// Fast Delegates, part 2:
515//
516// Define the delegate storage, and cope with static functions
517//
518////////////////////////////////////////////////////////////////////////////////
519
520// DelegateMemento -- an opaque structure which can hold an arbitary delegate.
521// It knows nothing about the calling convention or number of arguments used by
522// the function pointed to.
523// It supplies comparison operators so that it can be stored in STL collections.
524// It cannot be set to anything other than null, nor invoked directly:
525// it must be converted to a specific delegate.
526
527// Implementation:
528// There are two possible implementations: the Safe method and the Evil method.
529// DelegateMemento - Safe version
530//
531// This implementation is standard-compliant, but a bit tricky.
532// A static function pointer is stored inside the class.
533// Here are the valid values:
534// +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
535// | 0 | 0 | 0 | Empty |
536// | !=0 |(dontcare)| Invoker | Static function|
537// | 0 | !=0 | !=0* | Method call |
538// +--------------------+----------+------------+----------------+
539// * For Metrowerks, this can be 0. (first virtual function in a
540// single_inheritance class).
541// When stored stored inside a specific delegate, the 'dontcare' entries are replaced
542// with a reference to the delegate itself. This complicates the = and == operators
543// for the delegate class.
544
545// DelegateMemento - Evil version
546//
547// For compilers where data pointers are at least as big as code pointers, it is
548// possible to store the function pointer in the this pointer, using another
549// horrible_cast. In this case the DelegateMemento implementation is simple:
550// +--pThis --+-- pMemFunc-+-- Meaning---------------------+
551// | 0 | 0 | Empty |
552// | !=0 | !=0* | Static function or method call|
553// +----------+------------+-------------------------------+
554// * For Metrowerks, this can be 0. (first virtual function in a
555// single_inheritance class).
556// Note that the Sun C++ and MSVC documentation explicitly state that they
557// support static_cast between void * and function pointers.
558
559class DelegateMemento {
560protected:
561    // the data is protected, not private, because many
562    // compilers have problems with template friends.
563    typedef void (detail::GenericClass::*GenericMemFuncType)(); // arbitrary MFP.
564    GenericMemFuncType m_pFunction;
565    detail::GenericClass *m_pthis;
566
567#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
568    typedef void (*GenericFuncPtr)(); // arbitrary code pointer
569    GenericFuncPtr m_pStaticFunction;
570#endif
571
572public:
573#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
574    DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {};
575    void clear() {
576        m_pthis=0; m_pFunction=0; m_pStaticFunction=0;
577    }
578#else
579    DelegateMemento() : m_pFunction(0), m_pthis(0) {};
580    void clear() { m_pthis=0; m_pFunction=0; }
581#endif
582public:
583#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
584    inline bool IsEqual (const DelegateMemento &x) const{
585        // We have to cope with the static function pointers as a special case
586        if (m_pFunction!=x.m_pFunction) return false;
587        // the static function ptrs must either both be equal, or both be 0.
588        if (m_pStaticFunction!=x.m_pStaticFunction) return false;
589        if (m_pStaticFunction!=0) return m_pthis==x.m_pthis;
590        else return true;
591    }
592#else // Evil Method
593    inline bool IsEqual (const DelegateMemento &x) const{
594        return m_pthis==x.m_pthis && m_pFunction==x.m_pFunction;
595    }
596#endif
597    // Provide a strict weak ordering for DelegateMementos.
598    inline bool IsLess(const DelegateMemento &right) const {
599        // deal with static function pointers first
600#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
601        if (m_pStaticFunction !=0 || right.m_pStaticFunction!=0)
602                return m_pStaticFunction < right.m_pStaticFunction;
603#endif
604        if (m_pthis !=right.m_pthis) return m_pthis < right.m_pthis;
605    // There are no ordering operators for member function pointers,
606    // but we can fake one by comparing each byte. The resulting ordering is
607    // arbitrary (and compiler-dependent), but it permits storage in ordered STL containers.
608        return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
609
610    }
611    // BUGFIX (Mar 2005):
612    // We can't just compare m_pFunction because on Metrowerks,
613    // m_pFunction can be zero even if the delegate is not empty!
614    inline bool operator ! () const // Is it bound to anything?
615    { return m_pthis==0 && m_pFunction==0; }
616    inline bool empty() const // Is it bound to anything?
617    { return m_pthis==0 && m_pFunction==0; }
618public:
619    DelegateMemento & operator = (const DelegateMemento &right) {
620        SetMementoFrom(right);
621        return *this;
622    }
623    inline bool operator <(const DelegateMemento &right) {
624        return IsLess(right);
625    }
626    inline bool operator >(const DelegateMemento &right) {
627        return right.IsLess(*this);
628    }
629    DelegateMemento (const DelegateMemento &right) :
630        m_pFunction(right.m_pFunction), m_pthis(right.m_pthis)
631#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
632        , m_pStaticFunction (right.m_pStaticFunction)
633#endif
634        {}
635protected:
636    void SetMementoFrom(const DelegateMemento &right) {
637        m_pFunction = right.m_pFunction;
638        m_pthis = right.m_pthis;
639#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
640        m_pStaticFunction = right.m_pStaticFunction;
641#endif
642    }
643};
644
645
646// ClosurePtr<>
647//
648// A private wrapper class that adds function signatures to DelegateMemento.
649// It's the class that does most of the actual work.
650// The signatures are specified by:
651// GenericMemFunc: must be a type of GenericClass member function pointer.
652// StaticFuncPtr: must be a type of function pointer with the same signature
653// as GenericMemFunc.
654// UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6
655// where it never returns void (returns DefaultVoid instead).
656
657// An outer class, FastDelegateN<>, handles the invoking and creates the
658// necessary typedefs.
659// This class does everything else.
660
661namespace detail {
662
663template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr>
664class ClosurePtr : public DelegateMemento {
665public:
666    // These functions are for setting the delegate to a member function.
667
668    // Here's the clever bit: we convert an arbitrary member function into a
669    // standard form. XMemFunc should be a member function of class X, but I can't
670    // enforce that here. It needs to be enforced by the wrapper class.
671    template < class X, class XMemFunc >
672    inline void bindmemfunc(X *pthis, XMemFunc function_to_bind ) {
673        m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
674            ::Convert(pthis, function_to_bind, m_pFunction);
675#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
676        m_pStaticFunction = 0;
677#endif
678    }
679    // For const member functions, we only need a const class pointer.
680    // Since we know that the member function is const, it's safe to
681    // remove the const qualifier from the 'this' pointer with a const_cast.
682    // VC6 has problems if we just overload 'bindmemfunc', so we give it a different name.
683    template < class X, class XMemFunc>
684    inline void bindconstmemfunc(const X *pthis, XMemFunc function_to_bind) {
685        m_pthis= SimplifyMemFunc< sizeof(function_to_bind) >
686            ::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction);
687#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
688        m_pStaticFunction = 0;
689#endif
690    }
691#ifdef FASTDELEGATE_GCC_BUG_8271 // At present, GCC doesn't recognize constness of MFPs in templates
692    template < class X, class XMemFunc>
693    inline void bindmemfunc(const X *pthis, XMemFunc function_to_bind) {
694        bindconstmemfunc(pthis, function_to_bind);
695#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
696        m_pStaticFunction = 0;
697#endif
698    }
699#endif
700    // These functions are required for invoking the stored function
701    inline GenericClass *GetClosureThis() const { return m_pthis; }
702    inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); }
703
704// There are a few ways of dealing with static function pointers.
705// There's a standard-compliant, but tricky method.
706// There's also a straightforward hack, that won't work on DOS compilers using the
707// medium memory model. It's so evil that I can't recommend it, but I've
708// implemented it anyway because it produces very nice asm code.
709
710#if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
711
712// ClosurePtr<> - Safe version
713//
714// This implementation is standard-compliant, but a bit tricky.
715// I store the function pointer inside the class, and the delegate then
716// points to itself. Whenever the delegate is copied, these self-references
717// must be transformed, and this complicates the = and == operators.
718public:
719    // The next two functions are for operator ==, =, and the copy constructor.
720    // We may need to convert the m_pthis pointers, so that
721    // they remain as self-references.
722    template< class DerivedClass >
723    inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &x) {
724        SetMementoFrom(x);
725        if (m_pStaticFunction!=0) {
726            // transform self references...
727            m_pthis=reinterpret_cast<GenericClass *>(pParent);
728        }
729    }
730    // For static functions, the 'static_function_invoker' class in the parent
731    // will be called. The parent then needs to call GetStaticFunction() to find out
732    // the actual function to invoke.
733    template < class DerivedClass, class ParentInvokerSig >
734    inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
735                StaticFuncPtr function_to_bind ) {
736        if (function_to_bind==0) { // cope with assignment to 0
737            m_pFunction=0;
738        } else {
739            bindmemfunc(pParent, static_function_invoker);
740        }
741        m_pStaticFunction=reinterpret_cast<GenericFuncPtr>(function_to_bind);
742    }
743    inline UnvoidStaticFuncPtr GetStaticFunction() const {
744        return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction);
745    }
746#else
747
748// ClosurePtr<> - Evil version
749//
750// For compilers where data pointers are at least as big as code pointers, it is
751// possible to store the function pointer in the this pointer, using another
752// horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and
753// speeds up comparison and assignment. If C++ provided direct language support
754// for delegates, they would produce asm code that was almost identical to this.
755// Note that the Sun C++ and MSVC documentation explicitly state that they
756// support static_cast between void * and function pointers.
757
758    template< class DerivedClass >
759    inline void CopyFrom (DerivedClass *pParent, const DelegateMemento &right) {
760        SetMementoFrom(right);
761    }
762    // For static functions, the 'static_function_invoker' class in the parent
763    // will be called. The parent then needs to call GetStaticFunction() to find out
764    // the actual function to invoke.
765    // ******** EVIL, EVIL CODE! *******
766    template < class DerivedClass, class ParentInvokerSig>
767    inline void bindstaticfunc(DerivedClass *pParent, ParentInvokerSig static_function_invoker,
768                StaticFuncPtr function_to_bind) {
769        if (function_to_bind==0) { // cope with assignment to 0
770            m_pFunction=0;
771        } else {
772           // We'll be ignoring the 'this' pointer, but we need to make sure we pass
773           // a valid value to bindmemfunc().
774            bindmemfunc(pParent, static_function_invoker);
775        }
776
777        // WARNING! Evil hack. We store the function in the 'this' pointer!
778        // Ensure that there's a compilation failure if function pointers
779        // and data pointers have different sizes.
780        // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
781        typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass *)==sizeof(function_to_bind) ? 1 : -1];
782        m_pthis = horrible_cast<GenericClass *>(function_to_bind);
783        // MSVC, SunC++ and DMC accept the following (non-standard) code:
784// m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind));
785        // BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long
786// m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind));
787    }
788    // ******** EVIL, EVIL CODE! *******
789    // This function will be called with an invalid 'this' pointer!!
790    // We're just returning the 'this' pointer, converted into
791    // a function pointer!
792    inline UnvoidStaticFuncPtr GetStaticFunction() const {
793        // Ensure that there's a compilation failure if function pointers
794        // and data pointers have different sizes.
795        // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
796        typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr)==sizeof(this) ? 1 : -1];
797        return horrible_cast<UnvoidStaticFuncPtr>(this);
798    }
799#endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
800
801    // Does the closure contain this static function?
802    inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr){
803        if (funcptr==0) return empty();
804    // For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary
805    // value that is not equal to any valid function pointer.
806        else return funcptr==reinterpret_cast<StaticFuncPtr>(GetStaticFunction());
807    }
808};
809
810
811} // namespace detail
812
813////////////////////////////////////////////////////////////////////////////////
814// Fast Delegates, part 3:
815//
816// Wrapper classes to ensure type safety
817//
818////////////////////////////////////////////////////////////////////////////////
819
820
821// Once we have the member function conversion templates, it's easy to make the
822// wrapper classes. So that they will work with as many compilers as possible,
823// the classes are of the form
824// FastDelegate3<int, char *, double>
825// They can cope with any combination of parameters. The max number of parameters
826// allowed is 8, but it is trivial to increase this limit.
827// Note that we need to treat const member functions seperately.
828// All this class does is to enforce type safety, and invoke the delegate with
829// the correct list of parameters.
830
831// Because of the weird rule about the class of derived member function pointers,
832// you sometimes need to apply a downcast to the 'this' pointer.
833// This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below.
834// If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction,
835// without this trick you'd need to write:
836// MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction);
837// but with the trick you can write
838// MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction);
839
840// RetType is the type the compiler uses in compiling the template. For VC6,
841// it cannot be void. DesiredRetType is the real type which is returned from
842// all of the functions. It can be void.
843
844// Implicit conversion to "bool" is achieved using the safe_bool idiom,
845// using member data pointers (MDP). This allows "if (dg)..." syntax
846// Because some compilers (eg codeplay) don't have a unique value for a zero
847// MDP, an extra padding member is added to the SafeBool struct.
848// Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so
849// in that case the static function constructor is not made explicit; this
850// allows "if (dg==0) ..." to compile.
851
852//N=0
853template<class RetType=detail::DefaultVoid>
854class FastDelegate0 {
855private:
856    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
857    typedef DesiredRetType (*StaticFunctionPtr)();
858    typedef RetType (*UnvoidStaticFunctionPtr)();
859    typedef RetType (detail::GenericClass::*GenericMemFn)();
860    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
861    ClosureType m_Closure;
862public:
863    // Typedefs to aid generic programming
864    typedef FastDelegate0 type;
865
866    // Construction and comparison functions
867    FastDelegate0() { clear(); }
868    FastDelegate0(const FastDelegate0 &x) {
869        m_Closure.CopyFrom(this, x.m_Closure); }
870    void operator = (const FastDelegate0 &x) {
871        m_Closure.CopyFrom(this, x.m_Closure); }
872    bool operator ==(const FastDelegate0 &x) const {
873        return m_Closure.IsEqual(x.m_Closure); }
874    bool operator !=(const FastDelegate0 &x) const {
875        return !m_Closure.IsEqual(x.m_Closure); }
876    bool operator <(const FastDelegate0 &x) const {
877        return m_Closure.IsLess(x.m_Closure); }
878    bool operator >(const FastDelegate0 &x) const {
879        return x.m_Closure.IsLess(m_Closure); }
880    // Binding to non-const member functions
881    template < class X, class Y >
882    FastDelegate0(Y *pthis, DesiredRetType (X::* function_to_bind)() ) {
883        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
884    template < class X, class Y >
885    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)()) {
886        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
887    // Binding to const member functions.
888    template < class X, class Y >
889    FastDelegate0(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) {
890        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
891    template < class X, class Y >
892    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)() const) {
893        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
894    // Static functions. We convert them into a member function call.
895    // This constructor also provides implicit conversion
896    FastDelegate0(DesiredRetType (*function_to_bind)() ) {
897        bind(function_to_bind); }
898    // for efficiency, prevent creation of a temporary
899    void operator = (DesiredRetType (*function_to_bind)() ) {
900        bind(function_to_bind); }
901    inline void bind(DesiredRetType (*function_to_bind)()) {
902        m_Closure.bindstaticfunc(this, &FastDelegate0::InvokeStaticFunction,
903            function_to_bind); }
904    // Invoke the delegate
905    RetType operator() () const {
906    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(); }
907    // Implicit conversion to "bool" using the safe_bool idiom
908private:
909    typedef struct SafeBoolStruct {
910        int a_data_pointer_to_this_is_0_on_buggy_compilers;
911        StaticFunctionPtr m_nonzero;
912    } UselessTypedef;
913    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
914public:
915    operator unspecified_bool_type() const {
916        return empty()? 0: &SafeBoolStruct::m_nonzero;
917    }
918    // necessary to allow ==0 to work despite the safe_bool idiom
919    inline bool operator==(StaticFunctionPtr funcptr) {
920        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
921    inline bool operator!=(StaticFunctionPtr funcptr) {
922        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
923    inline bool operator ! () const { // Is it bound to anything?
924            return !m_Closure; }
925    inline bool empty() const {
926            return !m_Closure; }
927    void clear() { m_Closure.clear();}
928    // Conversion to and from the DelegateMemento storage class
929    const DelegateMemento & GetMemento() { return m_Closure; }
930    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
931
932private: // Invoker for static functions
933    RetType InvokeStaticFunction() const {
934    return (*(m_Closure.GetStaticFunction()))(); }
935};
936
937//N=1
938template<class Param1, class RetType=detail::DefaultVoid>
939class FastDelegate1 {
940private:
941    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
942    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1);
943    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1);
944    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1);
945    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
946    ClosureType m_Closure;
947public:
948    // Typedefs to aid generic programming
949    typedef FastDelegate1 type;
950
951    // Construction and comparison functions
952    FastDelegate1() { clear(); }
953    FastDelegate1(const FastDelegate1 &x) {
954        m_Closure.CopyFrom(this, x.m_Closure); }
955    void operator = (const FastDelegate1 &x) {
956        m_Closure.CopyFrom(this, x.m_Closure); }
957    bool operator ==(const FastDelegate1 &x) const {
958        return m_Closure.IsEqual(x.m_Closure); }
959    bool operator !=(const FastDelegate1 &x) const {
960        return !m_Closure.IsEqual(x.m_Closure); }
961    bool operator <(const FastDelegate1 &x) const {
962        return m_Closure.IsLess(x.m_Closure); }
963    bool operator >(const FastDelegate1 &x) const {
964        return x.m_Closure.IsLess(m_Closure); }
965    // Binding to non-const member functions
966    template < class X, class Y >
967    FastDelegate1(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) ) {
968        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
969    template < class X, class Y >
970    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1)) {
971        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
972    // Binding to const member functions.
973    template < class X, class Y >
974    FastDelegate1(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) {
975        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
976    template < class X, class Y >
977    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1) const) {
978        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
979    // Static functions. We convert them into a member function call.
980    // This constructor also provides implicit conversion
981    FastDelegate1(DesiredRetType (*function_to_bind)(Param1 p1) ) {
982        bind(function_to_bind); }
983    // for efficiency, prevent creation of a temporary
984    void operator = (DesiredRetType (*function_to_bind)(Param1 p1) ) {
985        bind(function_to_bind); }
986    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1)) {
987        m_Closure.bindstaticfunc(this, &FastDelegate1::InvokeStaticFunction,
988            function_to_bind); }
989    // Invoke the delegate
990    RetType operator() (Param1 p1) const {
991    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1); }
992    // Implicit conversion to "bool" using the safe_bool idiom
993private:
994    typedef struct SafeBoolStruct {
995        int a_data_pointer_to_this_is_0_on_buggy_compilers;
996        StaticFunctionPtr m_nonzero;
997    } UselessTypedef;
998    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
999public:
1000    operator unspecified_bool_type() const {
1001        return empty()? 0: &SafeBoolStruct::m_nonzero;
1002    }
1003    // necessary to allow ==0 to work despite the safe_bool idiom
1004    inline bool operator==(StaticFunctionPtr funcptr) {
1005        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1006    inline bool operator!=(StaticFunctionPtr funcptr) {
1007        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1008    inline bool operator ! () const { // Is it bound to anything?
1009            return !m_Closure; }
1010    inline bool empty() const {
1011            return !m_Closure; }
1012    void clear() { m_Closure.clear();}
1013    // Conversion to and from the DelegateMemento storage class
1014    const DelegateMemento & GetMemento() { return m_Closure; }
1015    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1016
1017private: // Invoker for static functions
1018    RetType InvokeStaticFunction(Param1 p1) const {
1019    return (*(m_Closure.GetStaticFunction()))(p1); }
1020};
1021
1022//N=2
1023template<class Param1, class Param2, class RetType=detail::DefaultVoid>
1024class FastDelegate2 {
1025private:
1026    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1027    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2);
1028    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2);
1029    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2);
1030    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1031    ClosureType m_Closure;
1032public:
1033    // Typedefs to aid generic programming
1034    typedef FastDelegate2 type;
1035
1036    // Construction and comparison functions
1037    FastDelegate2() { clear(); }
1038    FastDelegate2(const FastDelegate2 &x) {
1039        m_Closure.CopyFrom(this, x.m_Closure); }
1040    void operator = (const FastDelegate2 &x) {
1041        m_Closure.CopyFrom(this, x.m_Closure); }
1042    bool operator ==(const FastDelegate2 &x) const {
1043        return m_Closure.IsEqual(x.m_Closure); }
1044    bool operator !=(const FastDelegate2 &x) const {
1045        return !m_Closure.IsEqual(x.m_Closure); }
1046    bool operator <(const FastDelegate2 &x) const {
1047        return m_Closure.IsLess(x.m_Closure); }
1048    bool operator >(const FastDelegate2 &x) const {
1049        return x.m_Closure.IsLess(m_Closure); }
1050    // Binding to non-const member functions
1051    template < class X, class Y >
1052    FastDelegate2(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) ) {
1053        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1054    template < class X, class Y >
1055    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2)) {
1056        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1057    // Binding to const member functions.
1058    template < class X, class Y >
1059    FastDelegate2(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) {
1060        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1061    template < class X, class Y >
1062    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2) const) {
1063        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1064    // Static functions. We convert them into a member function call.
1065    // This constructor also provides implicit conversion
1066    FastDelegate2(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) {
1067        bind(function_to_bind); }
1068    // for efficiency, prevent creation of a temporary
1069    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2) ) {
1070        bind(function_to_bind); }
1071    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2)) {
1072        m_Closure.bindstaticfunc(this, &FastDelegate2::InvokeStaticFunction,
1073            function_to_bind); }
1074    // Invoke the delegate
1075    RetType operator() (Param1 p1, Param2 p2) const {
1076    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2); }
1077    // Implicit conversion to "bool" using the safe_bool idiom
1078private:
1079    typedef struct SafeBoolStruct {
1080        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1081        StaticFunctionPtr m_nonzero;
1082    } UselessTypedef;
1083    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1084public:
1085    operator unspecified_bool_type() const {
1086        return empty()? 0: &SafeBoolStruct::m_nonzero;
1087    }
1088    // necessary to allow ==0 to work despite the safe_bool idiom
1089    inline bool operator==(StaticFunctionPtr funcptr) {
1090        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1091    inline bool operator!=(StaticFunctionPtr funcptr) {
1092        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1093    inline bool operator ! () const { // Is it bound to anything?
1094            return !m_Closure; }
1095    inline bool empty() const {
1096            return !m_Closure; }
1097    void clear() { m_Closure.clear();}
1098    // Conversion to and from the DelegateMemento storage class
1099    const DelegateMemento & GetMemento() { return m_Closure; }
1100    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1101
1102private: // Invoker for static functions
1103    RetType InvokeStaticFunction(Param1 p1, Param2 p2) const {
1104    return (*(m_Closure.GetStaticFunction()))(p1, p2); }
1105};
1106
1107//N=3
1108template<class Param1, class Param2, class Param3, class RetType=detail::DefaultVoid>
1109class FastDelegate3 {
1110private:
1111    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1112    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
1113    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
1114    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3);
1115    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1116    ClosureType m_Closure;
1117public:
1118    // Typedefs to aid generic programming
1119    typedef FastDelegate3 type;
1120
1121    // Construction and comparison functions
1122    FastDelegate3() { clear(); }
1123    FastDelegate3(const FastDelegate3 &x) {
1124        m_Closure.CopyFrom(this, x.m_Closure); }
1125    void operator = (const FastDelegate3 &x) {
1126        m_Closure.CopyFrom(this, x.m_Closure); }
1127    bool operator ==(const FastDelegate3 &x) const {
1128        return m_Closure.IsEqual(x.m_Closure); }
1129    bool operator !=(const FastDelegate3 &x) const {
1130        return !m_Closure.IsEqual(x.m_Closure); }
1131    bool operator <(const FastDelegate3 &x) const {
1132        return m_Closure.IsLess(x.m_Closure); }
1133    bool operator >(const FastDelegate3 &x) const {
1134        return x.m_Closure.IsLess(m_Closure); }
1135    // Binding to non-const member functions
1136    template < class X, class Y >
1137    FastDelegate3(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
1138        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1139    template < class X, class Y >
1140    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
1141        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1142    // Binding to const member functions.
1143    template < class X, class Y >
1144    FastDelegate3(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
1145        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1146    template < class X, class Y >
1147    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
1148        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1149    // Static functions. We convert them into a member function call.
1150    // This constructor also provides implicit conversion
1151    FastDelegate3(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
1152        bind(function_to_bind); }
1153    // for efficiency, prevent creation of a temporary
1154    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3) ) {
1155        bind(function_to_bind); }
1156    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
1157        m_Closure.bindstaticfunc(this, &FastDelegate3::InvokeStaticFunction,
1158            function_to_bind); }
1159    // Invoke the delegate
1160    RetType operator() (Param1 p1, Param2 p2, Param3 p3) const {
1161    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3); }
1162    // Implicit conversion to "bool" using the safe_bool idiom
1163private:
1164    typedef struct SafeBoolStruct {
1165        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1166        StaticFunctionPtr m_nonzero;
1167    } UselessTypedef;
1168    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1169public:
1170    operator unspecified_bool_type() const {
1171        return empty()? 0: &SafeBoolStruct::m_nonzero;
1172    }
1173    // necessary to allow ==0 to work despite the safe_bool idiom
1174    inline bool operator==(StaticFunctionPtr funcptr) {
1175        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1176    inline bool operator!=(StaticFunctionPtr funcptr) {
1177        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1178    inline bool operator ! () const { // Is it bound to anything?
1179            return !m_Closure; }
1180    inline bool empty() const {
1181            return !m_Closure; }
1182    void clear() { m_Closure.clear();}
1183    // Conversion to and from the DelegateMemento storage class
1184    const DelegateMemento & GetMemento() { return m_Closure; }
1185    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1186
1187private: // Invoker for static functions
1188    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3) const {
1189    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3); }
1190};
1191
1192//N=4
1193template<class Param1, class Param2, class Param3, class Param4, class RetType=detail::DefaultVoid>
1194class FastDelegate4 {
1195private:
1196    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1197    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
1198    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
1199    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
1200    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1201    ClosureType m_Closure;
1202public:
1203    // Typedefs to aid generic programming
1204    typedef FastDelegate4 type;
1205
1206    // Construction and comparison functions
1207    FastDelegate4() { clear(); }
1208    FastDelegate4(const FastDelegate4 &x) {
1209        m_Closure.CopyFrom(this, x.m_Closure); }
1210    void operator = (const FastDelegate4 &x) {
1211        m_Closure.CopyFrom(this, x.m_Closure); }
1212    bool operator ==(const FastDelegate4 &x) const {
1213        return m_Closure.IsEqual(x.m_Closure); }
1214    bool operator !=(const FastDelegate4 &x) const {
1215        return !m_Closure.IsEqual(x.m_Closure); }
1216    bool operator <(const FastDelegate4 &x) const {
1217        return m_Closure.IsLess(x.m_Closure); }
1218    bool operator >(const FastDelegate4 &x) const {
1219        return x.m_Closure.IsLess(m_Closure); }
1220    // Binding to non-const member functions
1221    template < class X, class Y >
1222    FastDelegate4(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
1223        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1224    template < class X, class Y >
1225    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
1226        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1227    // Binding to const member functions.
1228    template < class X, class Y >
1229    FastDelegate4(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
1230        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1231    template < class X, class Y >
1232    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
1233        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1234    // Static functions. We convert them into a member function call.
1235    // This constructor also provides implicit conversion
1236    FastDelegate4(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
1237        bind(function_to_bind); }
1238    // for efficiency, prevent creation of a temporary
1239    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) ) {
1240        bind(function_to_bind); }
1241    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
1242        m_Closure.bindstaticfunc(this, &FastDelegate4::InvokeStaticFunction,
1243            function_to_bind); }
1244    // Invoke the delegate
1245    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
1246    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4); }
1247    // Implicit conversion to "bool" using the safe_bool idiom
1248private:
1249    typedef struct SafeBoolStruct {
1250        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1251        StaticFunctionPtr m_nonzero;
1252    } UselessTypedef;
1253    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1254public:
1255    operator unspecified_bool_type() const {
1256        return empty()? 0: &SafeBoolStruct::m_nonzero;
1257    }
1258    // necessary to allow ==0 to work despite the safe_bool idiom
1259    inline bool operator==(StaticFunctionPtr funcptr) {
1260        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1261    inline bool operator!=(StaticFunctionPtr funcptr) {
1262        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1263    inline bool operator ! () const { // Is it bound to anything?
1264            return !m_Closure; }
1265    inline bool empty() const {
1266            return !m_Closure; }
1267    void clear() { m_Closure.clear();}
1268    // Conversion to and from the DelegateMemento storage class
1269    const DelegateMemento & GetMemento() { return m_Closure; }
1270    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1271
1272private: // Invoker for static functions
1273    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
1274    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4); }
1275};
1276
1277//N=5
1278template<class Param1, class Param2, class Param3, class Param4, class Param5, class RetType=detail::DefaultVoid>
1279class FastDelegate5 {
1280private:
1281    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1282    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
1283    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
1284    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
1285    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1286    ClosureType m_Closure;
1287public:
1288    // Typedefs to aid generic programming
1289    typedef FastDelegate5 type;
1290
1291    // Construction and comparison functions
1292    FastDelegate5() { clear(); }
1293    FastDelegate5(const FastDelegate5 &x) {
1294        m_Closure.CopyFrom(this, x.m_Closure); }
1295    void operator = (const FastDelegate5 &x) {
1296        m_Closure.CopyFrom(this, x.m_Closure); }
1297    bool operator ==(const FastDelegate5 &x) const {
1298        return m_Closure.IsEqual(x.m_Closure); }
1299    bool operator !=(const FastDelegate5 &x) const {
1300        return !m_Closure.IsEqual(x.m_Closure); }
1301    bool operator <(const FastDelegate5 &x) const {
1302        return m_Closure.IsLess(x.m_Closure); }
1303    bool operator >(const FastDelegate5 &x) const {
1304        return x.m_Closure.IsLess(m_Closure); }
1305    // Binding to non-const member functions
1306    template < class X, class Y >
1307    FastDelegate5(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
1308        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1309    template < class X, class Y >
1310    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
1311        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1312    // Binding to const member functions.
1313    template < class X, class Y >
1314    FastDelegate5(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
1315        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1316    template < class X, class Y >
1317    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
1318        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1319    // Static functions. We convert them into a member function call.
1320    // This constructor also provides implicit conversion
1321    FastDelegate5(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
1322        bind(function_to_bind); }
1323    // for efficiency, prevent creation of a temporary
1324    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) ) {
1325        bind(function_to_bind); }
1326    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
1327        m_Closure.bindstaticfunc(this, &FastDelegate5::InvokeStaticFunction,
1328            function_to_bind); }
1329    // Invoke the delegate
1330    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
1331    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5); }
1332    // Implicit conversion to "bool" using the safe_bool idiom
1333private:
1334    typedef struct SafeBoolStruct {
1335        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1336        StaticFunctionPtr m_nonzero;
1337    } UselessTypedef;
1338    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1339public:
1340    operator unspecified_bool_type() const {
1341        return empty()? 0: &SafeBoolStruct::m_nonzero;
1342    }
1343    // necessary to allow ==0 to work despite the safe_bool idiom
1344    inline bool operator==(StaticFunctionPtr funcptr) {
1345        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1346    inline bool operator!=(StaticFunctionPtr funcptr) {
1347        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1348    inline bool operator ! () const { // Is it bound to anything?
1349            return !m_Closure; }
1350    inline bool empty() const {
1351            return !m_Closure; }
1352    void clear() { m_Closure.clear();}
1353    // Conversion to and from the DelegateMemento storage class
1354    const DelegateMemento & GetMemento() { return m_Closure; }
1355    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1356
1357private: // Invoker for static functions
1358    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
1359    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5); }
1360};
1361
1362//N=6
1363template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType=detail::DefaultVoid>
1364class FastDelegate6 {
1365private:
1366    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1367    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
1368    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
1369    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
1370    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1371    ClosureType m_Closure;
1372public:
1373    // Typedefs to aid generic programming
1374    typedef FastDelegate6 type;
1375
1376    // Construction and comparison functions
1377    FastDelegate6() { clear(); }
1378    FastDelegate6(const FastDelegate6 &x) {
1379        m_Closure.CopyFrom(this, x.m_Closure); }
1380    void operator = (const FastDelegate6 &x) {
1381        m_Closure.CopyFrom(this, x.m_Closure); }
1382    bool operator ==(const FastDelegate6 &x) const {
1383        return m_Closure.IsEqual(x.m_Closure); }
1384    bool operator !=(const FastDelegate6 &x) const {
1385        return !m_Closure.IsEqual(x.m_Closure); }
1386    bool operator <(const FastDelegate6 &x) const {
1387        return m_Closure.IsLess(x.m_Closure); }
1388    bool operator >(const FastDelegate6 &x) const {
1389        return x.m_Closure.IsLess(m_Closure); }
1390    // Binding to non-const member functions
1391    template < class X, class Y >
1392    FastDelegate6(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
1393        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1394    template < class X, class Y >
1395    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
1396        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1397    // Binding to const member functions.
1398    template < class X, class Y >
1399    FastDelegate6(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
1400        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1401    template < class X, class Y >
1402    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
1403        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1404    // Static functions. We convert them into a member function call.
1405    // This constructor also provides implicit conversion
1406    FastDelegate6(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
1407        bind(function_to_bind); }
1408    // for efficiency, prevent creation of a temporary
1409    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) ) {
1410        bind(function_to_bind); }
1411    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
1412        m_Closure.bindstaticfunc(this, &FastDelegate6::InvokeStaticFunction,
1413            function_to_bind); }
1414    // Invoke the delegate
1415    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
1416    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6); }
1417    // Implicit conversion to "bool" using the safe_bool idiom
1418private:
1419    typedef struct SafeBoolStruct {
1420        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1421        StaticFunctionPtr m_nonzero;
1422    } UselessTypedef;
1423    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1424public:
1425    operator unspecified_bool_type() const {
1426        return empty()? 0: &SafeBoolStruct::m_nonzero;
1427    }
1428    // necessary to allow ==0 to work despite the safe_bool idiom
1429    inline bool operator==(StaticFunctionPtr funcptr) {
1430        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1431    inline bool operator!=(StaticFunctionPtr funcptr) {
1432        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1433    inline bool operator ! () const { // Is it bound to anything?
1434            return !m_Closure; }
1435    inline bool empty() const {
1436            return !m_Closure; }
1437    void clear() { m_Closure.clear();}
1438    // Conversion to and from the DelegateMemento storage class
1439    const DelegateMemento & GetMemento() { return m_Closure; }
1440    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1441
1442private: // Invoker for static functions
1443    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
1444    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6); }
1445};
1446
1447//N=7
1448template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType=detail::DefaultVoid>
1449class FastDelegate7 {
1450private:
1451    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1452    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
1453    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
1454    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
1455    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1456    ClosureType m_Closure;
1457public:
1458    // Typedefs to aid generic programming
1459    typedef FastDelegate7 type;
1460
1461    // Construction and comparison functions
1462    FastDelegate7() { clear(); }
1463    FastDelegate7(const FastDelegate7 &x) {
1464        m_Closure.CopyFrom(this, x.m_Closure); }
1465    void operator = (const FastDelegate7 &x) {
1466        m_Closure.CopyFrom(this, x.m_Closure); }
1467    bool operator ==(const FastDelegate7 &x) const {
1468        return m_Closure.IsEqual(x.m_Closure); }
1469    bool operator !=(const FastDelegate7 &x) const {
1470        return !m_Closure.IsEqual(x.m_Closure); }
1471    bool operator <(const FastDelegate7 &x) const {
1472        return m_Closure.IsLess(x.m_Closure); }
1473    bool operator >(const FastDelegate7 &x) const {
1474        return x.m_Closure.IsLess(m_Closure); }
1475    // Binding to non-const member functions
1476    template < class X, class Y >
1477    FastDelegate7(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
1478        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1479    template < class X, class Y >
1480    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
1481        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1482    // Binding to const member functions.
1483    template < class X, class Y >
1484    FastDelegate7(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
1485        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1486    template < class X, class Y >
1487    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
1488        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1489    // Static functions. We convert them into a member function call.
1490    // This constructor also provides implicit conversion
1491    FastDelegate7(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
1492        bind(function_to_bind); }
1493    // for efficiency, prevent creation of a temporary
1494    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) ) {
1495        bind(function_to_bind); }
1496    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
1497        m_Closure.bindstaticfunc(this, &FastDelegate7::InvokeStaticFunction,
1498            function_to_bind); }
1499    // Invoke the delegate
1500    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
1501    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7); }
1502    // Implicit conversion to "bool" using the safe_bool idiom
1503private:
1504    typedef struct SafeBoolStruct {
1505        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1506        StaticFunctionPtr m_nonzero;
1507    } UselessTypedef;
1508    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1509public:
1510    operator unspecified_bool_type() const {
1511        return empty()? 0: &SafeBoolStruct::m_nonzero;
1512    }
1513    // necessary to allow ==0 to work despite the safe_bool idiom
1514    inline bool operator==(StaticFunctionPtr funcptr) {
1515        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1516    inline bool operator!=(StaticFunctionPtr funcptr) {
1517        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1518    inline bool operator ! () const { // Is it bound to anything?
1519            return !m_Closure; }
1520    inline bool empty() const {
1521            return !m_Closure; }
1522    void clear() { m_Closure.clear();}
1523    // Conversion to and from the DelegateMemento storage class
1524    const DelegateMemento & GetMemento() { return m_Closure; }
1525    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1526
1527private: // Invoker for static functions
1528    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
1529    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7); }
1530};
1531
1532//N=8
1533template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType=detail::DefaultVoid>
1534class FastDelegate8 {
1535private:
1536    typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
1537    typedef DesiredRetType (*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
1538    typedef RetType (*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
1539    typedef RetType (detail::GenericClass::*GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
1540    typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
1541    ClosureType m_Closure;
1542public:
1543    // Typedefs to aid generic programming
1544    typedef FastDelegate8 type;
1545
1546    // Construction and comparison functions
1547    FastDelegate8() { clear(); }
1548    FastDelegate8(const FastDelegate8 &x) {
1549        m_Closure.CopyFrom(this, x.m_Closure); }
1550    void operator = (const FastDelegate8 &x) {
1551        m_Closure.CopyFrom(this, x.m_Closure); }
1552    bool operator ==(const FastDelegate8 &x) const {
1553        return m_Closure.IsEqual(x.m_Closure); }
1554    bool operator !=(const FastDelegate8 &x) const {
1555        return !m_Closure.IsEqual(x.m_Closure); }
1556    bool operator <(const FastDelegate8 &x) const {
1557        return m_Closure.IsLess(x.m_Closure); }
1558    bool operator >(const FastDelegate8 &x) const {
1559        return x.m_Closure.IsLess(m_Closure); }
1560    // Binding to non-const member functions
1561    template < class X, class Y >
1562    FastDelegate8(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) {
1563        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1564    template < class X, class Y >
1565    inline void bind(Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
1566        m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind); }
1567    // Binding to const member functions.
1568    template < class X, class Y >
1569    FastDelegate8(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const) {
1570        m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind); }
1571    template < class X, class Y >
1572    inline void bind(const Y *pthis, DesiredRetType (X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const) {
1573        m_Closure.bindconstmemfunc(detail::implicit_cast<const X *>(pthis), function_to_bind); }
1574    // Static functions. We convert them into a member function call.
1575    // This constructor also provides implicit conversion
1576    FastDelegate8(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) {
1577        bind(function_to_bind); }
1578    // for efficiency, prevent creation of a temporary
1579    void operator = (DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) ) {
1580        bind(function_to_bind); }
1581    inline void bind(DesiredRetType (*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
1582        m_Closure.bindstaticfunc(this, &FastDelegate8::InvokeStaticFunction,
1583            function_to_bind); }
1584    // Invoke the delegate
1585    RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const {
1586    return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7, p8); }
1587    // Implicit conversion to "bool" using the safe_bool idiom
1588private:
1589    typedef struct SafeBoolStruct {
1590        int a_data_pointer_to_this_is_0_on_buggy_compilers;
1591        StaticFunctionPtr m_nonzero;
1592    } UselessTypedef;
1593    typedef StaticFunctionPtr SafeBoolStruct::*unspecified_bool_type;
1594public:
1595    operator unspecified_bool_type() const {
1596        return empty()? 0: &SafeBoolStruct::m_nonzero;
1597    }
1598    // necessary to allow ==0 to work despite the safe_bool idiom
1599    inline bool operator==(StaticFunctionPtr funcptr) {
1600        return m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1601    inline bool operator!=(StaticFunctionPtr funcptr) {
1602        return !m_Closure.IsEqualToStaticFuncPtr(funcptr); }
1603    inline bool operator ! () const { // Is it bound to anything?
1604            return !m_Closure; }
1605    inline bool empty() const {
1606            return !m_Closure; }
1607    void clear() { m_Closure.clear();}
1608    // Conversion to and from the DelegateMemento storage class
1609    const DelegateMemento & GetMemento() { return m_Closure; }
1610    void SetMemento(const DelegateMemento &any) { m_Closure.CopyFrom(this, any); }
1611
1612private: // Invoker for static functions
1613    RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const {
1614    return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7, p8); }
1615};
1616
1617
1618////////////////////////////////////////////////////////////////////////////////
1619// Fast Delegates, part 4:
1620//
1621// FastDelegate<> class (Original author: Jody Hagins)
1622// Allows boost::function style syntax like:
1623// FastDelegate< double (int, long) >
1624// instead of:
1625// FastDelegate2< int, long, double >
1626//
1627////////////////////////////////////////////////////////////////////////////////
1628
1629#ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
1630
1631// Declare FastDelegate as a class template. It will be specialized
1632// later for all number of arguments.
1633template <typename Signature>
1634class FastDelegate;
1635
1636//N=0
1637// Specialization to allow use of
1638// FastDelegate< R ( ) >
1639// instead of
1640// FastDelegate0 < R >
1641template<typename R>
1642class FastDelegate< R ( ) >
1643  // Inherit from FastDelegate0 so that it can be treated just like a FastDelegate0
1644  : public FastDelegate0 < R >
1645{
1646public:
1647  // Make using the base type a bit easier via typedef.
1648  typedef FastDelegate0 < R > BaseType;
1649
1650  // Allow users access to the specific type of this delegate.
1651  typedef FastDelegate SelfType;
1652
1653  // Mimic the base class constructors.
1654  FastDelegate() : BaseType() { }
1655
1656  template < class X, class Y >
1657  FastDelegate(Y * pthis,
1658    R (X::* function_to_bind)( ))
1659    : BaseType(pthis, function_to_bind) { }
1660
1661  template < class X, class Y >
1662  FastDelegate(const Y *pthis,
1663      R (X::* function_to_bind)( ) const)
1664    : BaseType(pthis, function_to_bind)
1665  { }
1666
1667  FastDelegate(R (*function_to_bind)( ))
1668    : BaseType(function_to_bind) { }
1669  void operator = (const BaseType &x) {
1670        *static_cast<BaseType*>(this) = x; }
1671};
1672
1673//N=1
1674// Specialization to allow use of
1675// FastDelegate< R ( Param1 ) >
1676// instead of
1677// FastDelegate1 < Param1, R >
1678template<typename R, class Param1>
1679class FastDelegate< R ( Param1 ) >
1680  // Inherit from FastDelegate1 so that it can be treated just like a FastDelegate1
1681  : public FastDelegate1 < Param1, R >
1682{
1683public:
1684  // Make using the base type a bit easier via typedef.
1685  typedef FastDelegate1 < Param1, R > BaseType;
1686
1687  // Allow users access to the specific type of this delegate.
1688  typedef FastDelegate SelfType;
1689
1690  // Mimic the base class constructors.
1691  FastDelegate() : BaseType() { }
1692
1693  template < class X, class Y >
1694  FastDelegate(Y * pthis,
1695    R (X::* function_to_bind)( Param1 p1 ))
1696    : BaseType(pthis, function_to_bind) { }
1697
1698  template < class X, class Y >
1699  FastDelegate(const Y *pthis,
1700      R (X::* function_to_bind)( Param1 p1 ) const)
1701    : BaseType(pthis, function_to_bind)
1702  { }
1703
1704  FastDelegate(R (*function_to_bind)( Param1 p1 ))
1705    : BaseType(function_to_bind) { }
1706  void operator = (const BaseType &x) {
1707        *static_cast<BaseType*>(this) = x; }
1708};
1709
1710//N=2
1711// Specialization to allow use of
1712// FastDelegate< R ( Param1, Param2 ) >
1713// instead of
1714// FastDelegate2 < Param1, Param2, R >
1715template<typename R, class Param1, class Param2>
1716class FastDelegate< R ( Param1, Param2 ) >
1717  // Inherit from FastDelegate2 so that it can be treated just like a FastDelegate2
1718  : public FastDelegate2 < Param1, Param2, R >
1719{
1720public:
1721  // Make using the base type a bit easier via typedef.
1722  typedef FastDelegate2 < Param1, Param2, R > BaseType;
1723
1724  // Allow users access to the specific type of this delegate.
1725  typedef FastDelegate SelfType;
1726
1727  // Mimic the base class constructors.
1728  FastDelegate() : BaseType() { }
1729
1730  template < class X, class Y >
1731  FastDelegate(Y * pthis,
1732    R (X::* function_to_bind)( Param1 p1, Param2 p2 ))
1733    : BaseType(pthis, function_to_bind) { }
1734
1735  template < class X, class Y >
1736  FastDelegate(const Y *pthis,
1737      R (X::* function_to_bind)( Param1 p1, Param2 p2 ) const)
1738    : BaseType(pthis, function_to_bind)
1739  { }
1740
1741  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2 ))
1742    : BaseType(function_to_bind) { }
1743  void operator = (const BaseType &x) {
1744        *static_cast<BaseType*>(this) = x; }
1745};
1746
1747//N=3
1748// Specialization to allow use of
1749// FastDelegate< R ( Param1, Param2, Param3 ) >
1750// instead of
1751// FastDelegate3 < Param1, Param2, Param3, R >
1752template<typename R, class Param1, class Param2, class Param3>
1753class FastDelegate< R ( Param1, Param2, Param3 ) >
1754  // Inherit from FastDelegate3 so that it can be treated just like a FastDelegate3
1755  : public FastDelegate3 < Param1, Param2, Param3, R >
1756{
1757public:
1758  // Make using the base type a bit easier via typedef.
1759  typedef FastDelegate3 < Param1, Param2, Param3, R > BaseType;
1760
1761  // Allow users access to the specific type of this delegate.
1762  typedef FastDelegate SelfType;
1763
1764  // Mimic the base class constructors.
1765  FastDelegate() : BaseType() { }
1766
1767  template < class X, class Y >
1768  FastDelegate(Y * pthis,
1769    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ))
1770    : BaseType(pthis, function_to_bind) { }
1771
1772  template < class X, class Y >
1773  FastDelegate(const Y *pthis,
1774      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ) const)
1775    : BaseType(pthis, function_to_bind)
1776  { }
1777
1778  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3 ))
1779    : BaseType(function_to_bind) { }
1780  void operator = (const BaseType &x) {
1781        *static_cast<BaseType*>(this) = x; }
1782};
1783
1784//N=4
1785// Specialization to allow use of
1786// FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
1787// instead of
1788// FastDelegate4 < Param1, Param2, Param3, Param4, R >
1789template<typename R, class Param1, class Param2, class Param3, class Param4>
1790class FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
1791  // Inherit from FastDelegate4 so that it can be treated just like a FastDelegate4
1792  : public FastDelegate4 < Param1, Param2, Param3, Param4, R >
1793{
1794public:
1795  // Make using the base type a bit easier via typedef.
1796  typedef FastDelegate4 < Param1, Param2, Param3, Param4, R > BaseType;
1797
1798  // Allow users access to the specific type of this delegate.
1799  typedef FastDelegate SelfType;
1800
1801  // Mimic the base class constructors.
1802  FastDelegate() : BaseType() { }
1803
1804  template < class X, class Y >
1805  FastDelegate(Y * pthis,
1806    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ))
1807    : BaseType(pthis, function_to_bind) { }
1808
1809  template < class X, class Y >
1810  FastDelegate(const Y *pthis,
1811      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ) const)
1812    : BaseType(pthis, function_to_bind)
1813  { }
1814
1815  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4 ))
1816    : BaseType(function_to_bind) { }
1817  void operator = (const BaseType &x) {
1818        *static_cast<BaseType*>(this) = x; }
1819};
1820
1821//N=5
1822// Specialization to allow use of
1823// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
1824// instead of
1825// FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
1826template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5>
1827class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
1828  // Inherit from FastDelegate5 so that it can be treated just like a FastDelegate5
1829  : public FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
1830{
1831public:
1832  // Make using the base type a bit easier via typedef.
1833  typedef FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > BaseType;
1834
1835  // Allow users access to the specific type of this delegate.
1836  typedef FastDelegate SelfType;
1837
1838  // Mimic the base class constructors.
1839  FastDelegate() : BaseType() { }
1840
1841  template < class X, class Y >
1842  FastDelegate(Y * pthis,
1843    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ))
1844    : BaseType(pthis, function_to_bind) { }
1845
1846  template < class X, class Y >
1847  FastDelegate(const Y *pthis,
1848      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ) const)
1849    : BaseType(pthis, function_to_bind)
1850  { }
1851
1852  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5 ))
1853    : BaseType(function_to_bind) { }
1854  void operator = (const BaseType &x) {
1855        *static_cast<BaseType*>(this) = x; }
1856};
1857
1858//N=6
1859// Specialization to allow use of
1860// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
1861// instead of
1862// FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
1863template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6>
1864class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
1865  // Inherit from FastDelegate6 so that it can be treated just like a FastDelegate6
1866  : public FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
1867{
1868public:
1869  // Make using the base type a bit easier via typedef.
1870  typedef FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > BaseType;
1871
1872  // Allow users access to the specific type of this delegate.
1873  typedef FastDelegate SelfType;
1874
1875  // Mimic the base class constructors.
1876  FastDelegate() : BaseType() { }
1877
1878  template < class X, class Y >
1879  FastDelegate(Y * pthis,
1880    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ))
1881    : BaseType(pthis, function_to_bind) { }
1882
1883  template < class X, class Y >
1884  FastDelegate(const Y *pthis,
1885      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ) const)
1886    : BaseType(pthis, function_to_bind)
1887  { }
1888
1889  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6 ))
1890    : BaseType(function_to_bind) { }
1891  void operator = (const BaseType &x) {
1892        *static_cast<BaseType*>(this) = x; }
1893};
1894
1895//N=7
1896// Specialization to allow use of
1897// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
1898// instead of
1899// FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
1900template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7>
1901class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
1902  // Inherit from FastDelegate7 so that it can be treated just like a FastDelegate7
1903  : public FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
1904{
1905public:
1906  // Make using the base type a bit easier via typedef.
1907  typedef FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > BaseType;
1908
1909  // Allow users access to the specific type of this delegate.
1910  typedef FastDelegate SelfType;
1911
1912  // Mimic the base class constructors.
1913  FastDelegate() : BaseType() { }
1914
1915  template < class X, class Y >
1916  FastDelegate(Y * pthis,
1917    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ))
1918    : BaseType(pthis, function_to_bind) { }
1919
1920  template < class X, class Y >
1921  FastDelegate(const Y *pthis,
1922      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ) const)
1923    : BaseType(pthis, function_to_bind)
1924  { }
1925
1926  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7 ))
1927    : BaseType(function_to_bind) { }
1928  void operator = (const BaseType &x) {
1929        *static_cast<BaseType*>(this) = x; }
1930};
1931
1932//N=8
1933// Specialization to allow use of
1934// FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) >
1935// instead of
1936// FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R >
1937template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8>
1938class FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) >
1939  // Inherit from FastDelegate8 so that it can be treated just like a FastDelegate8
1940  : public FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R >
1941{
1942public:
1943  // Make using the base type a bit easier via typedef.
1944  typedef FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R > BaseType;
1945
1946  // Allow users access to the specific type of this delegate.
1947  typedef FastDelegate SelfType;
1948
1949  // Mimic the base class constructors.
1950  FastDelegate() : BaseType() { }
1951
1952  template < class X, class Y >
1953  FastDelegate(Y * pthis,
1954    R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ))
1955    : BaseType(pthis, function_to_bind) { }
1956
1957  template < class X, class Y >
1958  FastDelegate(const Y *pthis,
1959      R (X::* function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ) const)
1960    : BaseType(pthis, function_to_bind)
1961  { }
1962
1963  FastDelegate(R (*function_to_bind)( Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8 ))
1964    : BaseType(function_to_bind) { }
1965  void operator = (const BaseType &x) {
1966        *static_cast<BaseType*>(this) = x; }
1967};
1968
1969
1970#endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
1971
1972////////////////////////////////////////////////////////////////////////////////
1973// Fast Delegates, part 5:
1974//
1975// MakeDelegate() helper function
1976//
1977// MakeDelegate(&x, &X::func) returns a fastdelegate of the type
1978// necessary for calling x.func() with the correct number of arguments.
1979// This makes it possible to eliminate many typedefs from user code.
1980//
1981////////////////////////////////////////////////////////////////////////////////
1982
1983// Also declare overloads of a MakeDelegate() global function to
1984// reduce the need for typedefs.
1985// We need seperate overloads for const and non-const member functions.
1986// Also, because of the weird rule about the class of derived member function pointers,
1987// implicit downcasts may need to be applied later to the 'this' pointer.
1988// That's why two classes (X and Y) appear in the definitions. Y must be implicitly
1989// castable to X.
1990
1991// Workaround for VC6. VC6 needs void return types converted into DefaultVoid.
1992// GCC 3.2 and later won't compile this unless it's preceded by 'typename',
1993// but VC6 doesn't allow 'typename' in this context.
1994// So, I have to use a macro.
1995
1996#ifdef FASTDLGT_VC6
1997#define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type
1998#else
1999#define FASTDLGT_RETTYPE RetType
2000#endif
2001
2002//N=0
2003template <class X, class Y, class RetType>
2004FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)()) {
2005    return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
2006}
2007
2008template <class X, class Y, class RetType>
2009FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)() const) {
2010    return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
2011}
2012
2013//N=1
2014template <class X, class Y, class Param1, class RetType>
2015FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1)) {
2016    return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
2017}
2018
2019template <class X, class Y, class Param1, class RetType>
2020FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1) const) {
2021    return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
2022}
2023
2024//N=2
2025template <class X, class Y, class Param1, class Param2, class RetType>
2026FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2)) {
2027    return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
2028}
2029
2030template <class X, class Y, class Param1, class Param2, class RetType>
2031FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2) const) {
2032    return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
2033}
2034
2035//N=3
2036template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
2037FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3)) {
2038    return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
2039}
2040
2041template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
2042FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3) const) {
2043    return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
2044}
2045
2046//N=4
2047template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
2048FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
2049    return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
2050}
2051
2052template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
2053FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
2054    return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
2055}
2056
2057//N=5
2058template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
2059FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
2060    return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
2061}
2062
2063template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
2064FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
2065    return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
2066}
2067
2068//N=6
2069template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
2070FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
2071    return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
2072}
2073
2074template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
2075FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
2076    return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
2077}
2078
2079//N=7
2080template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
2081FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
2082    return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
2083}
2084
2085template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
2086FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
2087    return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
2088}
2089
2090//N=8
2091template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType>
2092FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
2093    return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func);
2094}
2095
2096template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType>
2097FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType (X::*func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const) {
2098    return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func);
2099}
2100
2101
2102 // clean up after ourselves...
2103#undef FASTDLGT_RETTYPE
2104
2105} // namespace fastdelegate
2106
2107#endif // !defined(FASTDELEGATE_H)
2108
src/cpu.cpp
11#include <stdlib.h>
2#include <stdio.h>
2#include <stdio.h>
33#include <sys/mman.h>
4#include <unistd.h>
5#include <fcntl.h>
4#include <unistd.h>
5#include <fcntl.h>
66#include "jz4740.h"
7#include "cpu.h"
8
7#include "cpu.h"
8
99inline int sdram_convert(unsigned int pllin,unsigned int *sdram_freq)
1010{
1111    register unsigned int ns, tmp;
...... 
2020    return 0;
2121
2222}
23
24void pll_init(unsigned int clock)
25{
23
24void pll_init(unsigned int clock)
25{
2626    register unsigned int cfcr, plcr1;
2727    unsigned int sdramclock = 0;
2828    int n2FR[33] = {
...... 
4444    pllout2 = (cfcr & CPM_CPCCR_PCS) ? clock : (clock / 2);
4545
4646    /* Init UHC clock */
47// REG_CPM_UHCCDR = pllout2 / 48000000 - 1;
48        jz_cpmregl[0x6C>>2] = pllout2 / 48000000 - 1;
47// REG_CPM_UHCCDR = pllout2 / 48000000 - 1;
48        jz_cpmregl[0x6C>>2] = pllout2 / 48000000 - 1;
4949
5050    nf = clock * 2 / CFG_EXTAL;
5151    plcr1 = ((nf - 2) << CPM_CPPCR_PLLM_BIT) | /* FD */
...... 
5656
5757    /* init PLL */
5858// REG_CPM_CPCCR = cfcr;
59// REG_CPM_CPPCR = plcr1;
60          jz_cpmregl[0] = cfcr;
61        jz_cpmregl[0x10>>2] = plcr1;
59// REG_CPM_CPPCR = plcr1;
60          jz_cpmregl[0] = cfcr;
61        jz_cpmregl[0x10>>2] = plcr1;
6262
6363      sdram_convert(clock,&sdramclock);
6464      if(sdramclock > 0)
6565      {
6666// REG_EMC_RTCOR = sdramclock;
67// REG_EMC_RTCNT = sdramclock;
68          jz_emcregs[0x8C>>1] = sdramclock;
69        jz_emcregs[0x88>>1] = sdramclock;
67// REG_EMC_RTCNT = sdramclock;
68          jz_emcregs[0x8C>>1] = sdramclock;
69        jz_emcregs[0x88>>1] = sdramclock;
7070
7171      }else
7272      {
7373      printf("sdram init fail!\n");
7474      while(1);
75      }
76
77}
78
79
80void jz_cpuspeed(unsigned clockspeed)
81{
75      }
76
77}
78
79
80void jz_cpuspeed(unsigned clockspeed)
81{
8282    if (clockspeed >= 200 && clockspeed <= 430)
8383    {
8484        jz_dev = open("/dev/mem", O_RDWR);
8585        if(jz_dev)
86        {
87            jz_cpmregl=(unsigned long *)mmap(0, 0x80, PROT_READ|PROT_WRITE, MAP_SHARED, jz_dev, 0x10000000);
88            jz_emcregl=(unsigned long *)mmap(0, 0x90, PROT_READ|PROT_WRITE, MAP_SHARED, jz_dev, 0x13010000);
89            jz_emcregs=(unsigned short *)jz_emcregl;
90            pll_init(clockspeed*1000000);
91            munmap((void *)jz_cpmregl, 0x80);
92            munmap((void *)jz_emcregl, 0x90);
86        {
87            jz_cpmregl=(unsigned long *)mmap(0, 0x80, PROT_READ|PROT_WRITE, MAP_SHARED, jz_dev, 0x10000000);
88            jz_emcregl=(unsigned long *)mmap(0, 0x90, PROT_READ|PROT_WRITE, MAP_SHARED, jz_dev, 0x13010000);
89            jz_emcregs=(unsigned short *)jz_emcregl;
90            pll_init(clockspeed*1000000);
91            munmap((void *)jz_cpmregl, 0x80);
92            munmap((void *)jz_emcregl, 0x90);
9393            close(jz_dev);
9494        }
9595        else
9696            printf("failed opening /dev/mem \n");
9797    }
98}
98}
src/cpu.h
11#ifndef CPU_H
22#define CPU_H
33
4/* Define this to the CPU frequency */
5#define CPU_FREQ 336000000 /* CPU clock: 336 MHz */
6#define CFG_EXTAL 12000000 /* EXT clock: 12 Mhz */
7
4/* Define this to the CPU frequency */
5#define CPU_FREQ 336000000 /* CPU clock: 336 MHz */
6#define CFG_EXTAL 12000000 /* EXT clock: 12 Mhz */
7
88// SDRAM Timings, unit: ns
99#define SDRAM_TRAS 45 /* RAS# Active Time */
1010#define SDRAM_RCD 20 /* RAS# to CAS# Delay */
1111#define SDRAM_TPC 20 /* RAS# Precharge Time */
1212#define SDRAM_TRWL 7 /* Write Latency Time */
13#define SDRAM_TREF 15625 /* Refresh period: 4096 refresh cycles/64ms */
13#define SDRAM_TREF 15625 /* Refresh period: 4096 refresh cycles/64ms */
1414//#define SDRAM_TREF 7812 /* Refresh period: 8192 refresh cycles/64ms */
1515
16static unsigned long jz_dev;
17static volatile unsigned long *jz_cpmregl, *jz_emcregl;
16static unsigned long jz_dev;
17static volatile unsigned long *jz_cpmregl, *jz_emcregl;
1818volatile unsigned short *jz_emcregs;
1919
2020void jz_cpuspeed(unsigned clockspeed);
21void pll_init(unsigned int clock);
21void pll_init(unsigned int clock);
2222inline int sdram_convert(unsigned int pllin,unsigned int *sdram_freq);
23
23
2424#endif
src/jz4740.h
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * Copyright (C) 2006-2007 by Ingenic Semiconductor Inc.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23/*
24 * linux/include/asm-mips/mach-jz4740/jz4740.h
25 *
26 * JZ4740 common definition.
27 *
28 * Copyright (C) 2006 - 2007 Ingenic Semiconductor Inc.
29 *
30 * Author: <lhhuang@ingenic.cn>
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License version 2 as
34 * published by the Free Software Foundation.
35 */
36
37/*
38 * Include file for Ingenic Semiconductor's JZ4740 CPU.
39 */
40#ifndef __JZ4740_H__
41#define __JZ4740_H__
42
43#ifndef __ASSEMBLY__
44
45#define REG8(addr) (*(volatile unsigned char *)(addr))
46#define REG16(addr) (*(volatile unsigned short *)(addr))
47#define REG32(addr) (*(volatile unsigned int *)(addr))
48
49#endif /* !ASSEMBLY */
50
51/*************************************************************************
52 * Boot ROM Specification
53 */
54
55/* NOR Boot config */
56#define JZ4740_NORBOOT_8BIT 0x00000000 /* 8-bit data bus flash */
57#define JZ4740_NORBOOT_16BIT 0x10101010 /* 16-bit data bus flash */
58#define JZ4740_NORBOOT_32BIT 0x20202020 /* 32-bit data bus flash */
59
60/* NAND Boot config */
61#define JZ4740_NANDBOOT_B8R3 0xffffffff /* 8-bit bus & 3 row cycles */
62#define JZ4740_NANDBOOT_B8R2 0xf0f0f0f0 /* 8-bit bus & 2 row cycles */
63#define JZ4740_NANDBOOT_B16R3 0x0f0f0f0f /* 16-bit bus & 3 row cycles */
64#define JZ4740_NANDBOOT_B16R2 0x00000000 /* 16-bit bus & 2 row cycles */
65
66
67/*************************************************************************
68 * Register Definitions
69 */
70#define CPM_BASE 0xB0000000
71#define INTC_BASE 0xB0001000
72#define TCU_BASE 0xB0002000
73#define WDT_BASE 0xB0002000
74#define RTC_BASE 0xB0003000
75#define GPIO_BASE 0xB0010000
76#define AIC_BASE 0xB0020000
77#define ICDC_BASE 0xB0020000
78#define MSC_BASE 0xB0021000
79#define UART0_BASE 0xB0030000
80#define I2C_BASE 0xB0042000
81#define SSI_BASE 0xB0043000
82#define SADC_BASE 0xB0070000
83#define EMC_BASE 0xB3010000
84#define DMAC_BASE 0xB3020000
85#define UHC_BASE 0xB3030000
86#define UDC_BASE 0xB3040000
87#define LCD_BASE 0xB3050000
88#define SLCD_BASE 0xB3050000
89#define CIM_BASE 0xB3060000
90#define ETH_BASE 0xB3100000
91
92
93/*************************************************************************
94 * INTC (Interrupt Controller)
95 *************************************************************************/
96#define INTC_ISR (INTC_BASE + 0x00)
97#define INTC_IMR (INTC_BASE + 0x04)
98#define INTC_IMSR (INTC_BASE + 0x08)
99#define INTC_IMCR (INTC_BASE + 0x0c)
100#define INTC_IPR (INTC_BASE + 0x10)
101
102#define REG_INTC_ISR REG32(INTC_ISR)
103#define REG_INTC_IMR REG32(INTC_IMR)
104#define REG_INTC_IMSR REG32(INTC_IMSR)
105#define REG_INTC_IMCR REG32(INTC_IMCR)
106#define REG_INTC_IPR REG32(INTC_IPR)
107
108// 1st-level interrupts
109#define IRQ_I2C 1
110#define IRQ_EMC 2
111#define IRQ_UHC 3
112#define IRQ_UART0 9
113#define IRQ_SADC 12
114#define IRQ_MSC 14
115#define IRQ_RTC 15
116#define IRQ_SSI 16
117#define IRQ_CIM 17
118#define IRQ_AIC 18
119#define IRQ_ETH 19
120#define IRQ_DMAC 20
121#define IRQ_TCU2 21
122#define IRQ_TCU1 22
123#define IRQ_TCU0 23
124#define IRQ_UDC 24
125#define IRQ_GPIO3 25
126#define IRQ_GPIO2 26
127#define IRQ_GPIO1 27
128#define IRQ_GPIO0 28
129#define IRQ_IPU 29
130#define IRQ_LCD 30
131
132// 2nd-level interrupts
133#define IRQ_DMA_0 32 /* 32 to 37 for DMAC channel 0 to 5 */
134#define IRQ_GPIO_0 48 /* 48 to 175 for GPIO pin 0 to 127 */
135
136
137/*************************************************************************
138 * RTC
139 *************************************************************************/
140#define RTC_RCR (RTC_BASE + 0x00) /* RTC Control Register */
141#define RTC_RSR (RTC_BASE + 0x04) /* RTC Second Register */
142#define RTC_RSAR (RTC_BASE + 0x08) /* RTC Second Alarm Register */
143#define RTC_RGR (RTC_BASE + 0x0c) /* RTC Regulator Register */
144
145#define RTC_HCR (RTC_BASE + 0x20) /* Hibernate Control Register */
146#define RTC_HWFCR (RTC_BASE + 0x24) /* Hibernate Wakeup Filter Counter Reg */
147#define RTC_HRCR (RTC_BASE + 0x28) /* Hibernate Reset Counter Register */
148#define RTC_HWCR (RTC_BASE + 0x2c) /* Hibernate Wakeup Control Register */
149#define RTC_HWRSR (RTC_BASE + 0x30) /* Hibernate Wakeup Status Register */
150#define RTC_HSPR (RTC_BASE + 0x34) /* Hibernate Scratch Pattern Register */
151
152#define REG_RTC_RCR REG32(RTC_RCR)
153#define REG_RTC_RSR REG32(RTC_RSR)
154#define REG_RTC_RSAR REG32(RTC_RSAR)
155#define REG_RTC_RGR REG32(RTC_RGR)
156#define REG_RTC_HCR REG32(RTC_HCR)
157#define REG_RTC_HWFCR REG32(RTC_HWFCR)
158#define REG_RTC_HRCR REG32(RTC_HRCR)
159#define REG_RTC_HWCR REG32(RTC_HWCR)
160#define REG_RTC_HWRSR REG32(RTC_HWRSR)
161#define REG_RTC_HSPR REG32(RTC_HSPR)
162
163/* RTC Control Register */
164#define RTC_RCR_WRDY (1 << 7) /* Write Ready Flag */
165#define RTC_RCR_HZ (1 << 6) /* 1Hz Flag */
166#define RTC_RCR_HZIE (1 << 5) /* 1Hz Interrupt Enable */
167#define RTC_RCR_AF (1 << 4) /* Alarm Flag */
168#define RTC_RCR_AF_BIT 4 /* Alarm Flag */
169#define RTC_RCR_AIE (1 << 3) /* Alarm Interrupt Enable */
170#define RTC_RCR_AE (1 << 2) /* Alarm Enable */
171#define RTC_RCR_RTCE (1 << 0) /* RTC Enable */
172
173/* RTC Regulator Register */
174#define RTC_RGR_LOCK (1 << 31) /* Lock Bit */
175#define RTC_RGR_ADJC_BIT 16
176#define RTC_RGR_ADJC_MASK (0x3ff << RTC_RGR_ADJC_BIT)
177#define RTC_RGR_NC1HZ_BIT 0
178#define RTC_RGR_NC1HZ_MASK (0xffff << RTC_RGR_NC1HZ_BIT)
179
180/* Hibernate Control Register */
181#define RTC_HCR_PD (1 << 0) /* Power Down */
182
183/* Hibernate Wakeup Filter Counter Register */
184#define RTC_HWFCR_BIT 5
185#define RTC_HWFCR_MASK (0x7ff << RTC_HWFCR_BIT)
186
187/* Hibernate Reset Counter Register */
188#define RTC_HRCR_BIT 5
189#define RTC_HRCR_MASK (0x7f << RTC_HRCR_BIT)
190
191/* Hibernate Wakeup Control Register */
192#define RTC_HWCR_EALM (1 << 0) /* RTC alarm wakeup enable */
193
194/* Hibernate Wakeup Status Register */
195#define RTC_HWRSR_HR (1 << 5) /* Hibernate reset */
196#define RTC_HWRSR_PPR (1 << 4) /* PPR reset */
197#define RTC_HWRSR_PIN (1 << 1) /* Wakeup pin status bit */
198#define RTC_HWRSR_ALM (1 << 0) /* RTC alarm status bit */
199
200
201/*************************************************************************
202 * CPM (Clock reset and Power control Management)
203 *************************************************************************/
204#define CPM_CPCCR (CPM_BASE+0x00)
205#define CPM_CPPCR (CPM_BASE+0x10)
206#define CPM_I2SCDR (CPM_BASE+0x60)
207#define CPM_LPCDR (CPM_BASE+0x64)
208#define CPM_MSCCDR (CPM_BASE+0x68)
209#define CPM_UHCCDR (CPM_BASE+0x6C)
210
211#define CPM_LCR (CPM_BASE+0x04)
212#define CPM_CLKGR (CPM_BASE+0x20)
213#define CPM_SCR (CPM_BASE+0x24)
214
215#define CPM_HCR (CPM_BASE+0x30)
216#define CPM_HWFCR (CPM_BASE+0x34)
217#define CPM_HRCR (CPM_BASE+0x38)
218#define CPM_HWCR (CPM_BASE+0x3c)
219#define CPM_HWSR (CPM_BASE+0x40)
220#define CPM_HSPR (CPM_BASE+0x44)
221
222#define CPM_RSR (CPM_BASE+0x08)
223
224
225#define REG_CPM_CPCCR REG32(CPM_CPCCR)
226#define REG_CPM_CPPCR REG32(CPM_CPPCR)
227#define REG_CPM_I2SCDR REG32(CPM_I2SCDR)
228#define REG_CPM_LPCDR REG32(CPM_LPCDR)
229#define REG_CPM_MSCCDR REG32(CPM_MSCCDR)
230#define REG_CPM_UHCCDR REG32(CPM_UHCCDR)
231
232#define REG_CPM_LCR REG32(CPM_LCR)
233#define REG_CPM_CLKGR REG32(CPM_CLKGR)
234#define REG_CPM_SCR REG32(CPM_SCR)
235#define REG_CPM_HCR REG32(CPM_HCR)
236#define REG_CPM_HWFCR REG32(CPM_HWFCR)
237#define REG_CPM_HRCR REG32(CPM_HRCR)
238#define REG_CPM_HWCR REG32(CPM_HWCR)
239#define REG_CPM_HWSR REG32(CPM_HWSR)
240#define REG_CPM_HSPR REG32(CPM_HSPR)
241
242#define REG_CPM_RSR REG32(CPM_RSR)
243
244
245/* Clock Control Register */
246#define CPM_CPCCR_I2CS (1 << 31)
247#define CPM_CPCCR_CLKOEN (1 << 30)
248#define CPM_CPCCR_UCS (1 << 29)
249#define CPM_CPCCR_UDIV_BIT 23
250#define CPM_CPCCR_UDIV_MASK (0x3f << CPM_CPCCR_UDIV_BIT)
251#define CPM_CPCCR_CE (1 << 22)
252#define CPM_CPCCR_PCS (1 << 21)
253#define CPM_CPCCR_LDIV_BIT 16
254#define CPM_CPCCR_LDIV_MASK (0x1f << CPM_CPCCR_LDIV_BIT)
255#define CPM_CPCCR_MDIV_BIT 12
256#define CPM_CPCCR_MDIV_MASK (0x0f << CPM_CPCCR_MDIV_BIT)
257#define CPM_CPCCR_PDIV_BIT 8
258#define CPM_CPCCR_PDIV_MASK (0x0f << CPM_CPCCR_PDIV_BIT)
259#define CPM_CPCCR_HDIV_BIT 4
260#define CPM_CPCCR_HDIV_MASK (0x0f << CPM_CPCCR_HDIV_BIT)
261#define CPM_CPCCR_CDIV_BIT 0
262#define CPM_CPCCR_CDIV_MASK (0x0f << CPM_CPCCR_CDIV_BIT)
263
264/* I2S Clock Divider Register */
265#define CPM_I2SCDR_I2SDIV_BIT 0
266#define CPM_I2SCDR_I2SDIV_MASK (0x1ff << CPM_I2SCDR_I2SDIV_BIT)
267
268/* LCD Pixel Clock Divider Register */
269#define CPM_LPCDR_PIXDIV_BIT 0
270#define CPM_LPCDR_PIXDIV_MASK (0x1ff << CPM_LPCDR_PIXDIV_BIT)
271
272/* MSC Clock Divider Register */
273#define CPM_MSCCDR_MSCDIV_BIT 0
274#define CPM_MSCCDR_MSCDIV_MASK (0x1f << CPM_MSCCDR_MSCDIV_BIT)
275
276/* PLL Control Register */
277#define CPM_CPPCR_PLLM_BIT 23
278#define CPM_CPPCR_PLLM_MASK (0x1ff << CPM_CPPCR_PLLM_BIT)
279#define CPM_CPPCR_PLLN_BIT 18
280#define CPM_CPPCR_PLLN_MASK (0x1f << CPM_CPPCR_PLLN_BIT)
281#define CPM_CPPCR_PLLOD_BIT 16
282#define CPM_CPPCR_PLLOD_MASK (0x03 << CPM_CPPCR_PLLOD_BIT)
283#define CPM_CPPCR_PLLS (1 << 10)
284#define CPM_CPPCR_PLLBP (1 << 9)
285#define CPM_CPPCR_PLLEN (1 << 8)
286#define CPM_CPPCR_PLLST_BIT 0
287#define CPM_CPPCR_PLLST_MASK (0xff << CPM_CPPCR_PLLST_BIT)
288
289/* Low Power Control Register */
290#define CPM_LCR_DOZE_DUTY_BIT 3
291#define CPM_LCR_DOZE_DUTY_MASK (0x1f << CPM_LCR_DOZE_DUTY_BIT)
292#define CPM_LCR_DOZE_ON (1 << 2)
293#define CPM_LCR_LPM_BIT 0
294#define CPM_LCR_LPM_MASK (0x3 << CPM_LCR_LPM_BIT)
295  #define CPM_LCR_LPM_IDLE (0x0 << CPM_LCR_LPM_BIT)
296  #define CPM_LCR_LPM_SLEEP (0x1 << CPM_LCR_LPM_BIT)
297
298/* Clock Gate Register */
299#define CPM_CLKGR_UART1 (1 << 15)
300#define CPM_CLKGR_UHC (1 << 14)
301#define CPM_CLKGR_IPU (1 << 13)
302#define CPM_CLKGR_DMAC (1 << 12)
303#define CPM_CLKGR_UDC (1 << 11)
304#define CPM_CLKGR_LCD (1 << 10)
305#define CPM_CLKGR_CIM (1 << 9)
306#define CPM_CLKGR_SADC (1 << 8)
307#define CPM_CLKGR_MSC (1 << 7)
308#define CPM_CLKGR_AIC1 (1 << 6)
309#define CPM_CLKGR_AIC2 (1 << 5)
310#define CPM_CLKGR_SSI (1 << 4)
311#define CPM_CLKGR_I2C (1 << 3)
312#define CPM_CLKGR_RTC (1 << 2)
313#define CPM_CLKGR_TCU (1 << 1)
314#define CPM_CLKGR_UART0 (1 << 0)
315
316/* Sleep Control Register */
317#define CPM_SCR_O1ST_BIT 8
318#define CPM_SCR_O1ST_MASK (0xff << CPM_SCR_O1ST_BIT)
319#define CPM_SCR_USBHOST_SUSPEND (1 << 7)
320#define CPM_SCR_USBPHY_ENABLE (1 << 6)
321#define CPM_SCR_OSC_ENABLE (1 << 4)
322
323/* Hibernate Control Register */
324#define CPM_HCR_PD (1 << 0)
325
326/* Wakeup Filter Counter Register in Hibernate Mode */
327#define CPM_HWFCR_TIME_BIT 0
328#define CPM_HWFCR_TIME_MASK (0x3ff << CPM_HWFCR_TIME_BIT)
329
330/* Reset Counter Register in Hibernate Mode */
331#define CPM_HRCR_TIME_BIT 0
332#define CPM_HRCR_TIME_MASK (0x7f << CPM_HRCR_TIME_BIT)
333
334/* Wakeup Control Register in Hibernate Mode */
335#define CPM_HWCR_WLE_LOW (0 << 2)
336#define CPM_HWCR_WLE_HIGH (1 << 2)
337#define CPM_HWCR_PIN_WAKEUP (1 << 1)
338#define CPM_HWCR_RTC_WAKEUP (1 << 0)
339
340/* Wakeup Status Register in Hibernate Mode */
341#define CPM_HWSR_WSR_PIN (1 << 1)
342#define CPM_HWSR_WSR_RTC (1 << 0)
343
344/* Reset Status Register */
345#define CPM_RSR_HR (1 << 2)
346#define CPM_RSR_WR (1 << 1)
347#define CPM_RSR_PR (1 << 0)
348
349
350/*************************************************************************
351 * TCU (Timer Counter Unit)
352 *************************************************************************/
353#define TCU_TSR (TCU_BASE + 0x1C) /* Timer Stop Register */
354#define TCU_TSSR (TCU_BASE + 0x2C) /* Timer Stop Set Register */
355#define TCU_TSCR (TCU_BASE + 0x3C) /* Timer Stop Clear Register */
356#define TCU_TER (TCU_BASE + 0x10) /* Timer Counter Enable Register */
357#define TCU_TESR (TCU_BASE + 0x14) /* Timer Counter Enable Set Register */
358#define TCU_TECR (TCU_BASE + 0x18) /* Timer Counter Enable Clear Register */
359#define TCU_TFR (TCU_BASE + 0x20) /* Timer Flag Register */
360#define TCU_TFSR (TCU_BASE + 0x24) /* Timer Flag Set Register */
361#define TCU_TFCR (TCU_BASE + 0x28) /* Timer Flag Clear Register */
362#define TCU_TMR (TCU_BASE + 0x30) /* Timer Mask Register */
363#define TCU_TMSR (TCU_BASE + 0x34) /* Timer Mask Set Register */
364#define TCU_TMCR (TCU_BASE + 0x38) /* Timer Mask Clear Register */
365#define TCU_TDFR0 (TCU_BASE + 0x40) /* Timer Data Full Register */
366#define TCU_TDHR0 (TCU_BASE + 0x44) /* Timer Data Half Register */
367#define TCU_TCNT0 (TCU_BASE + 0x48) /* Timer Counter Register */
368#define TCU_TCSR0 (TCU_BASE + 0x4C) /* Timer Control Register */
369#define TCU_TDFR1 (TCU_BASE + 0x50)
370#define TCU_TDHR1 (TCU_BASE + 0x54)
371#define TCU_TCNT1 (TCU_BASE + 0x58)
372#define TCU_TCSR1 (TCU_BASE + 0x5C)
373#define TCU_TDFR2 (TCU_BASE + 0x60)
374#define TCU_TDHR2 (TCU_BASE + 0x64)
375#define TCU_TCNT2 (TCU_BASE + 0x68)
376#define TCU_TCSR2 (TCU_BASE + 0x6C)
377#define TCU_TDFR3 (TCU_BASE + 0x70)
378#define TCU_TDHR3 (TCU_BASE + 0x74)
379#define TCU_TCNT3 (TCU_BASE + 0x78)
380#define TCU_TCSR3 (TCU_BASE + 0x7C)
381#define TCU_TDFR4 (TCU_BASE + 0x80)
382#define TCU_TDHR4 (TCU_BASE + 0x84)
383#define TCU_TCNT4 (TCU_BASE + 0x88)
384#define TCU_TCSR4 (TCU_BASE + 0x8C)
385#define TCU_TDFR5 (TCU_BASE + 0x90)
386#define TCU_TDHR5 (TCU_BASE + 0x94)
387#define TCU_TCNT5 (TCU_BASE + 0x98)
388#define TCU_TCSR5 (TCU_BASE + 0x9C)
389
390#define REG_TCU_TSR REG32(TCU_TSR)
391#define REG_TCU_TSSR REG32(TCU_TSSR)
392#define REG_TCU_TSCR REG32(TCU_TSCR)
393#define REG_TCU_TER REG8(TCU_TER)
394#define REG_TCU_TESR REG8(TCU_TESR)
395#define REG_TCU_TECR REG8(TCU_TECR)
396#define REG_TCU_TFR REG32(TCU_TFR)
397#define REG_TCU_TFSR REG32(TCU_TFSR)
398#define REG_TCU_TFCR REG32(TCU_TFCR)
399#define REG_TCU_TMR REG32(TCU_TMR)
400#define REG_TCU_TMSR REG32(TCU_TMSR)
401#define REG_TCU_TMCR REG32(TCU_TMCR)
402#define REG_TCU_TDFR0 REG16(TCU_TDFR0)
403#define REG_TCU_TDHR0 REG16(TCU_TDHR0)
404#define REG_TCU_TCNT0 REG16(TCU_TCNT0)
405#define REG_TCU_TCSR0 REG16(TCU_TCSR0)
406#define REG_TCU_TDFR1 REG16(TCU_TDFR1)
407#define REG_TCU_TDHR1 REG16(TCU_TDHR1)
408#define REG_TCU_TCNT1 REG16(TCU_TCNT1)
409#define REG_TCU_TCSR1 REG16(TCU_TCSR1)
410#define REG_TCU_TDFR2 REG16(TCU_TDFR2)
411#define REG_TCU_TDHR2 REG16(TCU_TDHR2)
412#define REG_TCU_TCNT2 REG16(TCU_TCNT2)
413#define REG_TCU_TCSR2 REG16(TCU_TCSR2)
414#define REG_TCU_TDFR3 REG16(TCU_TDFR3)
415#define REG_TCU_TDHR3 REG16(TCU_TDHR3)
416#define REG_TCU_TCNT3 REG16(TCU_TCNT3)
417#define REG_TCU_TCSR3 REG16(TCU_TCSR3)
418#define REG_TCU_TDFR4 REG16(TCU_TDFR4)
419#define REG_TCU_TDHR4 REG16(TCU_TDHR4)
420#define REG_TCU_TCNT4 REG16(TCU_TCNT4)
421#define REG_TCU_TCSR4 REG16(TCU_TCSR4)
422
423// n = 0,1,2,3,4,5,6,7
424#define TCU_TDFR(n) (TCU_BASE + (0x40 + (n)*0x10)) /* Timer Data Full Reg */
425#define TCU_TDHR(n) (TCU_BASE + (0x44 + (n)*0x10)) /* Timer Data Half Reg */
426#define TCU_TCNT(n) (TCU_BASE + (0x48 + (n)*0x10)) /* Timer Counter Reg */
427#define TCU_TCSR(n) (TCU_BASE + (0x4C + (n)*0x10)) /* Timer Control Reg */
428
429#define REG_TCU_TDFR(n) REG16(TCU_TDFR((n)))
430#define REG_TCU_TDHR(n) REG16(TCU_TDHR((n)))
431#define REG_TCU_TCNT(n) REG16(TCU_TCNT((n)))
432#define REG_TCU_TCSR(n) REG16(TCU_TCSR((n)))
433
434// Register definitions
435#define TCU_TCSR_PWM_SD (1 << 9)
436#define TCU_TCSR_PWM_INITL_HIGH (1 << 8)
437#define TCU_TCSR_PWM_EN (1 << 7)
438#define TCU_TCSR_PRESCALE_BIT 3
439#define TCU_TCSR_PRESCALE_MASK (0x7 << TCU_TCSR_PRESCALE_BIT)
440  #define TCU_TCSR_PRESCALE1 (0x0 << TCU_TCSR_PRESCALE_BIT)
441  #define TCU_TCSR_PRESCALE4 (0x1 << TCU_TCSR_PRESCALE_BIT)
442  #define TCU_TCSR_PRESCALE16 (0x2 << TCU_TCSR_PRESCALE_BIT)
443  #define TCU_TCSR_PRESCALE64 (0x3 << TCU_TCSR_PRESCALE_BIT)
444  #define TCU_TCSR_PRESCALE256 (0x4 << TCU_TCSR_PRESCALE_BIT)
445  #define TCU_TCSR_PRESCALE1024 (0x5 << TCU_TCSR_PRESCALE_BIT)
446#define TCU_TCSR_EXT_EN (1 << 2)
447#define TCU_TCSR_RTC_EN (1 << 1)
448#define TCU_TCSR_PCK_EN (1 << 0)
449
450#define TCU_TER_TCEN5 (1 << 5)
451#define TCU_TER_TCEN4 (1 << 4)
452#define TCU_TER_TCEN3 (1 << 3)
453#define TCU_TER_TCEN2 (1 << 2)
454#define TCU_TER_TCEN1 (1 << 1)
455#define TCU_TER_TCEN0 (1 << 0)
456
457#define TCU_TESR_TCST5 (1 << 5)
458#define TCU_TESR_TCST4 (1 << 4)
459#define TCU_TESR_TCST3 (1 << 3)
460#define TCU_TESR_TCST2 (1 << 2)
461#define TCU_TESR_TCST1 (1 << 1)
462#define TCU_TESR_TCST0 (1 << 0)
463
464#define TCU_TECR_TCCL5 (1 << 5)
465#define TCU_TECR_TCCL4 (1 << 4)
466#define TCU_TECR_TCCL3 (1 << 3)
467#define TCU_TECR_TCCL2 (1 << 2)
468#define TCU_TECR_TCCL1 (1 << 1)
469#define TCU_TECR_TCCL0 (1 << 0)
470
471#define TCU_TFR_HFLAG5 (1 << 21)
472#define TCU_TFR_HFLAG4 (1 << 20)
473#define TCU_TFR_HFLAG3 (1 << 19)
474#define TCU_TFR_HFLAG2 (1 << 18)
475#define TCU_TFR_HFLAG1 (1 << 17)
476#define TCU_TFR_HFLAG0 (1 << 16)
477#define TCU_TFR_FFLAG5 (1 << 5)
478#define TCU_TFR_FFLAG4 (1 << 4)
479#define TCU_TFR_FFLAG3 (1 << 3)
480#define TCU_TFR_FFLAG2 (1 << 2)
481#define TCU_TFR_FFLAG1 (1 << 1)
482#define TCU_TFR_FFLAG0 (1 << 0)
483
484#define TCU_TFSR_HFLAG5 (1 << 21)
485#define TCU_TFSR_HFLAG4 (1 << 20)
486#define TCU_TFSR_HFLAG3 (1 << 19)
487#define TCU_TFSR_HFLAG2 (1 << 18)
488#define TCU_TFSR_HFLAG1 (1 << 17)
489#define TCU_TFSR_HFLAG0 (1 << 16)
490#define TCU_TFSR_FFLAG5 (1 << 5)
491#define TCU_TFSR_FFLAG4 (1 << 4)
492#define TCU_TFSR_FFLAG3 (1 << 3)
493#define TCU_TFSR_FFLAG2 (1 << 2)
494#define TCU_TFSR_FFLAG1 (1 << 1)
495#define TCU_TFSR_FFLAG0 (1 << 0)
496
497#define TCU_TFCR_HFLAG5 (1 << 21)
498#define TCU_TFCR_HFLAG4 (1 << 20)
499#define TCU_TFCR_HFLAG3 (1 << 19)
500#define TCU_TFCR_HFLAG2 (1 << 18)
501#define TCU_TFCR_HFLAG1 (1 << 17)
502#define TCU_TFCR_HFLAG0 (1 << 16)
503#define TCU_TFCR_FFLAG5 (1 << 5)
504#define TCU_TFCR_FFLAG4 (1 << 4)
505#define TCU_TFCR_FFLAG3 (1 << 3)
506#define TCU_TFCR_FFLAG2 (1 << 2)
507#define TCU_TFCR_FFLAG1 (1 << 1)
508#define TCU_TFCR_FFLAG0 (1 << 0)
509
510#define TCU_TMR_HMASK5 (1 << 21)
511#define TCU_TMR_HMASK4 (1 << 20)
512#define TCU_TMR_HMASK3 (1 << 19)
513#define TCU_TMR_HMASK2 (1 << 18)
514#define TCU_TMR_HMASK1 (1 << 17)
515#define TCU_TMR_HMASK0 (1 << 16)
516#define TCU_TMR_FMASK5 (1 << 5)
517#define TCU_TMR_FMASK4 (1 << 4)
518#define TCU_TMR_FMASK3 (1 << 3)
519#define TCU_TMR_FMASK2 (1 << 2)
520#define TCU_TMR_FMASK1 (1 << 1)
521#define TCU_TMR_FMASK0 (1 << 0)
522
523#define TCU_TMSR_HMST5 (1 << 21)
524#define TCU_TMSR_HMST4 (1 << 20)
525#define TCU_TMSR_HMST3 (1 << 19)
526#define TCU_TMSR_HMST2 (1 << 18)
527#define TCU_TMSR_HMST1 (1 << 17)
528#define TCU_TMSR_HMST0 (1 << 16)
529#define TCU_TMSR_FMST5 (1 << 5)
530#define TCU_TMSR_FMST4 (1 << 4)
531#define TCU_TMSR_FMST3 (1 << 3)
532#define TCU_TMSR_FMST2 (1 << 2)
533#define TCU_TMSR_FMST1 (1 << 1)
534#define TCU_TMSR_FMST0 (1 << 0)
535
536#define TCU_TMCR_HMCL5 (1 << 21)
537#define TCU_TMCR_HMCL4 (1 << 20)
538#define TCU_TMCR_HMCL3 (1 << 19)
539#define TCU_TMCR_HMCL2 (1 << 18)
540#define TCU_TMCR_HMCL1 (1 << 17)
541#define TCU_TMCR_HMCL0 (1 << 16)
542#define TCU_TMCR_FMCL5 (1 << 5)
543#define TCU_TMCR_FMCL4 (1 << 4)
544#define TCU_TMCR_FMCL3 (1 << 3)
545#define TCU_TMCR_FMCL2 (1 << 2)
546#define TCU_TMCR_FMCL1 (1 << 1)
547#define TCU_TMCR_FMCL0 (1 << 0)
548
549#define TCU_TSR_WDTS (1 << 16)
550#define TCU_TSR_STOP5 (1 << 5)
551#define TCU_TSR_STOP4 (1 << 4)
552#define TCU_TSR_STOP3 (1 << 3)
553#define TCU_TSR_STOP2 (1 << 2)
554#define TCU_TSR_STOP1 (1 << 1)
555#define TCU_TSR_STOP0 (1 << 0)
556
557#define TCU_TSSR_WDTSS (1 << 16)
558#define TCU_TSSR_STPS5 (1 << 5)
559#define TCU_TSSR_STPS4 (1 << 4)
560#define TCU_TSSR_STPS3 (1 << 3)
561#define TCU_TSSR_STPS2 (1 << 2)
562#define TCU_TSSR_STPS1 (1 << 1)
563#define TCU_TSSR_STPS0 (1 << 0)
564
565#define TCU_TSSR_WDTSC (1 << 16)
566#define TCU_TSSR_STPC5 (1 << 5)
567#define TCU_TSSR_STPC4 (1 << 4)
568#define TCU_TSSR_STPC3 (1 << 3)
569#define TCU_TSSR_STPC2 (1 << 2)
570#define TCU_TSSR_STPC1 (1 << 1)
571#define TCU_TSSR_STPC0 (1 << 0)
572
573
574/*************************************************************************
575 * WDT (WatchDog Timer)
576 *************************************************************************/
577#define WDT_TDR (WDT_BASE + 0x00)
578#define WDT_TCER (WDT_BASE + 0x04)
579#define WDT_TCNT (WDT_BASE + 0x08)
580#define WDT_TCSR (WDT_BASE + 0x0C)
581
582#define REG_WDT_TDR REG16(WDT_TDR)
583#define REG_WDT_TCER REG8(WDT_TCER)
584#define REG_WDT_TCNT REG16(WDT_TCNT)
585#define REG_WDT_TCSR REG16(WDT_TCSR)
586
587// Register definition
588#define WDT_TCSR_PRESCALE_BIT 3
589#define WDT_TCSR_PRESCALE_MASK (0x7 << WDT_TCSR_PRESCALE_BIT)
590  #define WDT_TCSR_PRESCALE1 (0x0 << WDT_TCSR_PRESCALE_BIT)
591  #define WDT_TCSR_PRESCALE4 (0x1 << WDT_TCSR_PRESCALE_BIT)
592  #define WDT_TCSR_PRESCALE16 (0x2 << WDT_TCSR_PRESCALE_BIT)
593  #define WDT_TCSR_PRESCALE64 (0x3 << WDT_TCSR_PRESCALE_BIT)
594  #define WDT_TCSR_PRESCALE256 (0x4 << WDT_TCSR_PRESCALE_BIT)
595  #define WDT_TCSR_PRESCALE1024 (0x5 << WDT_TCSR_PRESCALE_BIT)
596#define WDT_TCSR_EXT_EN (1 << 2)
597#define WDT_TCSR_RTC_EN (1 << 1)
598#define WDT_TCSR_PCK_EN (1 << 0)
599
600#define WDT_TCER_TCEN (1 << 0)
601
602
603/*************************************************************************
604 * DMAC (DMA Controller)
605 *************************************************************************/
606
607#define MAX_DMA_NUM 6 /* max 6 channels */
608
609#define DMAC_DSAR(n) (DMAC_BASE + (0x00 + (n) * 0x20)) /* DMA source address */
610#define DMAC_DTAR(n) (DMAC_BASE + (0x04 + (n) * 0x20)) /* DMA target address */
611#define DMAC_DTCR(n) (DMAC_BASE + (0x08 + (n) * 0x20)) /* DMA transfer count */
612#define DMAC_DRSR(n) (DMAC_BASE + (0x0c + (n) * 0x20)) /* DMA request source */
613#define DMAC_DCCSR(n) (DMAC_BASE + (0x10 + (n) * 0x20)) /* DMA control/status */
614#define DMAC_DCMD(n) (DMAC_BASE + (0x14 + (n) * 0x20)) /* DMA command */
615#define DMAC_DDA(n) (DMAC_BASE + (0x18 + (n) * 0x20)) /* DMA descriptor address */
616#define DMAC_DMACR (DMAC_BASE + 0x0300) /* DMA control register */
617#define DMAC_DMAIPR (DMAC_BASE + 0x0304) /* DMA interrupt pending */
618#define DMAC_DMADBR (DMAC_BASE + 0x0308) /* DMA doorbell */
619#define DMAC_DMADBSR (DMAC_BASE + 0x030C) /* DMA doorbell set */
620
621// channel 0
622#define DMAC_DSAR0 DMAC_DSAR(0)
623#define DMAC_DTAR0 DMAC_DTAR(0)
624#define DMAC_DTCR0 DMAC_DTCR(0)
625#define DMAC_DRSR0 DMAC_DRSR(0)
626#define DMAC_DCCSR0 DMAC_DCCSR(0)
627#define DMAC_DCMD0 DMAC_DCMD(0)
628#define DMAC_DDA0 DMAC_DDA(0)
629
630// channel 1
631#define DMAC_DSAR1 DMAC_DSAR(1)
632#define DMAC_DTAR1 DMAC_DTAR(1)
633#define DMAC_DTCR1 DMAC_DTCR(1)
634#define DMAC_DRSR1 DMAC_DRSR(1)
635#define DMAC_DCCSR1 DMAC_DCCSR(1)
636#define DMAC_DCMD1 DMAC_DCMD(1)
637#define DMAC_DDA1 DMAC_DDA(1)
638
639// channel 2
640#define DMAC_DSAR2 DMAC_DSAR(2)
641#define DMAC_DTAR2 DMAC_DTAR(2)
642#define DMAC_DTCR2 DMAC_DTCR(2)
643#define DMAC_DRSR2 DMAC_DRSR(2)
644#define DMAC_DCCSR2 DMAC_DCCSR(2)
645#define DMAC_DCMD2 DMAC_DCMD(2)
646#define DMAC_DDA2 DMAC_DDA(2)
647
648// channel 3
649#define DMAC_DSAR3 DMAC_DSAR(3)
650#define DMAC_DTAR3 DMAC_DTAR(3)
651#define DMAC_DTCR3 DMAC_DTCR(3)
652#define DMAC_DRSR3 DMAC_DRSR(3)
653#define DMAC_DCCSR3 DMAC_DCCSR(3)
654#define DMAC_DCMD3 DMAC_DCMD(3)
655#define DMAC_DDA3 DMAC_DDA(3)
656
657// channel 4
658#define DMAC_DSAR4 DMAC_DSAR(4)
659#define DMAC_DTAR4 DMAC_DTAR(4)
660#define DMAC_DTCR4 DMAC_DTCR(4)
661#define DMAC_DRSR4 DMAC_DRSR(4)
662#define DMAC_DCCSR4 DMAC_DCCSR(4)
663#define DMAC_DCMD4 DMAC_DCMD(4)
664#define DMAC_DDA4 DMAC_DDA(4)
665
666// channel 5
667#define DMAC_DSAR5 DMAC_DSAR(5)
668#define DMAC_DTAR5 DMAC_DTAR(5)
669#define DMAC_DTCR5 DMAC_DTCR(5)
670#define DMAC_DRSR5 DMAC_DRSR(5)
671#define DMAC_DCCSR5 DMAC_DCCSR(5)
672#define DMAC_DCMD5 DMAC_DCMD(5)
673#define DMAC_DDA5 DMAC_DDA(5)
674
675#define REG_DMAC_DSAR(n) REG32(DMAC_DSAR((n)))
676#define REG_DMAC_DTAR(n) REG32(DMAC_DTAR((n)))
677#define REG_DMAC_DTCR(n) REG32(DMAC_DTCR((n)))
678#define REG_DMAC_DRSR(n) REG32(DMAC_DRSR((n)))
679#define REG_DMAC_DCCSR(n) REG32(DMAC_DCCSR((n)))
680#define REG_DMAC_DCMD(n) REG32(DMAC_DCMD((n)))
681#define REG_DMAC_DDA(n) REG32(DMAC_DDA((n)))
682#define REG_DMAC_DMACR REG32(DMAC_DMACR)
683#define REG_DMAC_DMAIPR REG32(DMAC_DMAIPR)
684#define REG_DMAC_DMADBR REG32(DMAC_DMADBR)
685#define REG_DMAC_DMADBSR REG32(DMAC_DMADBSR)
686
687// DMA request source register
688#define DMAC_DRSR_RS_BIT 0
689#define DMAC_DRSR_RS_MASK (0x1f << DMAC_DRSR_RS_BIT)
690  #define DMAC_DRSR_RS_AUTO (8 << DMAC_DRSR_RS_BIT)
691  #define DMAC_DRSR_RS_UART0OUT (20 << DMAC_DRSR_RS_BIT)
692  #define DMAC_DRSR_RS_UART0IN (21 << DMAC_DRSR_RS_BIT)
693  #define DMAC_DRSR_RS_SSIOUT (22 << DMAC_DRSR_RS_BIT)
694  #define DMAC_DRSR_RS_SSIIN (23 << DMAC_DRSR_RS_BIT)
695  #define DMAC_DRSR_RS_AICOUT (24 << DMAC_DRSR_RS_BIT)
696  #define DMAC_DRSR_RS_AICIN (25 << DMAC_DRSR_RS_BIT)
697  #define DMAC_DRSR_RS_MSCOUT (26 << DMAC_DRSR_RS_BIT)
698  #define DMAC_DRSR_RS_MSCIN (27 << DMAC_DRSR_RS_BIT)
699  #define DMAC_DRSR_RS_TCU (28 << DMAC_DRSR_RS_BIT)
700  #define DMAC_DRSR_RS_SADC (29 << DMAC_DRSR_RS_BIT)
701  #define DMAC_DRSR_RS_SLCD (30 << DMAC_DRSR_RS_BIT)
702
703// DMA channel control/status register
704#define DMAC_DCCSR_NDES (1 << 31) /* descriptor (0) or not (1) ? */
705#define DMAC_DCCSR_CDOA_BIT 16 /* copy of DMA offset address */
706#define DMAC_DCCSR_CDOA_MASK (0xff << DMAC_DCCSR_CDOA_BIT)
707#define DMAC_DCCSR_INV (1 << 6) /* descriptor invalid */
708#define DMAC_DCCSR_AR (1 << 4) /* address error */
709#define DMAC_DCCSR_TT (1 << 3) /* transfer terminated */
710#define DMAC_DCCSR_HLT (1 << 2) /* DMA halted */
711#define DMAC_DCCSR_CT (1 << 1) /* count terminated */
712#define DMAC_DCCSR_EN (1 << 0) /* channel enable bit */
713
714// DMA channel command register
715#define DMAC_DCMD_SAI (1 << 23) /* source address increment */
716#define DMAC_DCMD_DAI (1 << 22) /* dest address increment */
717#define DMAC_DCMD_RDIL_BIT 16 /* request detection interval length */
718#define DMAC_DCMD_RDIL_MASK (0x0f << DMAC_DCMD_RDIL_BIT)
719  #define DMAC_DCMD_RDIL_IGN (0 << DMAC_DCMD_RDIL_BIT)
720  #define DMAC_DCMD_RDIL_2 (1 << DMAC_DCMD_RDIL_BIT)
721  #define DMAC_DCMD_RDIL_4 (2 << DMAC_DCMD_RDIL_BIT)
722  #define DMAC_DCMD_RDIL_8 (3 << DMAC_DCMD_RDIL_BIT)
723  #define DMAC_DCMD_RDIL_12 (4 << DMAC_DCMD_RDIL_BIT)
724  #define DMAC_DCMD_RDIL_16 (5 << DMAC_DCMD_RDIL_BIT)
725  #define DMAC_DCMD_RDIL_20 (6 << DMAC_DCMD_RDIL_BIT)
726  #define DMAC_DCMD_RDIL_24 (7 << DMAC_DCMD_RDIL_BIT)
727  #define DMAC_DCMD_RDIL_28 (8 << DMAC_DCMD_RDIL_BIT)
728  #define DMAC_DCMD_RDIL_32 (9 << DMAC_DCMD_RDIL_BIT)
729  #define DMAC_DCMD_RDIL_48 (10 << DMAC_DCMD_RDIL_BIT)
730  #define DMAC_DCMD_RDIL_60 (11 << DMAC_DCMD_RDIL_BIT)
731  #define DMAC_DCMD_RDIL_64 (12 << DMAC_DCMD_RDIL_BIT)
732  #define DMAC_DCMD_RDIL_124 (13 << DMAC_DCMD_RDIL_BIT)
733  #define DMAC_DCMD_RDIL_128 (14 << DMAC_DCMD_RDIL_BIT)
734  #define DMAC_DCMD_RDIL_200 (15 << DMAC_DCMD_RDIL_BIT)
735#define DMAC_DCMD_SWDH_BIT 14 /* source port width */
736#define DMAC_DCMD_SWDH_MASK (0x03 << DMAC_DCMD_SWDH_BIT)
737  #define DMAC_DCMD_SWDH_32 (0 << DMAC_DCMD_SWDH_BIT)
738  #define DMAC_DCMD_SWDH_8 (1 << DMAC_DCMD_SWDH_BIT)
739  #define DMAC_DCMD_SWDH_16 (2 << DMAC_DCMD_SWDH_BIT)
740#define DMAC_DCMD_DWDH_BIT 12 /* dest port width */
741#define DMAC_DCMD_DWDH_MASK (0x03 << DMAC_DCMD_DWDH_BIT)
742  #define DMAC_DCMD_DWDH_32 (0 << DMAC_DCMD_DWDH_BIT)
743  #define DMAC_DCMD_DWDH_8 (1 << DMAC_DCMD_DWDH_BIT)
744  #define DMAC_DCMD_DWDH_16 (2 << DMAC_DCMD_DWDH_BIT)
745#define DMAC_DCMD_DS_BIT 8 /* transfer data size of a data unit */
746#define DMAC_DCMD_DS_MASK (0x07 << DMAC_DCMD_DS_BIT)
747  #define DMAC_DCMD_DS_32BIT (0 << DMAC_DCMD_DS_BIT)
748  #define DMAC_DCMD_DS_8BIT (1 << DMAC_DCMD_DS_BIT)
749  #define DMAC_DCMD_DS_16BIT (2 << DMAC_DCMD_DS_BIT)
750  #define DMAC_DCMD_DS_16BYTE (3 << DMAC_DCMD_DS_BIT)
751  #define DMAC_DCMD_DS_32BYTE (4 << DMAC_DCMD_DS_BIT)
752#define DMAC_DCMD_TM (1 << 7) /* transfer mode: 0-single 1-block */
753#define DMAC_DCMD_DES_V (1 << 4) /* descriptor valid flag */
754#define DMAC_DCMD_DES_VM (1 << 3) /* descriptor valid mask: 1:support V-bit */
755#define DMAC_DCMD_DES_VIE (1 << 2) /* DMA valid error interrupt enable */
756#define DMAC_DCMD_TIE (1 << 1) /* DMA transfer interrupt enable */
757#define DMAC_DCMD_LINK (1 << 0) /* descriptor link enable */
758
759// DMA descriptor address register
760#define DMAC_DDA_BASE_BIT 12 /* descriptor base address */
761#define DMAC_DDA_BASE_MASK (0x0fffff << DMAC_DDA_BASE_BIT)
762#define DMAC_DDA_OFFSET_BIT 4 /* descriptor offset address */
763#define DMAC_DDA_OFFSET_MASK (0x0ff << DMAC_DDA_OFFSET_BIT)
764
765// DMA control register
766#define DMAC_DMACR_PR_BIT 8 /* channel priority mode */
767#define DMAC_DMACR_PR_MASK (0x03 << DMAC_DMACR_PR_BIT)
768  #define DMAC_DMACR_PR_012345 (0 << DMAC_DMACR_PR_BIT)
769  #define DMAC_DMACR_PR_023145 (1 << DMAC_DMACR_PR_BIT)
770  #define DMAC_DMACR_PR_201345 (2 << DMAC_DMACR_PR_BIT)
771  #define DMAC_DMACR_PR_RR (3 << DMAC_DMACR_PR_BIT) /* round robin */
772#define DMAC_DMACR_HLT (1 << 3) /* DMA halt flag */
773#define DMAC_DMACR_AR (1 << 2) /* address error flag */
774#define DMAC_DMACR_DMAE (1 << 0) /* DMA enable bit */
775
776// DMA doorbell register
777#define DMAC_DMADBR_DB5 (1 << 5) /* doorbell for channel 5 */
778#define DMAC_DMADBR_DB4 (1 << 5) /* doorbell for channel 4 */
779#define DMAC_DMADBR_DB3 (1 << 5) /* doorbell for channel 3 */
780#define DMAC_DMADBR_DB2 (1 << 5) /* doorbell for channel 2 */
781#define DMAC_DMADBR_DB1 (1 << 5) /* doorbell for channel 1 */
782#define DMAC_DMADBR_DB0 (1 << 5) /* doorbell for channel 0 */
783
784// DMA doorbell set register
785#define DMAC_DMADBSR_DBS5 (1 << 5) /* enable doorbell for channel 5 */
786#define DMAC_DMADBSR_DBS4 (1 << 5) /* enable doorbell for channel 4 */
787#define DMAC_DMADBSR_DBS3 (1 << 5) /* enable doorbell for channel 3 */
788#define DMAC_DMADBSR_DBS2 (1 << 5) /* enable doorbell for channel 2 */
789#define DMAC_DMADBSR_DBS1 (1 << 5) /* enable doorbell for channel 1 */
790#define DMAC_DMADBSR_DBS0 (1 << 5) /* enable doorbell for channel 0 */
791
792// DMA interrupt pending register
793#define DMAC_DMAIPR_CIRQ5 (1 << 5) /* irq pending status for channel 5 */
794#define DMAC_DMAIPR_CIRQ4 (1 << 4) /* irq pending status for channel 4 */
795#define DMAC_DMAIPR_CIRQ3 (1 << 3) /* irq pending status for channel 3 */
796#define DMAC_DMAIPR_CIRQ2 (1 << 2) /* irq pending status for channel 2 */
797#define DMAC_DMAIPR_CIRQ1 (1 << 1) /* irq pending status for channel 1 */
798#define DMAC_DMAIPR_CIRQ0 (1 << 0) /* irq pending status for channel 0 */
799
800
801/*************************************************************************
802 * GPIO (General-Purpose I/O Ports)
803 *************************************************************************/
804#define MAX_GPIO_NUM 128
805
806//n = 0,1,2,3
807#define GPIO_PXPIN(n) (GPIO_BASE + (0x00 + (n)*0x100)) /* PIN Level Register */
808#define GPIO_PXDAT(n) (GPIO_BASE + (0x10 + (n)*0x100)) /* Port Data Register */
809#define GPIO_PXDATS(n) (GPIO_BASE + (0x14 + (n)*0x100)) /* Port Data Set Register */
810#define GPIO_PXDATC(n) (GPIO_BASE + (0x18 + (n)*0x100)) /* Port Data Clear Register */
811#define GPIO_PXIM(n) (GPIO_BASE + (0x20 + (n)*0x100)) /* Interrupt Mask Register */
812#define GPIO_PXIMS(n) (GPIO_BASE + (0x24 + (n)*0x100)) /* Interrupt Mask Set Reg */
813#define GPIO_PXIMC(n) (GPIO_BASE + (0x28 + (n)*0x100)) /* Interrupt Mask Clear Reg */
814#define GPIO_PXPE(n) (GPIO_BASE + (0x30 + (n)*0x100)) /* Pull Enable Register */
815#define GPIO_PXPES(n) (GPIO_BASE + (0x34 + (n)*0x100)) /* Pull Enable Set Reg. */
816#define GPIO_PXPEC(n) (GPIO_BASE + (0x38 + (n)*0x100)) /* Pull Enable Clear Reg. */
817#define GPIO_PXFUN(n) (GPIO_BASE + (0x40 + (n)*0x100)) /* Function Register */
818#define GPIO_PXFUNS(n) (GPIO_BASE + (0x44 + (n)*0x100)) /* Function Set Register */
819#define GPIO_PXFUNC(n) (GPIO_BASE + (0x48 + (n)*0x100)) /* Function Clear Register */
820#define GPIO_PXSEL(n) (GPIO_BASE + (0x50 + (n)*0x100)) /* Select Register */
821#define GPIO_PXSELS(n) (GPIO_BASE + (0x54 + (n)*0x100)) /* Select Set Register */
822#define GPIO_PXSELC(n) (GPIO_BASE + (0x58 + (n)*0x100)) /* Select Clear Register */
823#define GPIO_PXDIR(n) (GPIO_BASE + (0x60 + (n)*0x100)) /* Direction Register */
824#define GPIO_PXDIRS(n) (GPIO_BASE + (0x64 + (n)*0x100)) /* Direction Set Register */
825#define GPIO_PXDIRC(n) (GPIO_BASE + (0x68 + (n)*0x100)) /* Direction Clear Register */
826#define GPIO_PXTRG(n) (GPIO_BASE + (0x70 + (n)*0x100)) /* Trigger Register */
827#define GPIO_PXTRGS(n) (GPIO_BASE + (0x74 + (n)*0x100)) /* Trigger Set Register */
828#define GPIO_PXTRGC(n) (GPIO_BASE + (0x78 + (n)*0x100)) /* Trigger Set Register */
829#define GPIO_PXFLG(n) (GPIO_BASE + (0x80 + (n)*0x100)) /* Port Flag Register */
830#define GPIO_PXFLGC(n) (GPIO_BASE + (0x14 + (n)*0x100)) /* Port Flag clear Register */
831
832#define REG_GPIO_PXPIN(n) REG32(GPIO_PXPIN((n))) /* PIN level */
833#define REG_GPIO_PXDAT(n) REG32(GPIO_PXDAT((n))) /* 1: interrupt pending */
834#define REG_GPIO_PXDATS(n) REG32(GPIO_PXDATS((n)))
835#define REG_GPIO_PXDATC(n) REG32(GPIO_PXDATC((n)))
836#define REG_GPIO_PXIM(n) REG32(GPIO_PXIM((n))) /* 1: mask pin interrupt */
837#define REG_GPIO_PXIMS(n) REG32(GPIO_PXIMS((n)))
838#define REG_GPIO_PXIMC(n) REG32(GPIO_PXIMC((n)))
839#define REG_GPIO_PXPE(n) REG32(GPIO_PXPE((n))) /* 1: disable pull up/down */
840#define REG_GPIO_PXPES(n) REG32(GPIO_PXPES((n)))
841#define REG_GPIO_PXPEC(n) REG32(GPIO_PXPEC((n)))
842#define REG_GPIO_PXFUN(n) REG32(GPIO_PXFUN((n))) /* 0:GPIO or intr, 1:FUNC */
843#define REG_GPIO_PXFUNS(n) REG32(GPIO_PXFUNS((n)))
844#define REG_GPIO_PXFUNC(n) REG32(GPIO_PXFUNC((n)))
845#define REG_GPIO_PXSEL(n) REG32(GPIO_PXSEL((n))) /* 0:GPIO/Fun0,1:intr/fun1*/
846#define REG_GPIO_PXSELS(n) REG32(GPIO_PXSELS((n)))
847#define REG_GPIO_PXSELC(n) REG32(GPIO_PXSELC((n)))
848#define REG_GPIO_PXDIR(n) REG32(GPIO_PXDIR((n))) /* 0:input/low-level-trig/falling-edge-trig, 1:output/high-level-trig/rising-edge-trig */
849#define REG_GPIO_PXDIRS(n) REG32(GPIO_PXDIRS((n)))
850#define REG_GPIO_PXDIRC(n) REG32(GPIO_PXDIRC((n)))
851#define REG_GPIO_PXTRG(n) REG32(GPIO_PXTRG((n))) /* 0:level-trigger, 1:edge-trigger */
852#define REG_GPIO_PXTRGS(n) REG32(GPIO_PXTRGS((n)))
853#define REG_GPIO_PXTRGC(n) REG32(GPIO_PXTRGC((n)))
854#define REG_GPIO_PXFLG(n) REG32(GPIO_PXFLG((n))) /* interrupt flag */
855#define REG_GPIO_PXFLGC(n) REG32(GPIO_PXFLGC((n))) /* interrupt flag */
856
857
858/*************************************************************************
859 * UART
860 *************************************************************************/
861
862#define IRDA_BASE UART0_BASE
863#define UART_BASE UART0_BASE
864#define UART_OFF 0x1000
865
866/* Register Offset */
867#define OFF_RDR (0x00) /* R 8b H'xx */
868#define OFF_TDR (0x00) /* W 8b H'xx */
869#define OFF_DLLR (0x00) /* RW 8b H'00 */
870#define OFF_DLHR (0x04) /* RW 8b H'00 */
871#define OFF_IER (0x04) /* RW 8b H'00 */
872#define OFF_ISR (0x08) /* R 8b H'01 */
873#define OFF_FCR (0x08) /* W 8b H'00 */
874#define OFF_LCR (0x0C) /* RW 8b H'00 */
875#define OFF_MCR (0x10) /* RW 8b H'00 */
876#define OFF_LSR (0x14) /* R 8b H'00 */
877#define OFF_MSR (0x18) /* R 8b H'00 */
878#define OFF_SPR (0x1C) /* RW 8b H'00 */
879#define OFF_SIRCR (0x20) /* RW 8b H'00, UART0 */
880#define OFF_UMR (0x24) /* RW 8b H'00, UART M Register */
881#define OFF_UACR (0x28) /* RW 8b H'00, UART Add Cycle Register */
882
883/* Register Address */
884#define UART0_RDR (UART0_BASE + OFF_RDR)
885#define UART0_TDR (UART0_BASE + OFF_TDR)
886#define UART0_DLLR (UART0_BASE + OFF_DLLR)
887#define UART0_DLHR (UART0_BASE + OFF_DLHR)
888#define UART0_IER (UART0_BASE + OFF_IER)
889#define UART0_ISR (UART0_BASE + OFF_ISR)
890#define UART0_FCR (UART0_BASE + OFF_FCR)
891#define UART0_LCR (UART0_BASE + OFF_LCR)
892#define UART0_MCR (UART0_BASE + OFF_MCR)
893#define UART0_LSR (UART0_BASE + OFF_LSR)
894#define UART0_MSR (UART0_BASE + OFF_MSR)
895#define UART0_SPR (UART0_BASE + OFF_SPR)
896#define UART0_SIRCR (UART0_BASE + OFF_SIRCR)
897#define UART0_UMR (UART0_BASE + OFF_UMR)
898#define UART0_UACR (UART0_BASE + OFF_UACR)
899
900/*
901 * Define macros for UART_IER
902 * UART Interrupt Enable Register
903 */
904#define UART_IER_RIE (1 << 0) /* 0: receive fifo "full" interrupt disable */
905#define UART_IER_TIE (1 << 1) /* 0: transmit fifo "empty" interrupt disable */
906#define UART_IER_RLIE (1 << 2) /* 0: receive line status interrupt disable */
907#define UART_IER_MIE (1 << 3) /* 0: modem status interrupt disable */
908#define UART_IER_RTIE (1 << 4) /* 0: receive timeout interrupt disable */
909
910/*
911 * Define macros for UART_ISR
912 * UART Interrupt Status Register
913 */
914#define UART_ISR_IP (1 << 0) /* 0: interrupt is pending 1: no interrupt */
915#define UART_ISR_IID (7 << 1) /* Source of Interrupt */
916#define UART_ISR_IID_MSI (0 << 1) /* Modem status interrupt */
917#define UART_ISR_IID_THRI (1 << 1) /* Transmitter holding register empty */
918#define UART_ISR_IID_RDI (2 << 1) /* Receiver data interrupt */
919#define UART_ISR_IID_RLSI (3 << 1) /* Receiver line status interrupt */
920#define UART_ISR_FFMS (3 << 6) /* FIFO mode select, set when UART_FCR.FE is set to 1 */
921#define UART_ISR_FFMS_NO_FIFO (0 << 6)
922#define UART_ISR_FFMS_FIFO_MODE (3 << 6)
923
924/*
925 * Define macros for UART_FCR
926 * UART FIFO Control Register
927 */
928#define UART_FCR_FE (1 << 0) /* 0: non-FIFO mode 1: FIFO mode */
929#define UART_FCR_RFLS (1 << 1) /* write 1 to flush receive FIFO */
930#define UART_FCR_TFLS (1 << 2) /* write 1 to flush transmit FIFO */
931#define UART_FCR_DMS (1 << 3) /* 0: disable DMA mode */
932#define UART_FCR_UUE (1 << 4) /* 0: disable UART */
933#define UART_FCR_RTRG (3 << 6) /* Receive FIFO Data Trigger */
934#define UART_FCR_RTRG_1 (0 << 6)
935#define UART_FCR_RTRG_4 (1 << 6)
936#define UART_FCR_RTRG_8 (2 << 6)
937#define UART_FCR_RTRG_15 (3 << 6)
938
939/*
940 * Define macros for UART_LCR
941 * UART Line Control Register
942 */
943#define UART_LCR_WLEN (3 << 0) /* word length */
944#define UART_LCR_WLEN_5 (0 << 0)
945#define UART_LCR_WLEN_6 (1 << 0)
946#define UART_LCR_WLEN_7 (2 << 0)
947#define UART_LCR_WLEN_8 (3 << 0)
948#define UART_LCR_STOP (1 << 2) /* 0: 1 stop bit when word length is 5,6,7,8
949                       1: 1.5 stop bits when 5; 2 stop bits when 6,7,8 */
950#define UART_LCR_STOP_1 (0 << 2) /* 0: 1 stop bit when word length is 5,6,7,8
951                       1: 1.5 stop bits when 5; 2 stop bits when 6,7,8 */
952#define UART_LCR_STOP_2 (1 << 2) /* 0: 1 stop bit when word length is 5,6,7,8
953                       1: 1.5 stop bits when 5; 2 stop bits when 6,7,8 */
954
955#define UART_LCR_PE (1 << 3) /* 0: parity disable */
956#define UART_LCR_PROE (1 << 4) /* 0: even parity 1: odd parity */
957#define UART_LCR_SPAR (1 << 5) /* 0: sticky parity disable */
958#define UART_LCR_SBRK (1 << 6) /* write 0 normal, write 1 send break */
959#define UART_LCR_DLAB (1 << 7) /* 0: access UART_RDR/TDR/IER 1: access UART_DLLR/DLHR */
960
961/*
962 * Define macros for UART_LSR
963 * UART Line Status Register
964 */
965#define UART_LSR_DR (1 << 0) /* 0: receive FIFO is empty 1: receive data is ready */
966#define UART_LSR_ORER (1 << 1) /* 0: no overrun error */
967#define UART_LSR_PER (1 << 2) /* 0: no parity error */
968#define UART_LSR_FER (1 << 3) /* 0; no framing error */
969#define UART_LSR_BRK (1 << 4) /* 0: no break detected 1: receive a break signal */
970#define UART_LSR_TDRQ (1 << 5) /* 1: transmit FIFO half "empty" */
971#define UART_LSR_TEMT (1 << 6) /* 1: transmit FIFO and shift registers empty */
972#define UART_LSR_RFER (1 << 7) /* 0: no receive error 1: receive error in FIFO mode */
973
974/*
975 * Define macros for UART_MCR
976 * UART Modem Control Register
977 */
978#define UART_MCR_DTR (1 << 0) /* 0: DTR_ ouput high */
979#define UART_MCR_RTS (1 << 1) /* 0: RTS_ output high */
980#define UART_MCR_OUT1 (1 << 2) /* 0: UART_MSR.RI is set to 0 and RI_ input high */
981#define UART_MCR_OUT2 (1 << 3) /* 0: UART_MSR.DCD is set to 0 and DCD_ input high */
982#define UART_MCR_LOOP (1 << 4) /* 0: normal 1: loopback mode */
983#define UART_MCR_MCE (1 << 7) /* 0: modem function is disable */
984
985/*
986 * Define macros for UART_MSR
987 * UART Modem Status Register
988 */
989#define UART_MSR_DCTS (1 << 0) /* 0: no change on CTS_ pin since last read of UART_MSR */
990#define UART_MSR_DDSR (1 << 1) /* 0: no change on DSR_ pin since last read of UART_MSR */
991#define UART_MSR_DRI (1 << 2) /* 0: no change on RI_ pin since last read of UART_MSR */
992#define UART_MSR_DDCD (1 << 3) /* 0: no change on DCD_ pin since last read of UART_MSR */
993#define UART_MSR_CTS (1 << 4) /* 0: CTS_ pin is high */
994#define UART_MSR_DSR (1 << 5) /* 0: DSR_ pin is high */
995#define UART_MSR_RI (1 << 6) /* 0: RI_ pin is high */
996#define UART_MSR_DCD (1 << 7) /* 0: DCD_ pin is high */
997
998/*
999 * Define macros for SIRCR
1000 * Slow IrDA Control Register
1001 */
1002#define SIRCR_TSIRE (1 << 0) /* 0: transmitter is in UART mode 1: IrDA mode */
1003#define SIRCR_RSIRE (1 << 1) /* 0: receiver is in UART mode 1: IrDA mode */
1004#define SIRCR_TPWS (1 << 2) /* 0: transmit 0 pulse width is 3/16 of bit length
1005                       1: 0 pulse width is 1.6us for 115.2Kbps */
1006#define SIRCR_TXPL (1 << 3) /* 0: encoder generates a positive pulse for 0 */
1007#define SIRCR_RXPL (1 << 4) /* 0: decoder interprets positive pulse as 0 */
1008
1009
1010/*************************************************************************
1011 * AIC (AC97/I2S Controller)
1012 *************************************************************************/
1013#define AIC_FR (AIC_BASE + 0x000)
1014#define AIC_CR (AIC_BASE + 0x004)
1015#define AIC_ACCR1 (AIC_BASE + 0x008)
1016#define AIC_ACCR2 (AIC_BASE + 0x00C)
1017#define AIC_I2SCR (AIC_BASE + 0x010)
1018#define AIC_SR (AIC_BASE + 0x014)
1019#define AIC_ACSR (AIC_BASE + 0x018)
1020#define AIC_I2SSR (AIC_BASE + 0x01C)
1021#define AIC_ACCAR (AIC_BASE + 0x020)
1022#define AIC_ACCDR (AIC_BASE + 0x024)
1023#define AIC_ACSAR (AIC_BASE + 0x028)
1024#define AIC_ACSDR (AIC_BASE + 0x02C)
1025#define AIC_I2SDIV (AIC_BASE + 0x030)
1026#define AIC_DR (AIC_BASE + 0x034)
1027
1028#define REG_AIC_FR REG32(AIC_FR)
1029#define REG_AIC_CR REG32(AIC_CR)
1030#define REG_AIC_ACCR1 REG32(AIC_ACCR1)
1031#define REG_AIC_ACCR2 REG32(AIC_ACCR2)
1032#define REG_AIC_I2SCR REG32(AIC_I2SCR)
1033#define REG_AIC_SR REG32(AIC_SR)
1034#define REG_AIC_ACSR REG32(AIC_ACSR)
1035#define REG_AIC_I2SSR REG32(AIC_I2SSR)
1036#define REG_AIC_ACCAR REG32(AIC_ACCAR)
1037#define REG_AIC_ACCDR REG32(AIC_ACCDR)
1038#define REG_AIC_ACSAR REG32(AIC_ACSAR)
1039#define REG_AIC_ACSDR REG32(AIC_ACSDR)
1040#define REG_AIC_I2SDIV REG32(AIC_I2SDIV)
1041#define REG_AIC_DR REG32(AIC_DR)
1042
1043/* AIC Controller Configuration Register (AIC_FR) */
1044
1045#define AIC_FR_RFTH_BIT 12 /* Receive FIFO Threshold */
1046#define AIC_FR_RFTH_MASK (0xf << AIC_FR_RFTH_BIT)
1047#define AIC_FR_TFTH_BIT 8 /* Transmit FIFO Threshold */
1048#define AIC_FR_TFTH_MASK (0xf << AIC_FR_TFTH_BIT)
1049#define AIC_FR_ICDC (1 << 5) /* External(0) or Internal CODEC(1) */
1050#define AIC_FR_AUSEL (1 << 4) /* AC97(0) or I2S/MSB-justified(1) */
1051#define AIC_FR_RST (1 << 3) /* AIC registers reset */
1052#define AIC_FR_BCKD (1 << 2) /* I2S BIT_CLK direction, 0:input,1:output */
1053#define AIC_FR_SYNCD (1 << 1) /* I2S SYNC direction, 0:input,1:output */
1054#define AIC_FR_ENB (1 << 0) /* AIC enable bit */
1055
1056/* AIC Controller Common Control Register (AIC_CR) */
1057
1058#define AIC_CR_OSS_BIT 19 /* Output Sample Size from memory (AIC V2 only) */
1059#define AIC_CR_OSS_MASK (0x7 << AIC_CR_OSS_BIT)
1060  #define AIC_CR_OSS_8BIT (0x0 << AIC_CR_OSS_BIT)
1061  #define AIC_CR_OSS_16BIT (0x1 << AIC_CR_OSS_BIT)
1062  #define AIC_CR_OSS_18BIT (0x2 << AIC_CR_OSS_BIT)
1063  #define AIC_CR_OSS_20BIT (0x3 << AIC_CR_OSS_BIT)
1064  #define AIC_CR_OSS_24BIT (0x4 << AIC_CR_OSS_BIT)
1065#define AIC_CR_ISS_BIT 16 /* Input Sample Size from memory (AIC V2 only) */
1066#define AIC_CR_ISS_MASK (0x7 << AIC_CR_ISS_BIT)
1067  #define AIC_CR_ISS_8BIT (0x0 << AIC_CR_ISS_BIT)
1068  #define AIC_CR_ISS_16BIT (0x1 << AIC_CR_ISS_BIT)
1069  #define AIC_CR_ISS_18BIT (0x2 << AIC_CR_ISS_BIT)
1070  #define AIC_CR_ISS_20BIT (0x3 << AIC_CR_ISS_BIT)
1071  #define AIC_CR_ISS_24BIT (0x4 << AIC_CR_ISS_BIT)
1072#define AIC_CR_RDMS (1 << 15) /* Receive DMA enable */
1073#define AIC_CR_TDMS (1 << 14) /* Transmit DMA enable */
1074#define AIC_CR_M2S (1 << 11) /* Mono to Stereo enable */
1075#define AIC_CR_ENDSW (1 << 10) /* Endian switch enable */
1076#define AIC_CR_AVSTSU (1 << 9) /* Signed <-> Unsigned toggle enable */
1077#define AIC_CR_FLUSH (1 << 8) /* Flush FIFO */
1078#define AIC_CR_EROR (1 << 6) /* Enable ROR interrupt */
1079#define AIC_CR_ETUR (1 << 5) /* Enable TUR interrupt */
1080#define AIC_CR_ERFS (1 << 4) /* Enable RFS interrupt */
1081#define AIC_CR_ETFS (1 << 3) /* Enable TFS interrupt */
1082#define AIC_CR_ENLBF (1 << 2) /* Enable Loopback Function */
1083#define AIC_CR_ERPL (1 << 1) /* Enable Playback Function */
1084#define AIC_CR_EREC (1 << 0) /* Enable Record Function */
1085
1086/* AIC Controller AC-link Control Register 1 (AIC_ACCR1) */
1087
1088#define AIC_ACCR1_RS_BIT 16 /* Receive Valid Slots */
1089#define AIC_ACCR1_RS_MASK (0x3ff << AIC_ACCR1_RS_BIT)
1090  #define AIC_ACCR1_RS_SLOT12 (1 << 25) /* Slot 12 valid bit */
1091  #define AIC_ACCR1_RS_SLOT11 (1 << 24) /* Slot 11 valid bit */
1092  #define AIC_ACCR1_RS_SLOT10 (1 << 23) /* Slot 10 valid bit */
1093  #define AIC_ACCR1_RS_SLOT9 (1 << 22) /* Slot 9 valid bit, LFE */
1094  #define AIC_ACCR1_RS_SLOT8 (1 << 21) /* Slot 8 valid bit, Surround Right */
1095  #define AIC_ACCR1_RS_SLOT7 (1 << 20) /* Slot 7 valid bit, Surround Left */
1096  #define AIC_ACCR1_RS_SLOT6 (1 << 19) /* Slot 6 valid bit, PCM Center */
1097  #define AIC_ACCR1_RS_SLOT5 (1 << 18) /* Slot 5 valid bit */
1098  #define AIC_ACCR1_RS_SLOT4 (1 << 17) /* Slot 4 valid bit, PCM Right */
1099  #define AIC_ACCR1_RS_SLOT3 (1 << 16) /* Slot 3 valid bit, PCM Left */
1100#define AIC_ACCR1_XS_BIT 0 /* Transmit Valid Slots */
1101#define AIC_ACCR1_XS_MASK (0x3ff << AIC_ACCR1_XS_BIT)
1102  #define AIC_ACCR1_XS_SLOT12 (1 << 9) /* Slot 12 valid bit */
1103  #define AIC_ACCR1_XS_SLOT11 (1 << 8) /* Slot 11 valid bit */
1104  #define AIC_ACCR1_XS_SLOT10 (1 << 7) /* Slot 10 valid bit */
1105  #define AIC_ACCR1_XS_SLOT9 (1 << 6) /* Slot 9 valid bit, LFE */
1106  #define AIC_ACCR1_XS_SLOT8 (1 << 5) /* Slot 8 valid bit, Surround Right */
1107  #define AIC_ACCR1_XS_SLOT7 (1 << 4) /* Slot 7 valid bit, Surround Left */
1108  #define AIC_ACCR1_XS_SLOT6 (1 << 3) /* Slot 6 valid bit, PCM Center */
1109  #define AIC_ACCR1_XS_SLOT5 (1 << 2) /* Slot 5 valid bit */
1110  #define AIC_ACCR1_XS_SLOT4 (1 << 1) /* Slot 4 valid bit, PCM Right */
1111  #define AIC_ACCR1_XS_SLOT3 (1 << 0) /* Slot 3 valid bit, PCM Left */
1112
1113/* AIC Controller AC-link Control Register 2 (AIC_ACCR2) */
1114
1115#define AIC_ACCR2_ERSTO (1 << 18) /* Enable RSTO interrupt */
1116#define AIC_ACCR2_ESADR (1 << 17) /* Enable SADR interrupt */
1117#define AIC_ACCR2_ECADT (1 << 16) /* Enable CADT interrupt */
1118#define AIC_ACCR2_OASS_BIT 8 /* Output Sample Size for AC-link */
1119#define AIC_ACCR2_OASS_MASK (0x3 << AIC_ACCR2_OASS_BIT)
1120  #define AIC_ACCR2_OASS_20BIT (0 << AIC_ACCR2_OASS_BIT) /* Output Audio Sample Size is 20-bit */
1121  #define AIC_ACCR2_OASS_18BIT (1 << AIC_ACCR2_OASS_BIT) /* Output Audio Sample Size is 18-bit */
1122  #define AIC_ACCR2_OASS_16BIT (2 << AIC_ACCR2_OASS_BIT) /* Output Audio Sample Size is 16-bit */
1123  #define AIC_ACCR2_OASS_8BIT (3 << AIC_ACCR2_OASS_BIT) /* Output Audio Sample Size is 8-bit */
1124#define AIC_ACCR2_IASS_BIT 6 /* Output Sample Size for AC-link */
1125#define AIC_ACCR2_IASS_MASK (0x3 << AIC_ACCR2_IASS_BIT)
1126  #define AIC_ACCR2_IASS_20BIT (0 << AIC_ACCR2_IASS_BIT) /* Input Audio Sample Size is 20-bit */
1127  #define AIC_ACCR2_IASS_18BIT (1 << AIC_ACCR2_IASS_BIT) /* Input Audio Sample Size is 18-bit */
1128  #define AIC_ACCR2_IASS_16BIT (2 << AIC_ACCR2_IASS_BIT) /* Input Audio Sample Size is 16-bit */
1129  #define AIC_ACCR2_IASS_8BIT (3 << AIC_ACCR2_IASS_BIT) /* Input Audio Sample Size is 8-bit */
1130#define AIC_ACCR2_SO (1 << 3) /* SDATA_OUT output value */
1131#define AIC_ACCR2_SR (1 << 2) /* RESET# pin level */
1132#define AIC_ACCR2_SS (1 << 1) /* SYNC pin level */
1133#define AIC_ACCR2_SA (1 << 0) /* SYNC and SDATA_OUT alternation */
1134
1135/* AIC Controller I2S/MSB-justified Control Register (AIC_I2SCR) */
1136
1137#define AIC_I2SCR_STPBK (1 << 12) /* Stop BIT_CLK for I2S/MSB-justified */
1138#define AIC_I2SCR_WL_BIT 1 /* Input/Output Sample Size for I2S/MSB-justified */
1139#define AIC_I2SCR_WL_MASK (0x7 << AIC_I2SCR_WL_BIT)
1140  #define AIC_I2SCR_WL_24BIT (0 << AIC_I2SCR_WL_BIT) /* Word Length is 24 bit */
1141  #define AIC_I2SCR_WL_20BIT (1 << AIC_I2SCR_WL_BIT) /* Word Length is 20 bit */
1142  #define AIC_I2SCR_WL_18BIT (2 << AIC_I2SCR_WL_BIT) /* Word Length is 18 bit */
1143  #define AIC_I2SCR_WL_16BIT (3 << AIC_I2SCR_WL_BIT) /* Word Length is 16 bit */
1144  #define AIC_I2SCR_WL_8BIT (4 << AIC_I2SCR_WL_BIT) /* Word Length is 8 bit */
1145#define AIC_I2SCR_AMSL (1 << 0) /* 0:I2S, 1:MSB-justified */
1146
1147/* AIC Controller FIFO Status Register (AIC_SR) */
1148
1149#define AIC_SR_RFL_BIT 24 /* Receive FIFO Level */
1150#define AIC_SR_RFL_MASK (0x3f << AIC_SR_RFL_BIT)
1151#define AIC_SR_TFL_BIT 8 /* Transmit FIFO level */
1152#define AIC_SR_TFL_MASK (0x3f << AIC_SR_TFL_BIT)
1153#define AIC_SR_ROR (1 << 6) /* Receive FIFO Overrun */
1154#define AIC_SR_TUR (1 << 5) /* Transmit FIFO Underrun */
1155#define AIC_SR_RFS (1 << 4) /* Receive FIFO Service Request */
1156#define AIC_SR_TFS (1 << 3) /* Transmit FIFO Service Request */
1157
1158/* AIC Controller AC-link Status Register (AIC_ACSR) */
1159
1160#define AIC_ACSR_SLTERR (1 << 21) /* Slot Error Flag */
1161#define AIC_ACSR_CRDY (1 << 20) /* External CODEC Ready Flag */
1162#define AIC_ACSR_CLPM (1 << 19) /* External CODEC low power mode flag */
1163#define AIC_ACSR_RSTO (1 << 18) /* External CODEC regs read status timeout */
1164#define AIC_ACSR_SADR (1 << 17) /* External CODEC regs status addr and data received */
1165#define AIC_ACSR_CADT (1 << 16) /* Command Address and Data Transmitted */
1166
1167/* AIC Controller I2S/MSB-justified Status Register (AIC_I2SSR) */
1168
1169#define AIC_I2SSR_BSY (1 << 2) /* AIC Busy in I2S/MSB-justified format */
1170
1171/* AIC Controller AC97 codec Command Address Register (AIC_ACCAR) */
1172
1173#define AIC_ACCAR_CAR_BIT 0
1174#define AIC_ACCAR_CAR_MASK (0xfffff << AIC_ACCAR_CAR_BIT)
1175
1176/* AIC Controller AC97 codec Command Data Register (AIC_ACCDR) */
1177
1178#define AIC_ACCDR_CDR_BIT 0
1179#define AIC_ACCDR_CDR_MASK (0xfffff << AIC_ACCDR_CDR_BIT)
1180
1181/* AIC Controller AC97 codec Status Address Register (AIC_ACSAR) */
1182
1183#define AIC_ACSAR_SAR_BIT 0
1184#define AIC_ACSAR_SAR_MASK (0xfffff << AIC_ACSAR_SAR_BIT)
1185
1186/* AIC Controller AC97 codec Status Data Register (AIC_ACSDR) */
1187
1188#define AIC_ACSDR_SDR_BIT 0
1189#define AIC_ACSDR_SDR_MASK (0xfffff << AIC_ACSDR_SDR_BIT)
1190
1191/* AIC Controller I2S/MSB-justified Clock Divider Register (AIC_I2SDIV) */
1192
1193#define AIC_I2SDIV_DIV_BIT 0
1194#define AIC_I2SDIV_DIV_MASK (0x7f << AIC_I2SDIV_DIV_BIT)
1195  #define AIC_I2SDIV_BITCLK_3072KHZ (0x0C << AIC_I2SDIV_DIV_BIT) /* BIT_CLK of 3.072MHz */
1196  #define AIC_I2SDIV_BITCLK_2836KHZ (0x0D << AIC_I2SDIV_DIV_BIT) /* BIT_CLK of 2.836MHz */
1197  #define AIC_I2SDIV_BITCLK_1418KHZ (0x1A << AIC_I2SDIV_DIV_BIT) /* BIT_CLK of 1.418MHz */
1198  #define AIC_I2SDIV_BITCLK_1024KHZ (0x24 << AIC_I2SDIV_DIV_BIT) /* BIT_CLK of 1.024MHz */
1199  #define AIC_I2SDIV_BITCLK_7089KHZ (0x34 << AIC_I2SDIV_DIV_BIT) /* BIT_CLK of 708.92KHz */
1200  #define AIC_I2SDIV_BITCLK_512KHZ (0x48 << AIC_I2SDIV_DIV_BIT) /* BIT_CLK of 512.00KHz */
1201
1202
1203/*************************************************************************
1204 * ICDC (Internal CODEC)
1205 *************************************************************************/
1206#define ICDC_CR (ICDC_BASE + 0x0400) /* ICDC Control Register */
1207#define ICDC_APWAIT (ICDC_BASE + 0x0404) /* Anti-Pop WAIT Stage Timing Control Register */
1208#define ICDC_APPRE (ICDC_BASE + 0x0408) /* Anti-Pop HPEN-PRE Stage Timing Control Register */
1209#define ICDC_APHPEN (ICDC_BASE + 0x040C) /* Anti-Pop HPEN Stage Timing Control Register */
1210#define ICDC_APSR (ICDC_BASE + 0x0410) /* Anti-Pop Status Register */
1211#define ICDC_CDCCR1 (ICDC_BASE + 0x0080)
1212#define ICDC_CDCCR2 (ICDC_BASE + 0x0084)
1213
1214#define REG_ICDC_CR REG32(ICDC_CR)
1215#define REG_ICDC_APWAIT REG32(ICDC_APWAIT)
1216#define REG_ICDC_APPRE REG32(ICDC_APPRE)
1217#define REG_ICDC_APHPEN REG32(ICDC_APHPEN)
1218#define REG_ICDC_APSR REG32(ICDC_APSR)
1219#define REG_ICDC_CDCCR1 REG32(ICDC_CDCCR1)
1220#define REG_ICDC_CDCCR2 REG32(ICDC_CDCCR2)
1221
1222/* ICDC Control Register */
1223#define ICDC_CR_LINVOL_BIT 24 /* LINE Input Volume Gain: GAIN=LINVOL*1.5-34.5 */
1224#define ICDC_CR_LINVOL_MASK (0x1f << ICDC_CR_LINVOL_BIT)
1225#define ICDC_CR_ASRATE_BIT 20 /* Audio Sample Rate */
1226#define ICDC_CR_ASRATE_MASK (0x0f << ICDC_CR_ASRATE_BIT)
1227  #define ICDC_CR_ASRATE_8000 (0x0 << ICDC_CR_ASRATE_BIT)
1228  #define ICDC_CR_ASRATE_11025 (0x1 << ICDC_CR_ASRATE_BIT)
1229  #define ICDC_CR_ASRATE_12000 (0x2 << ICDC_CR_ASRATE_BIT)
1230  #define ICDC_CR_ASRATE_16000 (0x3 << ICDC_CR_ASRATE_BIT)
1231  #define ICDC_CR_ASRATE_22050 (0x4 << ICDC_CR_ASRATE_BIT)
1232  #define ICDC_CR_ASRATE_24000 (0x5 << ICDC_CR_ASRATE_BIT)
1233  #define ICDC_CR_ASRATE_32000 (0x6 << ICDC_CR_ASRATE_BIT)
1234  #define ICDC_CR_ASRATE_44100 (0x7 << ICDC_CR_ASRATE_BIT)
1235  #define ICDC_CR_ASRATE_48000 (0x8 << ICDC_CR_ASRATE_BIT)
1236#define ICDC_CR_MICBG_BIT 18 /* MIC Boost Gain */
1237#define ICDC_CR_MICBG_MASK (0x3 << ICDC_CR_MICBG_BIT)
1238  #define ICDC_CR_MICBG_0DB (0x0 << ICDC_CR_MICBG_BIT)
1239  #define ICDC_CR_MICBG_6DB (0x1 << ICDC_CR_MICBG_BIT)
1240  #define ICDC_CR_MICBG_12DB (0x2 << ICDC_CR_MICBG_BIT)
1241  #define ICDC_CR_MICBG_20DB (0x3 << ICDC_CR_MICBG_BIT)
1242#define ICDC_CR_HPVOL_BIT 16 /* Headphone Volume Gain */
1243#define ICDC_CR_HPVOL_MASK (0x3 << ICDC_CR_HPVOL_BIT)
1244  #define ICDC_CR_HPVOL_0DB (0x0 << ICDC_CR_HPVOL_BIT)
1245  #define ICDC_CR_HPVOL_2DB (0x1 << ICDC_CR_HPVOL_BIT)
1246  #define ICDC_CR_HPVOL_4DB (0x2 << ICDC_CR_HPVOL_BIT)
1247  #define ICDC_CR_HPVOL_6DB (0x3 << ICDC_CR_HPVOL_BIT)
1248#define ICDC_CR_ELINEIN (1 << 13) /* Enable LINE Input */
1249#define ICDC_CR_EMIC (1 << 12) /* Enable MIC Input */
1250#define ICDC_CR_SW1ON (1 << 11) /* Switch 1 in CODEC is on */
1251#define ICDC_CR_EADC (1 << 10) /* Enable ADC */
1252#define ICDC_CR_SW2ON (1 << 9) /* Switch 2 in CODEC is on */
1253#define ICDC_CR_EDAC (1 << 8) /* Enable DAC */
1254#define ICDC_CR_HPMUTE (1 << 5) /* Headphone Mute */
1255#define ICDC_CR_HPTON (1 << 4) /* Headphone Amplifier Trun On */
1256#define ICDC_CR_HPTOFF (1 << 3) /* Headphone Amplifier Trun Off */
1257#define ICDC_CR_TAAP (1 << 2) /* Turn Around of the Anti-Pop Procedure */
1258#define ICDC_CR_EAP (1 << 1) /* Enable Anti-Pop Procedure */
1259#define ICDC_CR_SUSPD (1 << 0) /* CODEC Suspend */
1260
1261/* Anti-Pop WAIT Stage Timing Control Register */
1262#define ICDC_APWAIT_WAITSN_BIT 0
1263#define ICDC_APWAIT_WAITSN_MASK (0x7ff << ICDC_APWAIT_WAITSN_BIT)
1264
1265/* Anti-Pop HPEN-PRE Stage Timing Control Register */
1266#define ICDC_APPRE_PRESN_BIT 0
1267#define ICDC_APPRE_PRESN_MASK (0x1ff << ICDC_APPRE_PRESN_BIT)
1268
1269/* Anti-Pop HPEN Stage Timing Control Register */
1270#define ICDC_APHPEN_HPENSN_BIT 0
1271#define ICDC_APHPEN_HPENSN_MASK (0x3fff << ICDC_APHPEN_HPENSN_BIT)
1272
1273/* Anti-Pop Status Register */
1274#define ICDC_SR_HPST_BIT 14 /* Headphone Amplifier State */
1275#define ICDC_SR_HPST_MASK (0x7 << ICDC_SR_HPST_BIT)
1276#define ICDC_SR_HPST_HP_OFF (0x0 << ICDC_SR_HPST_BIT) /* HP amplifier is off */
1277#define ICDC_SR_HPST_TON_WAIT (0x1 << ICDC_SR_HPST_BIT) /* wait state in turn-on */
1278  #define ICDC_SR_HPST_TON_PRE (0x2 << ICDC_SR_HPST_BIT) /* pre-enable state in turn-on */
1279#define ICDC_SR_HPST_TON_HPEN (0x3 << ICDC_SR_HPST_BIT) /* HP enable state in turn-on */
1280  #define ICDC_SR_HPST_TOFF_HPEN (0x4 << ICDC_SR_HPST_BIT) /* HP enable state in turn-off */
1281  #define ICDC_SR_HPST_TOFF_PRE (0x5 << ICDC_SR_HPST_BIT) /* pre-enable state in turn-off */
1282  #define ICDC_SR_HPST_TOFF_WAIT (0x6 << ICDC_SR_HPST_BIT) /* wait state in turn-off */
1283  #define ICDC_SR_HPST_HP_ON (0x7 << ICDC_SR_HPST_BIT) /* HP amplifier is on */
1284#define ICDC_SR_SNCNT_BIT 0 /* Sample Number Counter */
1285#define ICDC_SR_SNCNT_MASK (0x3fff << ICDC_SR_SNCNT_BIT)
1286
1287#define ICDC_CDCCR1_ELININ (1 << 29)
1288#define ICDC_CDCCR1_EMIC (1 << 28)
1289#define ICDC_CDCCR1_SW1ON (1 << 27)
1290#define ICDC_CDCCR1_EADC (1 << 26)
1291#define ICDC_CDCCR1_SW2ON (1 << 25)
1292#define ICDC_CDCCR1_EDAC (1 << 24)
1293#define ICDC_CDCCR1_PDVR (1 << 20)
1294#define ICDC_CDCCR1_PDVRA (1 << 19)
1295#define ICDC_CDCCR1_VRPLD (1 << 18)
1296#define ICDC_CDCCR1_VRCGL (1 << 17)
1297#define ICDC_CDCCR1_VRCGH (1 << 16)
1298#define ICDC_CDCCR1_HPMUTE (1 << 14)
1299#define ICDC_CDCCR1_HPOV0 (1 << 13)
1300#define ICDC_CDCCR1_HPCG (1 << 12)
1301#define ICDC_CDCCR1_HPPLDM (1 << 11)
1302#define ICDC_CDCCR1_HPPLDR (1 << 10)
1303#define ICDC_CDCCR1_PDHPM (1 << 9)
1304#define ICDC_CDCCR1_PDHP (1 << 8)
1305#define ICDC_CDCCR1_SUSPD (1 << 1)
1306#define ICDC_CDCCR1_RST (1 << 0)
1307
1308#define ICDC_CDCCR2_AINVOL(n) ((n & 0x1F) << 16)
1309#define ICDC_CDCCR2_SMPR(n) ((n & 0xF) << 8)
1310#define ICDC_CDCCR2_MICBG(n) ((n & 0x3) << 4)
1311#define ICDC_CDCCR2_HPVOL(n) ((n & 0x3) << 0)
1312
1313#define ICDC_CDCCR2_SMPR_8 (0)
1314#define ICDC_CDCCR2_SMPR_11 (1)
1315#define ICDC_CDCCR2_SMPR_12 (2)
1316#define ICDC_CDCCR2_SMPR_16 (3)
1317#define ICDC_CDCCR2_SMPR_22 (4)
1318#define ICDC_CDCCR2_SMPR_24 (5)
1319#define ICDC_CDCCR2_SMPR_32 (6)
1320#define ICDC_CDCCR2_SMPR_44 (7)
1321#define ICDC_CDCCR2_SMPR_48 (8)
1322
1323#define ICDC_CDCCR2_HPVOL_0 (0)
1324#define ICDC_CDCCR2_HPVOL_2 (1)
1325#define ICDC_CDCCR2_HPVOL_4 (2)
1326#define ICDC_CDCCR2_HPVOL_6 (3)
1327
1328
1329/*************************************************************************
1330 * I2C
1331 *************************************************************************/
1332#define I2C_DR (I2C_BASE + 0x000)
1333#define I2C_CR (I2C_BASE + 0x004)
1334#define I2C_SR (I2C_BASE + 0x008)
1335#define I2C_GR (I2C_BASE + 0x00C)
1336
1337#define REG_I2C_DR REG8(I2C_DR)
1338#define REG_I2C_CR REG8(I2C_CR)
1339#define REG_I2C_SR REG8(I2C_SR)
1340#define REG_I2C_GR REG16(I2C_GR)
1341
1342/* I2C Control Register (I2C_CR) */
1343
1344#define I2C_CR_IEN (1 << 4)
1345#define I2C_CR_STA (1 << 3)
1346#define I2C_CR_STO (1 << 2)
1347#define I2C_CR_AC (1 << 1)
1348#define I2C_CR_I2CE (1 << 0)
1349
1350/* I2C Status Register (I2C_SR) */
1351
1352#define I2C_SR_STX (1 << 4)
1353#define I2C_SR_BUSY (1 << 3)
1354#define I2C_SR_TEND (1 << 2)
1355#define I2C_SR_DRF (1 << 1)
1356#define I2C_SR_ACKF (1 << 0)
1357
1358
1359/*************************************************************************
1360 * SSI
1361 *************************************************************************/
1362#define SSI_DR (SSI_BASE + 0x000)
1363#define SSI_CR0 (SSI_BASE + 0x004)
1364#define SSI_CR1 (SSI_BASE + 0x008)
1365#define SSI_SR (SSI_BASE + 0x00C)
1366#define SSI_ITR (SSI_BASE + 0x010)
1367#define SSI_ICR (SSI_BASE + 0x014)
1368#define SSI_GR (SSI_BASE + 0x018)
1369
1370#define REG_SSI_DR REG32(SSI_DR)
1371#define REG_SSI_CR0 REG16(SSI_CR0)
1372#define REG_SSI_CR1 REG32(SSI_CR1)
1373#define REG_SSI_SR REG32(SSI_SR)
1374#define REG_SSI_ITR REG16(SSI_ITR)
1375#define REG_SSI_ICR REG8(SSI_ICR)
1376#define REG_SSI_GR REG16(SSI_GR)
1377
1378/* SSI Data Register (SSI_DR) */
1379
1380#define SSI_DR_GPC_BIT 0
1381#define SSI_DR_GPC_MASK (0x1ff << SSI_DR_GPC_BIT)
1382
1383/* SSI Control Register 0 (SSI_CR0) */
1384
1385#define SSI_CR0_SSIE (1 << 15)
1386#define SSI_CR0_TIE (1 << 14)
1387#define SSI_CR0_RIE (1 << 13)
1388#define SSI_CR0_TEIE (1 << 12)
1389#define SSI_CR0_REIE (1 << 11)
1390#define SSI_CR0_LOOP (1 << 10)
1391#define SSI_CR0_RFINE (1 << 9)
1392#define SSI_CR0_RFINC (1 << 8)
1393#define SSI_CR0_FSEL (1 << 6)
1394#define SSI_CR0_TFLUSH (1 << 2)
1395#define SSI_CR0_RFLUSH (1 << 1)
1396#define SSI_CR0_DISREV (1 << 0)
1397
1398/* SSI Control Register 1 (SSI_CR1) */
1399
1400#define SSI_CR1_FRMHL_BIT 30
1401#define SSI_CR1_FRMHL_MASK (0x3 << SSI_CR1_FRMHL_BIT)
1402  #define SSI_CR1_FRMHL_CELOW_CE2LOW (0 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is low valid and SSI_CE2_ is low valid */
1403  #define SSI_CR1_FRMHL_CEHIGH_CE2LOW (1 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is high valid and SSI_CE2_ is low valid */
1404  #define SSI_CR1_FRMHL_CELOW_CE2HIGH (2 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is low valid and SSI_CE2_ is high valid */
1405  #define SSI_CR1_FRMHL_CEHIGH_CE2HIGH (3 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is high valid and SSI_CE2_ is high valid */
1406#define SSI_CR1_TFVCK_BIT 28
1407#define SSI_CR1_TFVCK_MASK (0x3 << SSI_CR1_TFVCK_BIT)
1408  #define SSI_CR1_TFVCK_0 (0 << SSI_CR1_TFVCK_BIT)
1409  #define SSI_CR1_TFVCK_1 (1 << SSI_CR1_TFVCK_BIT)
1410  #define SSI_CR1_TFVCK_2 (2 << SSI_CR1_TFVCK_BIT)
1411  #define SSI_CR1_TFVCK_3 (3 << SSI_CR1_TFVCK_BIT)
1412#define SSI_CR1_TCKFI_BIT 26
1413#define SSI_CR1_TCKFI_MASK (0x3 << SSI_CR1_TCKFI_BIT)
1414  #define SSI_CR1_TCKFI_0 (0 << SSI_CR1_TCKFI_BIT)
1415  #define SSI_CR1_TCKFI_1 (1 << SSI_CR1_TCKFI_BIT)
1416  #define SSI_CR1_TCKFI_2 (2 << SSI_CR1_TCKFI_BIT)
1417  #define SSI_CR1_TCKFI_3 (3 << SSI_CR1_TCKFI_BIT)
1418#define SSI_CR1_LFST (1 << 25)
1419#define SSI_CR1_ITFRM (1 << 24)
1420#define SSI_CR1_UNFIN (1 << 23)
1421#define SSI_CR1_MULTS (1 << 22)
1422#define SSI_CR1_FMAT_BIT 20
1423#define SSI_CR1_FMAT_MASK (0x3 << SSI_CR1_FMAT_BIT)
1424  #define SSI_CR1_FMAT_SPI (0 << SSI_CR1_FMAT_BIT) /* Motorola¡¯s SPI format */
1425  #define SSI_CR1_FMAT_SSP (1 << SSI_CR1_FMAT_BIT) /* TI's SSP format */
1426  #define SSI_CR1_FMAT_MW1 (2 << SSI_CR1_FMAT_BIT) /* National Microwire 1 format */
1427  #define SSI_CR1_FMAT_MW2 (3 << SSI_CR1_FMAT_BIT) /* National Microwire 2 format */
1428#define SSI_CR1_TTRG_BIT 16
1429#define SSI_CR1_TTRG_MASK (0xf << SSI_CR1_TTRG_BIT)
1430  #define SSI_CR1_TTRG_1 (0 << SSI_CR1_TTRG_BIT)
1431  #define SSI_CR1_TTRG_8 (1 << SSI_CR1_TTRG_BIT)
1432  #define SSI_CR1_TTRG_16 (2 << SSI_CR1_TTRG_BIT)
1433  #define SSI_CR1_TTRG_24 (3 << SSI_CR1_TTRG_BIT)
1434  #define SSI_CR1_TTRG_32 (4 << SSI_CR1_TTRG_BIT)
1435  #define SSI_CR1_TTRG_40 (5 << SSI_CR1_TTRG_BIT)
1436  #define SSI_CR1_TTRG_48 (6 << SSI_CR1_TTRG_BIT)
1437  #define SSI_CR1_TTRG_56 (7 << SSI_CR1_TTRG_BIT)
1438  #define SSI_CR1_TTRG_64 (8 << SSI_CR1_TTRG_BIT)
1439  #define SSI_CR1_TTRG_72 (9 << SSI_CR1_TTRG_BIT)
1440  #define SSI_CR1_TTRG_80 (10<< SSI_CR1_TTRG_BIT)
1441  #define SSI_CR1_TTRG_88 (11<< SSI_CR1_TTRG_BIT)
1442  #define SSI_CR1_TTRG_96 (12<< SSI_CR1_TTRG_BIT)
1443  #define SSI_CR1_TTRG_104 (13<< SSI_CR1_TTRG_BIT)
1444  #define SSI_CR1_TTRG_112 (14<< SSI_CR1_TTRG_BIT)
1445  #define SSI_CR1_TTRG_120 (15<< SSI_CR1_TTRG_BIT)
1446#define SSI_CR1_MCOM_BIT 12
1447#define SSI_CR1_MCOM_MASK (0xf << SSI_CR1_MCOM_BIT)
1448  #define SSI_CR1_MCOM_1BIT (0x0 << SSI_CR1_MCOM_BIT) /* 1-bit command selected */
1449  #define SSI_CR1_MCOM_2BIT (0x1 << SSI_CR1_MCOM_BIT) /* 2-bit command selected */
1450  #define SSI_CR1_MCOM_3BIT (0x2 << SSI_CR1_MCOM_BIT) /* 3-bit command selected */
1451  #define SSI_CR1_MCOM_4BIT (0x3 << SSI_CR1_MCOM_BIT) /* 4-bit command selected */
1452  #define SSI_CR1_MCOM_5BIT (0x4 << SSI_CR1_MCOM_BIT) /* 5-bit command selected */
1453  #define SSI_CR1_MCOM_6BIT (0x5 << SSI_CR1_MCOM_BIT) /* 6-bit command selected */
1454  #define SSI_CR1_MCOM_7BIT (0x6 << SSI_CR1_MCOM_BIT) /* 7-bit command selected */
1455  #define SSI_CR1_MCOM_8BIT (0x7 << SSI_CR1_MCOM_BIT) /* 8-bit command selected */
1456  #define SSI_CR1_MCOM_9BIT (0x8 << SSI_CR1_MCOM_BIT) /* 9-bit command selected */
1457  #define SSI_CR1_MCOM_10BIT (0x9 << SSI_CR1_MCOM_BIT) /* 10-bit command selected */
1458  #define SSI_CR1_MCOM_11BIT (0xA << SSI_CR1_MCOM_BIT) /* 11-bit command selected */
1459  #define SSI_CR1_MCOM_12BIT (0xB << SSI_CR1_MCOM_BIT) /* 12-bit command selected */
1460  #define SSI_CR1_MCOM_13BIT (0xC << SSI_CR1_MCOM_BIT) /* 13-bit command selected */
1461  #define SSI_CR1_MCOM_14BIT (0xD << SSI_CR1_MCOM_BIT) /* 14-bit command selected */
1462  #define SSI_CR1_MCOM_15BIT (0xE << SSI_CR1_MCOM_BIT) /* 15-bit command selected */
1463  #define SSI_CR1_MCOM_16BIT (0xF << SSI_CR1_MCOM_BIT) /* 16-bit command selected */
1464#define SSI_CR1_RTRG_BIT 8
1465#define SSI_CR1_RTRG_MASK (0xf << SSI_CR1_RTRG_BIT)
1466  #define SSI_CR1_RTRG_1 (0 << SSI_CR1_RTRG_BIT)
1467  #define SSI_CR1_RTRG_8 (1 << SSI_CR1_RTRG_BIT)
1468  #define SSI_CR1_RTRG_16 (2 << SSI_CR1_RTRG_BIT)
1469  #define SSI_CR1_RTRG_24 (3 << SSI_CR1_RTRG_BIT)
1470  #define SSI_CR1_RTRG_32 (4 << SSI_CR1_RTRG_BIT)
1471  #define SSI_CR1_RTRG_40 (5 << SSI_CR1_RTRG_BIT)
1472  #define SSI_CR1_RTRG_48 (6 << SSI_CR1_RTRG_BIT)
1473  #define SSI_CR1_RTRG_56 (7 << SSI_CR1_RTRG_BIT)
1474  #define SSI_CR1_RTRG_64 (8 << SSI_CR1_RTRG_BIT)
1475  #define SSI_CR1_RTRG_72 (9 << SSI_CR1_RTRG_BIT)
1476  #define SSI_CR1_RTRG_80 (10<< SSI_CR1_RTRG_BIT)
1477  #define SSI_CR1_RTRG_88 (11<< SSI_CR1_RTRG_BIT)
1478  #define SSI_CR1_RTRG_96 (12<< SSI_CR1_RTRG_BIT)
1479  #define SSI_CR1_RTRG_104 (13<< SSI_CR1_RTRG_BIT)
1480  #define SSI_CR1_RTRG_112 (14<< SSI_CR1_RTRG_BIT)
1481  #define SSI_CR1_RTRG_120 (15<< SSI_CR1_RTRG_BIT)
1482#define SSI_CR1_FLEN_BIT 4
1483#define SSI_CR1_FLEN_MASK (0xf << SSI_CR1_FLEN_BIT)
1484  #define SSI_CR1_FLEN_2BIT (0x0 << SSI_CR1_FLEN_BIT)
1485  #define SSI_CR1_FLEN_3BIT (0x1 << SSI_CR1_FLEN_BIT)
1486  #define SSI_CR1_FLEN_4BIT (0x2 << SSI_CR1_FLEN_BIT)
1487  #define SSI_CR1_FLEN_5BIT (0x3 << SSI_CR1_FLEN_BIT)
1488  #define SSI_CR1_FLEN_6BIT (0x4 << SSI_CR1_FLEN_BIT)
1489  #define SSI_CR1_FLEN_7BIT (0x5 << SSI_CR1_FLEN_BIT)
1490  #define SSI_CR1_FLEN_8BIT (0x6 << SSI_CR1_FLEN_BIT)
1491  #define SSI_CR1_FLEN_9BIT (0x7 << SSI_CR1_FLEN_BIT)
1492  #define SSI_CR1_FLEN_10BIT (0x8 << SSI_CR1_FLEN_BIT)
1493  #define SSI_CR1_FLEN_11BIT (0x9 << SSI_CR1_FLEN_BIT)
1494  #define SSI_CR1_FLEN_12BIT (0xA << SSI_CR1_FLEN_BIT)
1495  #define SSI_CR1_FLEN_13BIT (0xB << SSI_CR1_FLEN_BIT)
1496  #define SSI_CR1_FLEN_14BIT (0xC << SSI_CR1_FLEN_BIT)
1497  #define SSI_CR1_FLEN_15BIT (0xD << SSI_CR1_FLEN_BIT)
1498  #define SSI_CR1_FLEN_16BIT (0xE << SSI_CR1_FLEN_BIT)
1499  #define SSI_CR1_FLEN_17BIT (0xF << SSI_CR1_FLEN_BIT)
1500#define SSI_CR1_PHA (1 << 1)
1501#define SSI_CR1_POL (1 << 0)
1502
1503/* SSI Status Register (SSI_SR) */
1504
1505#define SSI_SR_TFIFONUM_BIT 16
1506#define SSI_SR_TFIFONUM_MASK (0xff << SSI_SR_TFIFONUM_BIT)
1507#define SSI_SR_RFIFONUM_BIT 8
1508#define SSI_SR_RFIFONUM_MASK (0xff << SSI_SR_RFIFONUM_BIT)
1509#define SSI_SR_END (1 << 7)
1510#define SSI_SR_BUSY (1 << 6)
1511#define SSI_SR_TFF (1 << 5)
1512#define SSI_SR_RFE (1 << 4)
1513#define SSI_SR_TFHE (1 << 3)
1514#define SSI_SR_RFHF (1 << 2)
1515#define SSI_SR_UNDR (1 << 1)
1516#define SSI_SR_OVER (1 << 0)
1517
1518/* SSI Interval Time Control Register (SSI_ITR) */
1519
1520#define SSI_ITR_CNTCLK (1 << 15)
1521#define SSI_ITR_IVLTM_BIT 0
1522#define SSI_ITR_IVLTM_MASK (0x7fff << SSI_ITR_IVLTM_BIT)
1523
1524
1525/*************************************************************************
1526 * MSC
1527 *************************************************************************/
1528#define MSC_STRPCL (MSC_BASE + 0x000)
1529#define MSC_STAT (MSC_BASE + 0x004)
1530#define MSC_CLKRT (MSC_BASE + 0x008)
1531#define MSC_CMDAT (MSC_BASE + 0x00C)
1532#define MSC_RESTO (MSC_BASE + 0x010)
1533#define MSC_RDTO (MSC_BASE + 0x014)
1534#define MSC_BLKLEN (MSC_BASE + 0x018)
1535#define MSC_NOB (MSC_BASE + 0x01C)
1536#define MSC_SNOB (MSC_BASE + 0x020)
1537#define MSC_IMASK (MSC_BASE + 0x024)
1538#define MSC_IREG (MSC_BASE + 0x028)
1539#define MSC_CMD (MSC_BASE + 0x02C)
1540#define MSC_ARG (MSC_BASE + 0x030)
1541#define MSC_RES (MSC_BASE + 0x034)
1542#define MSC_RXFIFO (MSC_BASE + 0x038)
1543#define MSC_TXFIFO (MSC_BASE + 0x03C)
1544
1545#define REG_MSC_STRPCL REG16(MSC_STRPCL)
1546#define REG_MSC_STAT REG32(MSC_STAT)
1547#define REG_MSC_CLKRT REG16(MSC_CLKRT)
1548#define REG_MSC_CMDAT REG32(MSC_CMDAT)
1549#define REG_MSC_RESTO REG16(MSC_RESTO)
1550#define REG_MSC_RDTO REG16(MSC_RDTO)
1551#define REG_MSC_BLKLEN REG16(MSC_BLKLEN)
1552#define REG_MSC_NOB REG16(MSC_NOB)
1553#define REG_MSC_SNOB REG16(MSC_SNOB)
1554#define REG_MSC_IMASK REG16(MSC_IMASK)
1555#define REG_MSC_IREG REG16(MSC_IREG)
1556#define REG_MSC_CMD REG8(MSC_CMD)
1557#define REG_MSC_ARG REG32(MSC_ARG)
1558#define REG_MSC_RES REG16(MSC_RES)
1559#define REG_MSC_RXFIFO REG32(MSC_RXFIFO)
1560#define REG_MSC_TXFIFO REG32(MSC_TXFIFO)
1561
1562/* MSC Clock and Control Register (MSC_STRPCL) */
1563
1564#define MSC_STRPCL_EXIT_MULTIPLE (1 << 7)
1565#define MSC_STRPCL_EXIT_TRANSFER (1 << 6)
1566#define MSC_STRPCL_START_READWAIT (1 << 5)
1567#define MSC_STRPCL_STOP_READWAIT (1 << 4)
1568#define MSC_STRPCL_RESET (1 << 3)
1569#define MSC_STRPCL_START_OP (1 << 2)
1570#define MSC_STRPCL_CLOCK_CONTROL_BIT 0
1571#define MSC_STRPCL_CLOCK_CONTROL_MASK (0x3 << MSC_STRPCL_CLOCK_CONTROL_BIT)
1572  #define MSC_STRPCL_CLOCK_CONTROL_STOP (0x1 << MSC_STRPCL_CLOCK_CONTROL_BIT) /* Stop MMC/SD clock */
1573  #define MSC_STRPCL_CLOCK_CONTROL_START (0x2 << MSC_STRPCL_CLOCK_CONTROL_BIT) /* Start MMC/SD clock */
1574
1575/* MSC Status Register (MSC_STAT) */
1576
1577#define MSC_STAT_IS_RESETTING (1 << 15)
1578#define MSC_STAT_SDIO_INT_ACTIVE (1 << 14)
1579#define MSC_STAT_PRG_DONE (1 << 13)
1580#define MSC_STAT_DATA_TRAN_DONE (1 << 12)
1581#define MSC_STAT_END_CMD_RES (1 << 11)
1582#define MSC_STAT_DATA_FIFO_AFULL (1 << 10)
1583#define MSC_STAT_IS_READWAIT (1 << 9)
1584#define MSC_STAT_CLK_EN (1 << 8)
1585#define MSC_STAT_DATA_FIFO_FULL (1 << 7)
1586#define MSC_STAT_DATA_FIFO_EMPTY (1 << 6)
1587#define MSC_STAT_CRC_RES_ERR (1 << 5)
1588#define MSC_STAT_CRC_READ_ERROR (1 << 4)
1589#define MSC_STAT_CRC_WRITE_ERROR_BIT 2
1590#define MSC_STAT_CRC_WRITE_ERROR_MASK (0x3 << MSC_STAT_CRC_WRITE_ERROR_BIT)
1591  #define MSC_STAT_CRC_WRITE_ERROR_NO (0 << MSC_STAT_CRC_WRITE_ERROR_BIT) /* No error on transmission of data */
1592  #define MSC_STAT_CRC_WRITE_ERROR (1 << MSC_STAT_CRC_WRITE_ERROR_BIT) /* Card observed erroneous transmission of data */
1593  #define MSC_STAT_CRC_WRITE_ERROR_NOSTS (2 << MSC_STAT_CRC_WRITE_ERROR_BIT) /* No CRC status is sent back */
1594#define MSC_STAT_TIME_OUT_RES (1 << 1)
1595#define MSC_STAT_TIME_OUT_READ (1 << 0)
1596
1597/* MSC Bus Clock Control Register (MSC_CLKRT) */
1598
1599#define MSC_CLKRT_CLK_RATE_BIT 0
1600#define MSC_CLKRT_CLK_RATE_MASK (0x7 << MSC_CLKRT_CLK_RATE_BIT)
1601  #define MSC_CLKRT_CLK_RATE_DIV_1 (0x0 << MSC_CLKRT_CLK_RATE_BIT) /* CLK_SRC */
1602  #define MSC_CLKRT_CLK_RATE_DIV_2 (0x1 << MSC_CLKRT_CLK_RATE_BIT) /* 1/2 of CLK_SRC */
1603  #define MSC_CLKRT_CLK_RATE_DIV_4 (0x2 << MSC_CLKRT_CLK_RATE_BIT) /* 1/4 of CLK_SRC */
1604  #define MSC_CLKRT_CLK_RATE_DIV_8 (0x3 << MSC_CLKRT_CLK_RATE_BIT) /* 1/8 of CLK_SRC */
1605  #define MSC_CLKRT_CLK_RATE_DIV_16 (0x4 << MSC_CLKRT_CLK_RATE_BIT) /* 1/16 of CLK_SRC */
1606  #define MSC_CLKRT_CLK_RATE_DIV_32 (0x5 << MSC_CLKRT_CLK_RATE_BIT) /* 1/32 of CLK_SRC */
1607  #define MSC_CLKRT_CLK_RATE_DIV_64 (0x6 << MSC_CLKRT_CLK_RATE_BIT) /* 1/64 of CLK_SRC */
1608  #define MSC_CLKRT_CLK_RATE_DIV_128 (0x7 << MSC_CLKRT_CLK_RATE_BIT) /* 1/128 of CLK_SRC */
1609
1610/* MSC Command Sequence Control Register (MSC_CMDAT) */
1611
1612#define MSC_CMDAT_IO_ABORT (1 << 11)
1613#define MSC_CMDAT_BUS_WIDTH_BIT 9
1614#define MSC_CMDAT_BUS_WIDTH_MASK (0x3 << MSC_CMDAT_BUS_WIDTH_BIT)
1615  #define MSC_CMDAT_BUS_WIDTH_1BIT (0x0 << MSC_CMDAT_BUS_WIDTH_BIT) /* 1-bit data bus */
1616  #define MSC_CMDAT_BUS_WIDTH_4BIT (0x2 << MSC_CMDAT_BUS_WIDTH_BIT) /* 4-bit data bus */
1617  #define CMDAT_BUS_WIDTH1 (0x0 << MSC_CMDAT_BUS_WIDTH_BIT)
1618  #define CMDAT_BUS_WIDTH4 (0x2 << MSC_CMDAT_BUS_WIDTH_BIT)
1619#define MSC_CMDAT_DMA_EN (1 << 8)
1620#define MSC_CMDAT_INIT (1 << 7)
1621#define MSC_CMDAT_BUSY (1 << 6)
1622#define MSC_CMDAT_STREAM_BLOCK (1 << 5)
1623#define MSC_CMDAT_WRITE (1 << 4)
1624#define MSC_CMDAT_READ (0 << 4)
1625#define MSC_CMDAT_DATA_EN (1 << 3)
1626#define MSC_CMDAT_RESPONSE_BIT 0
1627#define MSC_CMDAT_RESPONSE_MASK (0x7 << MSC_CMDAT_RESPONSE_BIT)
1628  #define MSC_CMDAT_RESPONSE_NONE (0x0 << MSC_CMDAT_RESPONSE_BIT) /* No response */
1629  #define MSC_CMDAT_RESPONSE_R1 (0x1 << MSC_CMDAT_RESPONSE_BIT) /* Format R1 and R1b */
1630  #define MSC_CMDAT_RESPONSE_R2 (0x2 << MSC_CMDAT_RESPONSE_BIT) /* Format R2 */
1631  #define MSC_CMDAT_RESPONSE_R3 (0x3 << MSC_CMDAT_RESPONSE_BIT) /* Format R3 */
1632  #define MSC_CMDAT_RESPONSE_R4 (0x4 << MSC_CMDAT_RESPONSE_BIT) /* Format R4 */
1633  #define MSC_CMDAT_RESPONSE_R5 (0x5 << MSC_CMDAT_RESPONSE_BIT) /* Format R5 */
1634  #define MSC_CMDAT_RESPONSE_R6 (0x6 << MSC_CMDAT_RESPONSE_BIT) /* Format R6 */
1635
1636#define CMDAT_DMA_EN (1 << 8)
1637#define CMDAT_INIT (1 << 7)
1638#define CMDAT_BUSY (1 << 6)
1639#define CMDAT_STREAM (1 << 5)
1640#define CMDAT_WRITE (1 << 4)
1641#define CMDAT_DATA_EN (1 << 3)
1642
1643/* MSC Interrupts Mask Register (MSC_IMASK) */
1644
1645#define MSC_IMASK_SDIO (1 << 7)
1646#define MSC_IMASK_TXFIFO_WR_REQ (1 << 6)
1647#define MSC_IMASK_RXFIFO_RD_REQ (1 << 5)
1648#define MSC_IMASK_END_CMD_RES (1 << 2)
1649#define MSC_IMASK_PRG_DONE (1 << 1)
1650#define MSC_IMASK_DATA_TRAN_DONE (1 << 0)
1651
1652
1653/* MSC Interrupts Status Register (MSC_IREG) */
1654
1655#define MSC_IREG_SDIO (1 << 7)
1656#define MSC_IREG_TXFIFO_WR_REQ (1 << 6)
1657#define MSC_IREG_RXFIFO_RD_REQ (1 << 5)
1658#define MSC_IREG_END_CMD_RES (1 << 2)
1659#define MSC_IREG_PRG_DONE (1 << 1)
1660#define MSC_IREG_DATA_TRAN_DONE (1 << 0)
1661
1662
1663/*************************************************************************
1664 * EMC (External Memory Controller)
1665 *************************************************************************/
1666#define EMC_BCR (EMC_BASE + 0x0) /* BCR */
1667
1668#define EMC_SMCR0 (EMC_BASE + 0x10) /* Static Memory Control Register 0 */
1669#define EMC_SMCR1 (EMC_BASE + 0x14) /* Static Memory Control Register 1 */
1670#define EMC_SMCR2 (EMC_BASE + 0x18) /* Static Memory Control Register 2 */
1671#define EMC_SMCR3 (EMC_BASE + 0x1c) /* Static Memory Control Register 3 */
1672#define EMC_SMCR4 (EMC_BASE + 0x20) /* Static Memory Control Register 4 */
1673#define EMC_SACR0 (EMC_BASE + 0x30) /* Static Memory Bank 0 Addr Config Reg */
1674#define EMC_SACR1 (EMC_BASE + 0x34) /* Static Memory Bank 1 Addr Config Reg */
1675#define EMC_SACR2 (EMC_BASE + 0x38) /* Static Memory Bank 2 Addr Config Reg */
1676#define EMC_SACR3 (EMC_BASE + 0x3c) /* Static Memory Bank 3 Addr Config Reg */
1677#define EMC_SACR4 (EMC_BASE + 0x40) /* Static Memory Bank 4 Addr Config Reg */
1678
1679#define EMC_NFCSR (EMC_BASE + 0x050) /* NAND Flash Control/Status Register */
1680#define EMC_NFECR (EMC_BASE + 0x100) /* NAND Flash ECC Control Register */
1681#define EMC_NFECC (EMC_BASE + 0x104) /* NAND Flash ECC Data Register */
1682#define EMC_NFPAR0 (EMC_BASE + 0x108) /* NAND Flash RS Parity 0 Register */
1683#define EMC_NFPAR1 (EMC_BASE + 0x10c) /* NAND Flash RS Parity 1 Register */
1684#define EMC_NFPAR2 (EMC_BASE + 0x110) /* NAND Flash RS Parity 2 Register */
1685#define EMC_NFINTS (EMC_BASE + 0x114) /* NAND Flash Interrupt Status Register */
1686#define EMC_NFINTE (EMC_BASE + 0x118) /* NAND Flash Interrupt Enable Register */
1687#define EMC_NFERR0 (EMC_BASE + 0x11c) /* NAND Flash RS Error Report 0 Register */
1688#define EMC_NFERR1 (EMC_BASE + 0x120) /* NAND Flash RS Error Report 1 Register */
1689#define EMC_NFERR2 (EMC_BASE + 0x124) /* NAND Flash RS Error Report 2 Register */
1690#define EMC_NFERR3 (EMC_BASE + 0x128) /* NAND Flash RS Error Report 3 Register */
1691
1692#define EMC_DMCR (EMC_BASE + 0x80) /* DRAM Control Register */
1693#define EMC_RTCSR (EMC_BASE + 0x84) /* Refresh Time Control/Status Register */
1694#define EMC_RTCNT (EMC_BASE + 0x88) /* Refresh Timer Counter */
1695#define EMC_RTCOR (EMC_BASE + 0x8c) /* Refresh Time Constant Register */
1696#define EMC_DMAR0 (EMC_BASE + 0x90) /* SDRAM Bank 0 Addr Config Register */
1697#define EMC_SDMR0 (EMC_BASE + 0xa000) /* Mode Register of SDRAM bank 0 */
1698
1699#define REG_EMC_BCR REG32(EMC_BCR)
1700
1701#define REG_EMC_SMCR0 REG32(EMC_SMCR0)
1702#define REG_EMC_SMCR1 REG32(EMC_SMCR1)
1703#define REG_EMC_SMCR2 REG32(EMC_SMCR2)
1704#define REG_EMC_SMCR3 REG32(EMC_SMCR3)
1705#define REG_EMC_SMCR4 REG32(EMC_SMCR4)
1706#define REG_EMC_SACR0 REG32(EMC_SACR0)
1707#define REG_EMC_SACR1 REG32(EMC_SACR1)
1708#define REG_EMC_SACR2 REG32(EMC_SACR2)
1709#define REG_EMC_SACR3 REG32(EMC_SACR3)
1710#define REG_EMC_SACR4 REG32(EMC_SACR4)
1711
1712#define REG_EMC_NFCSR REG32(EMC_NFCSR)
1713#define REG_EMC_NFECR REG32(EMC_NFECR)
1714#define REG_EMC_NFECC REG32(EMC_NFECC)
1715#define REG_EMC_NFPAR0 REG32(EMC_NFPAR0)
1716#define REG_EMC_NFPAR1 REG32(EMC_NFPAR1)
1717#define REG_EMC_NFPAR2 REG32(EMC_NFPAR2)
1718#define REG_EMC_NFINTS REG32(EMC_NFINTS)
1719#define REG_EMC_NFINTE REG32(EMC_NFINTE)
1720#define REG_EMC_NFERR0 REG32(EMC_NFERR0)
1721#define REG_EMC_NFERR1 REG32(EMC_NFERR1)
1722#define REG_EMC_NFERR2 REG32(EMC_NFERR2)
1723#define REG_EMC_NFERR3 REG32(EMC_NFERR3)
1724
1725#define REG_EMC_DMCR REG32(EMC_DMCR)
1726#define REG_EMC_RTCSR REG16(EMC_RTCSR)
1727#define REG_EMC_RTCNT REG16(EMC_RTCNT)
1728#define REG_EMC_RTCOR REG16(EMC_RTCOR)
1729#define REG_EMC_DMAR0 REG32(EMC_DMAR0)
1730
1731/* Static Memory Control Register */
1732#define EMC_SMCR_STRV_BIT 24
1733#define EMC_SMCR_STRV_MASK (0x0f << EMC_SMCR_STRV_BIT)
1734#define EMC_SMCR_TAW_BIT 20
1735#define EMC_SMCR_TAW_MASK (0x0f << EMC_SMCR_TAW_BIT)
1736#define EMC_SMCR_TBP_BIT 16
1737#define EMC_SMCR_TBP_MASK (0x0f << EMC_SMCR_TBP_BIT)
1738#define EMC_SMCR_TAH_BIT 12
1739#define EMC_SMCR_TAH_MASK (0x07 << EMC_SMCR_TAH_BIT)
1740#define EMC_SMCR_TAS_BIT 8
1741#define EMC_SMCR_TAS_MASK (0x07 << EMC_SMCR_TAS_BIT)
1742#define EMC_SMCR_BW_BIT 6
1743#define EMC_SMCR_BW_MASK (0x03 << EMC_SMCR_BW_BIT) /* Bus Width? */
1744  #define EMC_SMCR_BW_8BIT (0 << EMC_SMCR_BW_BIT)
1745  #define EMC_SMCR_BW_16BIT (1 << EMC_SMCR_BW_BIT)
1746  #define EMC_SMCR_BW_32BIT (2 << EMC_SMCR_BW_BIT)
1747#define EMC_SMCR_BCM (1 << 3)
1748#define EMC_SMCR_BL_BIT 1
1749#define EMC_SMCR_BL_MASK (0x03 << EMC_SMCR_BL_BIT) /* Bus Latency? */
1750  #define EMC_SMCR_BL_4 (0 << EMC_SMCR_BL_BIT)
1751  #define EMC_SMCR_BL_8 (1 << EMC_SMCR_BL_BIT)
1752  #define EMC_SMCR_BL_16 (2 << EMC_SMCR_BL_BIT)
1753  #define EMC_SMCR_BL_32 (3 << EMC_SMCR_BL_BIT)
1754#define EMC_SMCR_SMT (1 << 0)
1755
1756/* Static Memory Bank Addr Config Reg */
1757#define EMC_SACR_BASE_BIT 8
1758#define EMC_SACR_BASE_MASK (0xff << EMC_SACR_BASE_BIT)
1759#define EMC_SACR_MASK_BIT 0
1760#define EMC_SACR_MASK_MASK (0xff << EMC_SACR_MASK_BIT)
1761
1762/* NAND Flash Control/Status Register */
1763#define EMC_NFCSR_NFCE4 (1 << 7) /* NAND Flash Enable */
1764#define EMC_NFCSR_NFE4 (1 << 6) /* NAND Flash FCE# Assertion Enable */
1765#define EMC_NFCSR_NFCE3 (1 << 5)
1766#define EMC_NFCSR_NFE3 (1 << 4)
1767#define EMC_NFCSR_NFCE2 (1 << 3)
1768#define EMC_NFCSR_NFE2 (1 << 2)
1769#define EMC_NFCSR_NFCE1 (1 << 1)
1770#define EMC_NFCSR_NFE1 (1 << 0)
1771#define EMC_NFCSR_NFE(n) (1 << (((n)-1)*2))
1772#define EMC_NFCSR_NFCE(n) (1 << (((n)*2)-1))
1773
1774/* NAND Flash ECC Control Register */
1775#define EMC_NFECR_PRDY (1 << 4) /* Parity Ready */
1776#define EMC_NFECR_RS_DECODING (0 << 3) /* RS is in decoding phase */
1777#define EMC_NFECR_RS_ENCODING (1 << 3) /* RS is in encoding phase */
1778#define EMC_NFECR_HAMMING (0 << 2) /* Select HAMMING Correction Algorithm */
1779#define EMC_NFECR_RS (1 << 2) /* Select RS Correction Algorithm */
1780#define EMC_NFECR_ERST (1 << 1) /* ECC Reset */
1781#define EMC_NFECR_ECCE (1 << 0) /* ECC Enable */
1782
1783/* NAND Flash ECC Data Register */
1784#define EMC_NFECC_ECC2_BIT 16
1785