FastDelegate.h 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  1. #pragma once
  2. // FastDelegate.h
  3. // Efficient delegates in C++ that generate only two lines of asm code!
  4. // Documentation is found at http://www.codeproject.com/cpp/FastDelegate.asp
  5. //
  6. // - Don Clugston, Mar 2004.
  7. // Major contributions were made by Jody Hagins.
  8. // History:
  9. // 24-Apr-04 1.0 * Submitted to CodeProject.
  10. // 28-Apr-04 1.1 * Prevent most unsafe uses of evil static function hack.
  11. // * Improved syntax for horrible_cast (thanks Paul Bludov).
  12. // * Tested on Metrowerks MWCC and Intel ICL (IA32)
  13. // * Compiled, but not run, on Comeau C++ and Intel Itanium ICL.
  14. // 27-Jun-04 1.2 * Now works on Borland C++ Builder 5.5
  15. // * Now works on /clr "managed C++" code on VC7, VC7.1
  16. // * Comeau C++ now compiles without warnings.
  17. // * Prevent the virtual inheritance case from being used on
  18. // VC6 and earlier, which generate incorrect code.
  19. // * Improved warning and error messages. Non-standard hacks
  20. // now have compile-time checks to make them safer.
  21. // * implicit_cast used instead of static_cast in many cases.
  22. // * If calling a const member function, a const class pointer can be used.
  23. // * MakeDelegate() global helper function added to simplify pass-by-value.
  24. // * Added fastdelegate.clear()
  25. // 16-Jul-04 1.2.1* Workaround for gcc bug (const member function pointers in templates)
  26. // 30-Oct-04 1.3 * Support for (non-void) return values.
  27. // * No more workarounds in client code!
  28. // MSVC and Intel now use a clever hack invented by John Dlugosz:
  29. // - The FASTDELEGATEDECLARE workaround is no longer necessary.
  30. // - No more warning messages for VC6
  31. // * Less use of macros. Error messages should be more comprehensible.
  32. // * Added include guards
  33. // * Added FastDelegate::empty() to test if invocation is safe (Thanks Neville Franks).
  34. // * Now tested on VS 2005 Express Beta, PGI C++
  35. // 24-Dec-04 1.4 * Added DelegateMemento, to allow collections of disparate delegates.
  36. // * <,>,<=,>= comparison operators to allow storage in ordered containers.
  37. // * Substantial reduction of code size, especially the 'Closure' class.
  38. // * Standardised all the compiler-specific workarounds.
  39. // * MFP conversion now works for CodePlay (but not yet supported in the full code).
  40. // * Now compiles without warnings on _any_ supported compiler, including BCC 5.5.1
  41. // * New syntax: FastDelegate< int (char *, double) >.
  42. // 14-Feb-05 1.4.1* Now treats =0 as equivalent to .clear(), ==0 as equivalent to .empty(). (Thanks elfric).
  43. // * Now tested on Intel ICL for AMD64, VS2005 Beta for AMD64 and Itanium.
  44. // 30-Mar-05 1.5 * Safebool idiom: "if (dg)" is now equivalent to "if (!dg.empty())"
  45. // * Fully supported by CodePlay VectorC
  46. // * Bugfix for Metrowerks: empty() was buggy because a valid MFP can be 0 on MWCC!
  47. // * More optimal assignment,== and != operators for static function pointers.
  48. #ifndef FASTDELEGATE_H
  49. #define FASTDELEGATE_H
  50. #if _MSC_VER > 1000
  51. #pragma once
  52. #endif // _MSC_VER > 1000
  53. #include <memory.h> // to allow <,> comparisons
  54. ////////////////////////////////////////////////////////////////////////////////
  55. // Configuration options
  56. //
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // Uncomment the following #define for optimally-sized delegates.
  59. // In this case, the generated asm code is almost identical to the code you'd get
  60. // if the compiler had native support for delegates.
  61. // It will not work on systems where sizeof(dataptr) < sizeof(codeptr).
  62. // Thus, it will not work for DOS compilers using the medium model.
  63. // It will also probably fail on some DSP systems.
  64. #define FASTDELEGATE_USESTATICFUNCTIONHACK
  65. // Uncomment the next line to allow function declarator syntax.
  66. // It is automatically enabled for those compilers where it is known to work.
  67. //#define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  68. ////////////////////////////////////////////////////////////////////////////////
  69. // Compiler identification for workarounds
  70. //
  71. ////////////////////////////////////////////////////////////////////////////////
  72. // Compiler identification. It's not easy to identify Visual C++ because
  73. // many vendors fraudulently define Microsoft's identifiers.
  74. #if defined(_MSC_VER) && !defined(__MWERKS__) && !defined(__VECTOR_C) && !defined(__ICL) && !defined(__BORLANDC__)
  75. #define FASTDLGT_ISMSVC
  76. #if (_MSC_VER <1300) // Many workarounds are required for VC6.
  77. #define FASTDLGT_VC6
  78. #pragma warning(disable:4786) // disable this ridiculous warning
  79. #endif
  80. #endif
  81. // Does the compiler uses Microsoft's member function pointer structure?
  82. // If so, it needs special treatment.
  83. // Metrowerks CodeWarrior, Intel, and CodePlay fraudulently define Microsoft's
  84. // identifier, _MSC_VER. We need to filter Metrowerks out.
  85. #if defined(_MSC_VER) && !defined(__MWERKS__)
  86. #define FASTDLGT_MICROSOFT_MFP
  87. #if !defined(__VECTOR_C)
  88. // CodePlay doesn't have the __single/multi/virtual_inheritance keywords
  89. #define FASTDLGT_HASINHERITANCE_KEYWORDS
  90. #endif
  91. #endif
  92. // Does it allow function declarator syntax? The following compilers are known to work:
  93. #if defined(FASTDLGT_ISMSVC) && (_MSC_VER >=1310) // VC 7.1
  94. #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  95. #endif
  96. // Gcc(2.95+), and versions of Digital Mars, Intel and Comeau in common use.
  97. #if defined (__DMC__) || defined(__GNUC__) || defined(__ICL) || defined(__COMO__)
  98. #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  99. #endif
  100. // It works on Metrowerks MWCC 3.2.2. From boost.Config it should work on earlier ones too.
  101. #if defined (__MWERKS__)
  102. #define FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  103. #endif
  104. #ifdef __GNUC__ // Workaround GCC bug #8271
  105. // At present, GCC doesn't recognize constness of MFPs in templates
  106. #define FASTDELEGATE_GCC_BUG_8271
  107. #endif
  108. ////////////////////////////////////////////////////////////////////////////////
  109. // General tricks used in this code
  110. //
  111. // (a) Error messages are generated by typdefing an array of negative size to
  112. // generate compile-time errors.
  113. // (b) Warning messages on MSVC are generated by declaring unused variables, and
  114. // enabling the "variable XXX is never used" warning.
  115. // (c) Unions are used in a few compiler-specific cases to perform illegal casts.
  116. // (d) For Microsoft and Intel, when adjusting the 'this' pointer, it's cast to
  117. // (char *) first to ensure that the correct number of *bytes* are added.
  118. //
  119. ////////////////////////////////////////////////////////////////////////////////
  120. // Helper templates
  121. //
  122. ////////////////////////////////////////////////////////////////////////////////
  123. namespace fastdelegate {
  124. namespace detail { // we'll hide the implementation details in a nested namespace.
  125. // implicit_cast< >
  126. // I believe this was originally going to be in the C++ standard but
  127. // was left out by accident. It's even milder than static_cast.
  128. // I use it instead of static_cast<> to emphasize that I'm not doing
  129. // anything nasty.
  130. // Usage is identical to static_cast<>
  131. template <class OutputClass, class InputClass>
  132. inline OutputClass implicit_cast(InputClass input) {
  133. return input;
  134. }
  135. // horrible_cast< >
  136. // This is truly evil. It completely subverts C++'s type system, allowing you
  137. // to cast from any class to any other class. Technically, using a union
  138. // to perform the cast is undefined behaviour (even in C). But we can see if
  139. // it is OK by checking that the union is the same size as each of its members.
  140. // horrible_cast<> should only be used for compiler-specific workarounds.
  141. // Usage is identical to reinterpret_cast<>.
  142. // This union is declared outside the horrible_cast because BCC 5.5.1
  143. // can't inline a function with a nested class, and gives a warning.
  144. template <class OutputClass, class InputClass>
  145. union horrible_union {
  146. OutputClass out;
  147. InputClass in;
  148. };
  149. template <class OutputClass, class InputClass>
  150. inline OutputClass horrible_cast(const InputClass input) {
  151. horrible_union<OutputClass, InputClass> u;
  152. // Cause a compile-time error if in, out and u are not the same size.
  153. // If the compile fails here, it means the compiler has peculiar
  154. // unions which would prevent the cast from working.
  155. typedef int ERROR_CantUseHorrible_cast[sizeof(InputClass) == sizeof(u)
  156. && sizeof(InputClass) == sizeof(OutputClass) ? 1 : -1];
  157. u.in = input;
  158. return u.out;
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////
  161. // Workarounds
  162. //
  163. ////////////////////////////////////////////////////////////////////////////////
  164. // Backwards compatibility: This macro used to be necessary in the virtual inheritance
  165. // case for Intel and Microsoft. Now it just forward-declares the class.
  166. #define FASTDELEGATEDECLARE(CLASSNAME) class CLASSNAME;
  167. // Prevent use of the static function hack with the DOS medium model.
  168. #ifdef __MEDIUM__
  169. #undef FASTDELEGATE_USESTATICFUNCTIONHACK
  170. #endif
  171. // DefaultVoid - a workaround for 'void' templates in VC6.
  172. //
  173. // (1) VC6 and earlier do not allow 'void' as a default template argument.
  174. // (2) They also doesn't allow you to return 'void' from a function.
  175. //
  176. // Workaround for (1): Declare a dummy type 'DefaultVoid' which we use
  177. // when we'd like to use 'void'. We convert it into 'void' and back
  178. // using the templates DefaultVoidToVoid<> and VoidToDefaultVoid<>.
  179. // Workaround for (2): On VC6, the code for calling a void function is
  180. // identical to the code for calling a non-void function in which the
  181. // return value is never used, provided the return value is returned
  182. // in the EAX register, rather than on the stack.
  183. // This is true for most fundamental types such as int, enum, void *.
  184. // Const void * is the safest option since it doesn't participate
  185. // in any automatic conversions. But on a 16-bit compiler it might
  186. // cause extra code to be generated, so we disable it for all compilers
  187. // except for VC6 (and VC5).
  188. #ifdef FASTDLGT_VC6
  189. // VC6 workaround
  190. typedef const void* DefaultVoid;
  191. #else
  192. // On any other compiler, just use a normal void.
  193. typedef void DefaultVoid;
  194. #endif
  195. // Translate from 'DefaultVoid' to 'void'.
  196. // Everything else is unchanged
  197. template <class T>
  198. struct DefaultVoidToVoid { typedef T type; };
  199. template <>
  200. struct DefaultVoidToVoid<DefaultVoid> { typedef void type; };
  201. // Translate from 'void' into 'DefaultVoid'
  202. // Everything else is unchanged
  203. template <class T>
  204. struct VoidToDefaultVoid { typedef T type; };
  205. template <>
  206. struct VoidToDefaultVoid<void> { typedef DefaultVoid type; };
  207. ////////////////////////////////////////////////////////////////////////////////
  208. // Fast Delegates, part 1:
  209. //
  210. // Conversion of member function pointer to a standard form
  211. //
  212. ////////////////////////////////////////////////////////////////////////////////
  213. // GenericClass is a fake class, ONLY used to provide a type.
  214. // It is vitally important that it is never defined, so that the compiler doesn't
  215. // think it can optimize the invocation. For example, Borland generates simpler
  216. // code if it knows the class only uses single inheritance.
  217. // Compilers using Microsoft's structure need to be treated as a special case.
  218. #ifdef FASTDLGT_MICROSOFT_MFP
  219. #ifdef FASTDLGT_HASINHERITANCE_KEYWORDS
  220. // For Microsoft and Intel, we want to ensure that it's the most efficient type of MFP
  221. // (4 bytes), even when the /vmg option is used. Declaring an empty class
  222. // would give 16 byte pointers in this case....
  223. class __single_inheritance GenericClass;
  224. #endif
  225. // ...but for Codeplay, an empty class *always* gives 4 byte pointers.
  226. // If compiled with the /clr option ("managed C++"), the JIT compiler thinks
  227. // it needs to load GenericClass before it can call any of its functions,
  228. // (compiles OK but crashes at runtime!), so we need to declare an
  229. // empty class to make it happy.
  230. // Codeplay and VC4 can't cope with the unknown_inheritance case either.
  231. class GenericClass {};
  232. #else
  233. class GenericClass;
  234. #endif
  235. // The size of a single inheritance member function pointer.
  236. const int SINGLE_MEMFUNCPTR_SIZE = sizeof(void (GenericClass::*)());
  237. // SimplifyMemFunc< >::Convert()
  238. //
  239. // A template function that converts an arbitrary member function pointer into the
  240. // simplest possible form of member function pointer, using a supplied 'this' pointer.
  241. // According to the standard, this can be done legally with reinterpret_cast<>.
  242. // For (non-standard) compilers which use member function pointers which vary in size
  243. // depending on the class, we need to use knowledge of the internal structure of a
  244. // member function pointer, as used by the compiler. Template specialization is used
  245. // to distinguish between the sizes. Because some compilers don't support partial
  246. // template specialisation, I use full specialisation of a wrapper struct.
  247. // general case -- don't know how to convert it. Force a compile failure
  248. template <int N>
  249. struct SimplifyMemFunc {
  250. template <class X, class XFuncType, class GenericMemFuncType>
  251. inline static GenericClass* Convert(X* pthis, XFuncType function_to_bind,
  252. GenericMemFuncType& bound_func) {
  253. // Unsupported member function type -- force a compile failure.
  254. // (it's illegal to have a array with negative size).
  255. typedef char ERROR_Unsupported_member_function_pointer_on_this_compiler[N - 100];
  256. return 0;
  257. }
  258. };
  259. // For compilers where all member func ptrs are the same size, everything goes here.
  260. // For non-standard compilers, only single_inheritance classes go here.
  261. template <>
  262. struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE> {
  263. template <class X, class XFuncType, class GenericMemFuncType>
  264. inline static GenericClass* Convert(X* pthis, XFuncType function_to_bind,
  265. GenericMemFuncType& bound_func) {
  266. #if defined __DMC__
  267. // Digital Mars doesn't allow you to cast between abitrary PMF's,
  268. // even though the standard says you can. The 32-bit compiler lets you
  269. // static_cast through an int, but the DOS compiler doesn't.
  270. bound_func = horrible_cast<GenericMemFuncType>(function_to_bind);
  271. #else
  272. bound_func = reinterpret_cast<GenericMemFuncType>(function_to_bind);
  273. #endif
  274. return reinterpret_cast<GenericClass*>(pthis);
  275. }
  276. };
  277. ////////////////////////////////////////////////////////////////////////////////
  278. // Fast Delegates, part 1b:
  279. //
  280. // Workarounds for Microsoft and Intel
  281. //
  282. ////////////////////////////////////////////////////////////////////////////////
  283. // Compilers with member function pointers which violate the standard (MSVC, Intel, Codeplay),
  284. // need to be treated as a special case.
  285. #ifdef FASTDLGT_MICROSOFT_MFP
  286. // We use unions to perform horrible_casts. I would like to use #pragma pack(push, 1)
  287. // at the start of each function for extra safety, but VC6 seems to ICE
  288. // intermittently if you do this inside a template.
  289. // __multiple_inheritance classes go here
  290. // Nasty hack for Microsoft and Intel (IA32 and Itanium)
  291. template<>
  292. struct SimplifyMemFunc< SINGLE_MEMFUNCPTR_SIZE + sizeof(int) > {
  293. template <class X, class XFuncType, class GenericMemFuncType>
  294. inline static GenericClass* Convert(X* pthis, XFuncType function_to_bind,
  295. GenericMemFuncType& bound_func) {
  296. // We need to use a horrible_cast to do this conversion.
  297. // In MSVC, a multiple inheritance member pointer is internally defined as:
  298. union {
  299. XFuncType func;
  300. struct {
  301. GenericMemFuncType funcaddress; // points to the actual member function
  302. int delta; // #BYTES to be added to the 'this' pointer
  303. }s;
  304. } u;
  305. // Check that the horrible_cast will work
  306. typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind) == sizeof(u.s) ? 1 : -1];
  307. u.func = function_to_bind;
  308. bound_func = u.s.funcaddress;
  309. return reinterpret_cast<GenericClass*>(reinterpret_cast<char*>(pthis) + u.s.delta);
  310. }
  311. };
  312. // virtual inheritance is a real nuisance. It's inefficient and complicated.
  313. // On MSVC and Intel, there isn't enough information in the pointer itself to
  314. // enable conversion to a closure pointer. Earlier versions of this code didn't
  315. // work for all cases, and generated a compile-time error instead.
  316. // But a very clever hack invented by John M. Dlugosz solves this problem.
  317. // My code is somewhat different to his: I have no asm code, and I make no
  318. // assumptions about the calling convention that is used.
  319. // In VC++ and ICL, a virtual_inheritance member pointer
  320. // is internally defined as:
  321. struct MicrosoftVirtualMFP {
  322. void (GenericClass::* codeptr)(); // points to the actual member function
  323. int delta; // #bytes to be added to the 'this' pointer
  324. int vtable_index; // or 0 if no virtual inheritance
  325. };
  326. // The CRUCIAL feature of Microsoft/Intel MFPs which we exploit is that the
  327. // m_codeptr member is *always* called, regardless of the values of the other
  328. // members. (This is *not* true for other compilers, eg GCC, which obtain the
  329. // function address from the vtable if a virtual function is being called).
  330. // Dlugosz's trick is to make the codeptr point to a probe function which
  331. // returns the 'this' pointer that was used.
  332. // Define a generic class that uses virtual inheritance.
  333. // It has a trival member function that returns the value of the 'this' pointer.
  334. struct GenericVirtualClass : virtual public GenericClass
  335. {
  336. typedef GenericVirtualClass* (GenericVirtualClass::* ProbePtrType)();
  337. GenericVirtualClass* GetThis() { return this; }
  338. };
  339. // __virtual_inheritance classes go here
  340. template <>
  341. struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 2 * sizeof(int) >
  342. {
  343. template <class X, class XFuncType, class GenericMemFuncType>
  344. inline static GenericClass* Convert(X* pthis, XFuncType function_to_bind,
  345. GenericMemFuncType& bound_func) {
  346. union {
  347. XFuncType func;
  348. GenericClass* (X::* ProbeFunc)();
  349. MicrosoftVirtualMFP s;
  350. } u;
  351. u.func = function_to_bind;
  352. bound_func = reinterpret_cast<GenericMemFuncType>(u.s.codeptr);
  353. union {
  354. GenericVirtualClass::ProbePtrType virtfunc;
  355. MicrosoftVirtualMFP s;
  356. } u2;
  357. // Check that the horrible_cast<>s will work
  358. typedef int ERROR_CantUsehorrible_cast[sizeof(function_to_bind) == sizeof(u.s)
  359. && sizeof(function_to_bind) == sizeof(u.ProbeFunc)
  360. && sizeof(u2.virtfunc) == sizeof(u2.s) ? 1 : -1];
  361. // Unfortunately, taking the address of a MF prevents it from being inlined, so
  362. // this next line can't be completely optimised away by the compiler.
  363. u2.virtfunc = &GenericVirtualClass::GetThis;
  364. u.s.codeptr = u2.s.codeptr;
  365. return (pthis->*u.ProbeFunc)();
  366. }
  367. };
  368. #if (_MSC_VER <1300)
  369. // Nasty hack for Microsoft Visual C++ 6.0
  370. // unknown_inheritance classes go here
  371. // There is a compiler bug in MSVC6 which generates incorrect code in this case!!
  372. template <>
  373. struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3 * sizeof(int) >
  374. {
  375. template <class X, class XFuncType, class GenericMemFuncType>
  376. inline static GenericClass* Convert(X* pthis, XFuncType function_to_bind,
  377. GenericMemFuncType& bound_func) {
  378. // There is an apalling but obscure compiler bug in MSVC6 and earlier:
  379. // vtable_index and 'vtordisp' are always set to 0 in the
  380. // unknown_inheritance case!
  381. // This means that an incorrect function could be called!!!
  382. // Compiling with the /vmg option leads to potentially incorrect code.
  383. // This is probably the reason that the IDE has a user interface for specifying
  384. // the /vmg option, but it is disabled - you can only specify /vmg on
  385. // the command line. In VC1.5 and earlier, the compiler would ICE if it ever
  386. // encountered this situation.
  387. // It is OK to use the /vmg option if /vmm or /vms is specified.
  388. // Fortunately, the wrong function is only called in very obscure cases.
  389. // It only occurs when a derived class overrides a virtual function declared
  390. // in a virtual base class, and the member function
  391. // points to the *Derived* version of that function. The problem can be
  392. // completely averted in 100% of cases by using the *Base class* for the
  393. // member fpointer. Ie, if you use the base class as an interface, you'll
  394. // stay out of trouble.
  395. // Occasionally, you might want to point directly to a derived class function
  396. // that isn't an override of a base class. In this case, both vtable_index
  397. // and 'vtordisp' are zero, but a virtual_inheritance pointer will be generated.
  398. // We can generate correct code in this case. To prevent an incorrect call from
  399. // ever being made, on MSVC6 we generate a warning, and call a function to
  400. // make the program crash instantly.
  401. typedef char ERROR_VC6CompilerBug[-100];
  402. return 0;
  403. }
  404. };
  405. #else
  406. // Nasty hack for Microsoft and Intel (IA32 and Itanium)
  407. // unknown_inheritance classes go here
  408. // This is probably the ugliest bit of code I've ever written. Look at the casts!
  409. // There is a compiler bug in MSVC6 which prevents it from using this code.
  410. template <>
  411. struct SimplifyMemFunc<SINGLE_MEMFUNCPTR_SIZE + 3 * sizeof(int) >
  412. {
  413. template <class X, class XFuncType, class GenericMemFuncType>
  414. inline static GenericClass* Convert(X* pthis, XFuncType function_to_bind,
  415. GenericMemFuncType& bound_func) {
  416. // The member function pointer is 16 bytes long. We can't use a normal cast, but
  417. // we can use a union to do the conversion.
  418. union {
  419. XFuncType func;
  420. // In VC++ and ICL, an unknown_inheritance member pointer
  421. // is internally defined as:
  422. struct {
  423. GenericMemFuncType m_funcaddress; // points to the actual member function
  424. int delta; // #bytes to be added to the 'this' pointer
  425. int vtordisp; // #bytes to add to 'this' to find the vtable
  426. int vtable_index; // or 0 if no virtual inheritance
  427. } s;
  428. } u;
  429. // Check that the horrible_cast will work
  430. typedef int ERROR_CantUsehorrible_cast[sizeof(XFuncType) == sizeof(u.s) ? 1 : -1];
  431. u.func = function_to_bind;
  432. bound_func = u.s.funcaddress;
  433. int virtual_delta = 0;
  434. if (u.s.vtable_index) { // Virtual inheritance is used
  435. // First, get to the vtable.
  436. // It is 'vtordisp' bytes from the start of the class.
  437. const int* vtable = *reinterpret_cast<const int* const*>(
  438. reinterpret_cast<const char*>(pthis) + u.s.vtordisp);
  439. // 'vtable_index' tells us where in the table we should be looking.
  440. virtual_delta = u.s.vtordisp + *reinterpret_cast<const int*>(
  441. reinterpret_cast<const char*>(vtable) + u.s.vtable_index);
  442. }
  443. // The int at 'virtual_delta' gives us the amount to add to 'this'.
  444. // Finally we can add the three components together. Phew!
  445. return reinterpret_cast<GenericClass*>(
  446. reinterpret_cast<char*>(pthis) + u.s.delta + virtual_delta);
  447. };
  448. };
  449. #endif // MSVC 7 and greater
  450. #endif // MS/Intel hacks
  451. } // namespace detail
  452. ////////////////////////////////////////////////////////////////////////////////
  453. // Fast Delegates, part 2:
  454. //
  455. // Define the delegate storage, and cope with static functions
  456. //
  457. ////////////////////////////////////////////////////////////////////////////////
  458. // DelegateMemento -- an opaque structure which can hold an arbitary delegate.
  459. // It knows nothing about the calling convention or number of arguments used by
  460. // the function pointed to.
  461. // It supplies comparison operators so that it can be stored in STL collections.
  462. // It cannot be set to anything other than null, nor invoked directly:
  463. // it must be converted to a specific delegate.
  464. // Implementation:
  465. // There are two possible implementations: the Safe method and the Evil method.
  466. // DelegateMemento - Safe version
  467. //
  468. // This implementation is standard-compliant, but a bit tricky.
  469. // A static function pointer is stored inside the class.
  470. // Here are the valid values:
  471. // +-- Static pointer --+--pThis --+-- pMemFunc-+-- Meaning------+
  472. // | 0 | 0 | 0 | Empty |
  473. // | !=0 |(dontcare)| Invoker | Static function|
  474. // | 0 | !=0 | !=0* | Method call |
  475. // +--------------------+----------+------------+----------------+
  476. // * For Metrowerks, this can be 0. (first virtual function in a
  477. // single_inheritance class).
  478. // When stored stored inside a specific delegate, the 'dontcare' entries are replaced
  479. // with a reference to the delegate itself. This complicates the = and == operators
  480. // for the delegate class.
  481. // DelegateMemento - Evil version
  482. //
  483. // For compilers where data pointers are at least as big as code pointers, it is
  484. // possible to store the function pointer in the this pointer, using another
  485. // horrible_cast. In this case the DelegateMemento implementation is simple:
  486. // +--pThis --+-- pMemFunc-+-- Meaning---------------------+
  487. // | 0 | 0 | Empty |
  488. // | !=0 | !=0* | Static function or method call|
  489. // +----------+------------+-------------------------------+
  490. // * For Metrowerks, this can be 0. (first virtual function in a
  491. // single_inheritance class).
  492. // Note that the Sun C++ and MSVC documentation explicitly state that they
  493. // support static_cast between void * and function pointers.
  494. class DelegateMemento {
  495. protected:
  496. // the data is protected, not private, because many
  497. // compilers have problems with template friends.
  498. typedef void (detail::GenericClass::* GenericMemFuncType)(); // arbitrary MFP.
  499. detail::GenericClass* m_pthis;
  500. GenericMemFuncType m_pFunction;
  501. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  502. typedef void (*GenericFuncPtr)(); // arbitrary code pointer
  503. GenericFuncPtr m_pStaticFunction;
  504. #endif
  505. public:
  506. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  507. DelegateMemento() : m_pthis(0), m_pFunction(0), m_pStaticFunction(0) {};
  508. void clear() {
  509. m_pthis = 0; m_pFunction = 0; m_pStaticFunction = 0;
  510. }
  511. #else
  512. DelegateMemento() : m_pthis(0), m_pFunction(0) {};
  513. void clear() { m_pthis = 0; m_pFunction = 0; }
  514. #endif
  515. public:
  516. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  517. inline bool IsEqual(const DelegateMemento& x) const {
  518. // We have to cope with the static function pointers as a special case
  519. if (m_pFunction != x.m_pFunction) return false;
  520. // the static function ptrs must either both be equal, or both be 0.
  521. if (m_pStaticFunction != x.m_pStaticFunction) return false;
  522. if (m_pStaticFunction != 0) return m_pthis == x.m_pthis;
  523. else return true;
  524. }
  525. #else // Evil Method
  526. inline bool IsEqual(const DelegateMemento& x) const {
  527. return m_pthis == x.m_pthis && m_pFunction == x.m_pFunction;
  528. }
  529. #endif
  530. // Provide a strict weak ordering for DelegateMementos.
  531. inline bool IsLess(const DelegateMemento& right) const {
  532. // deal with static function pointers first
  533. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  534. if (m_pStaticFunction != 0 || right.m_pStaticFunction != 0)
  535. return m_pStaticFunction < right.m_pStaticFunction;
  536. #endif
  537. if (m_pthis != right.m_pthis) return m_pthis < right.m_pthis;
  538. // There are no ordering operators for member function pointers,
  539. // but we can fake one by comparing each byte. The resulting ordering is
  540. // arbitrary (and compiler-dependent), but it permits storage in ordered STL containers.
  541. return memcmp(&m_pFunction, &right.m_pFunction, sizeof(m_pFunction)) < 0;
  542. }
  543. // BUGFIX (Mar 2005):
  544. // We can't just compare m_pFunction because on Metrowerks,
  545. // m_pFunction can be zero even if the delegate is not empty!
  546. inline bool operator ! () const // Is it bound to anything?
  547. {
  548. return m_pthis == 0 && m_pFunction == 0;
  549. }
  550. inline bool empty() const // Is it bound to anything?
  551. {
  552. return m_pthis == 0 && m_pFunction == 0;
  553. }
  554. public:
  555. DelegateMemento& operator = (const DelegateMemento& right) {
  556. SetMementoFrom(right);
  557. return *this;
  558. }
  559. inline bool operator <(const DelegateMemento& right) {
  560. return IsLess(right);
  561. }
  562. inline bool operator >(const DelegateMemento& right) {
  563. return right.IsLess(*this);
  564. }
  565. DelegateMemento(const DelegateMemento& right) :
  566. m_pFunction(right.m_pFunction), m_pthis(right.m_pthis)
  567. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  568. , m_pStaticFunction(right.m_pStaticFunction)
  569. #endif
  570. {
  571. }
  572. protected:
  573. void SetMementoFrom(const DelegateMemento& right) {
  574. m_pFunction = right.m_pFunction;
  575. m_pthis = right.m_pthis;
  576. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  577. m_pStaticFunction = right.m_pStaticFunction;
  578. #endif
  579. }
  580. };
  581. // ClosurePtr<>
  582. //
  583. // A private wrapper class that adds function signatures to DelegateMemento.
  584. // It's the class that does most of the actual work.
  585. // The signatures are specified by:
  586. // GenericMemFunc: must be a type of GenericClass member function pointer.
  587. // StaticFuncPtr: must be a type of function pointer with the same signature
  588. // as GenericMemFunc.
  589. // UnvoidStaticFuncPtr: is the same as StaticFuncPtr, except on VC6
  590. // where it never returns void (returns DefaultVoid instead).
  591. // An outer class, FastDelegateN<>, handles the invoking and creates the
  592. // necessary typedefs.
  593. // This class does everything else.
  594. namespace detail {
  595. template < class GenericMemFunc, class StaticFuncPtr, class UnvoidStaticFuncPtr>
  596. class ClosurePtr : public DelegateMemento {
  597. public:
  598. // These functions are for setting the delegate to a member function.
  599. // Here's the clever bit: we convert an arbitrary member function into a
  600. // standard form. XMemFunc should be a member function of class X, but I can't
  601. // enforce that here. It needs to be enforced by the wrapper class.
  602. template < class X, class XMemFunc >
  603. inline void bindmemfunc(X* pthis, XMemFunc function_to_bind) {
  604. m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
  605. ::Convert(pthis, function_to_bind, m_pFunction);
  606. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  607. m_pStaticFunction = 0;
  608. #endif
  609. }
  610. // For const member functions, we only need a const class pointer.
  611. // Since we know that the member function is const, it's safe to
  612. // remove the const qualifier from the 'this' pointer with a const_cast.
  613. // VC6 has problems if we just overload 'bindmemfunc', so we give it a different name.
  614. template < class X, class XMemFunc>
  615. inline void bindconstmemfunc(const X* pthis, XMemFunc function_to_bind) {
  616. m_pthis = SimplifyMemFunc< sizeof(function_to_bind) >
  617. ::Convert(const_cast<X*>(pthis), function_to_bind, m_pFunction);
  618. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  619. m_pStaticFunction = 0;
  620. #endif
  621. }
  622. #ifdef FASTDELEGATE_GCC_BUG_8271 // At present, GCC doesn't recognize constness of MFPs in templates
  623. template < class X, class XMemFunc>
  624. inline void bindmemfunc(const X* pthis, XMemFunc function_to_bind) {
  625. bindconstmemfunc(pthis, function_to_bind);
  626. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  627. m_pStaticFunction = 0;
  628. #endif
  629. }
  630. #endif
  631. // These functions are required for invoking the stored function
  632. inline GenericClass* GetClosureThis() const { return m_pthis; }
  633. inline GenericMemFunc GetClosureMemPtr() const { return reinterpret_cast<GenericMemFunc>(m_pFunction); }
  634. // There are a few ways of dealing with static function pointers.
  635. // There's a standard-compliant, but tricky method.
  636. // There's also a straightforward hack, that won't work on DOS compilers using the
  637. // medium memory model. It's so evil that I can't recommend it, but I've
  638. // implemented it anyway because it produces very nice asm code.
  639. #if !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  640. // ClosurePtr<> - Safe version
  641. //
  642. // This implementation is standard-compliant, but a bit tricky.
  643. // I store the function pointer inside the class, and the delegate then
  644. // points to itself. Whenever the delegate is copied, these self-references
  645. // must be transformed, and this complicates the = and == operators.
  646. public:
  647. // The next two functions are for operator ==, =, and the copy constructor.
  648. // We may need to convert the m_pthis pointers, so that
  649. // they remain as self-references.
  650. template< class DerivedClass >
  651. inline void CopyFrom(DerivedClass* pParent, const DelegateMemento& x) {
  652. SetMementoFrom(x);
  653. if (m_pStaticFunction != 0) {
  654. // transform self references...
  655. m_pthis = reinterpret_cast<GenericClass*>(pParent);
  656. }
  657. }
  658. // For static functions, the 'static_function_invoker' class in the parent
  659. // will be called. The parent then needs to call GetStaticFunction() to find out
  660. // the actual function to invoke.
  661. template < class DerivedClass, class ParentInvokerSig >
  662. inline void bindstaticfunc(DerivedClass* pParent, ParentInvokerSig static_function_invoker,
  663. StaticFuncPtr function_to_bind) {
  664. if (function_to_bind == 0) { // cope with assignment to 0
  665. m_pFunction = 0;
  666. }
  667. else {
  668. bindmemfunc(pParent, static_function_invoker);
  669. }
  670. m_pStaticFunction = reinterpret_cast<GenericFuncPtr>(function_to_bind);
  671. }
  672. inline UnvoidStaticFuncPtr GetStaticFunction() const {
  673. return reinterpret_cast<UnvoidStaticFuncPtr>(m_pStaticFunction);
  674. }
  675. #else
  676. // ClosurePtr<> - Evil version
  677. //
  678. // For compilers where data pointers are at least as big as code pointers, it is
  679. // possible to store the function pointer in the this pointer, using another
  680. // horrible_cast. Invocation isn't any faster, but it saves 4 bytes, and
  681. // speeds up comparison and assignment. If C++ provided direct language support
  682. // for delegates, they would produce asm code that was almost identical to this.
  683. // Note that the Sun C++ and MSVC documentation explicitly state that they
  684. // support static_cast between void * and function pointers.
  685. template< class DerivedClass >
  686. inline void CopyFrom(DerivedClass* pParent, const DelegateMemento& right) {
  687. SetMementoFrom(right);
  688. }
  689. // For static functions, the 'static_function_invoker' class in the parent
  690. // will be called. The parent then needs to call GetStaticFunction() to find out
  691. // the actual function to invoke.
  692. // ******** EVIL, EVIL CODE! *******
  693. template < class DerivedClass, class ParentInvokerSig>
  694. inline void bindstaticfunc(DerivedClass* pParent, ParentInvokerSig static_function_invoker,
  695. StaticFuncPtr function_to_bind) {
  696. if (function_to_bind == 0) { // cope with assignment to 0
  697. m_pFunction = 0;
  698. }
  699. else {
  700. // We'll be ignoring the 'this' pointer, but we need to make sure we pass
  701. // a valid value to bindmemfunc().
  702. bindmemfunc(pParent, static_function_invoker);
  703. }
  704. // WARNING! Evil hack. We store the function in the 'this' pointer!
  705. // Ensure that there's a compilation failure if function pointers
  706. // and data pointers have different sizes.
  707. // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
  708. typedef int ERROR_CantUseEvilMethod[sizeof(GenericClass*) == sizeof(function_to_bind) ? 1 : -1];
  709. m_pthis = horrible_cast<GenericClass*>(function_to_bind);
  710. // MSVC, SunC++ and DMC accept the following (non-standard) code:
  711. // m_pthis = static_cast<GenericClass *>(static_cast<void *>(function_to_bind));
  712. // BCC32, Comeau and DMC accept this method. MSVC7.1 needs __int64 instead of long
  713. // m_pthis = reinterpret_cast<GenericClass *>(reinterpret_cast<long>(function_to_bind));
  714. }
  715. // ******** EVIL, EVIL CODE! *******
  716. // This function will be called with an invalid 'this' pointer!!
  717. // We're just returning the 'this' pointer, converted into
  718. // a function pointer!
  719. inline UnvoidStaticFuncPtr GetStaticFunction() const {
  720. // Ensure that there's a compilation failure if function pointers
  721. // and data pointers have different sizes.
  722. // If you get this error, you need to #undef FASTDELEGATE_USESTATICFUNCTIONHACK.
  723. typedef int ERROR_CantUseEvilMethod[sizeof(UnvoidStaticFuncPtr) == sizeof(this) ? 1 : -1];
  724. return horrible_cast<UnvoidStaticFuncPtr>(this);
  725. }
  726. #endif // !defined(FASTDELEGATE_USESTATICFUNCTIONHACK)
  727. // Does the closure contain this static function?
  728. inline bool IsEqualToStaticFuncPtr(StaticFuncPtr funcptr) {
  729. if (funcptr == 0) return empty();
  730. // For the Evil method, if it doesn't actually contain a static function, this will return an arbitrary
  731. // value that is not equal to any valid function pointer.
  732. else return funcptr == reinterpret_cast<StaticFuncPtr>(GetStaticFunction());
  733. }
  734. };
  735. } // namespace detail
  736. ////////////////////////////////////////////////////////////////////////////////
  737. // Fast Delegates, part 3:
  738. //
  739. // Wrapper classes to ensure type safety
  740. //
  741. ////////////////////////////////////////////////////////////////////////////////
  742. // Once we have the member function conversion templates, it's easy to make the
  743. // wrapper classes. So that they will work with as many compilers as possible,
  744. // the classes are of the form
  745. // FastDelegate3<int, char *, double>
  746. // They can cope with any combination of parameters. The max number of parameters
  747. // allowed is 8, but it is trivial to increase this limit.
  748. // Note that we need to treat const member functions seperately.
  749. // All this class does is to enforce type safety, and invoke the delegate with
  750. // the correct list of parameters.
  751. // Because of the weird rule about the class of derived member function pointers,
  752. // you sometimes need to apply a downcast to the 'this' pointer.
  753. // This is the reason for the use of "implicit_cast<X*>(pthis)" in the code below.
  754. // If CDerivedClass is derived from CBaseClass, but doesn't override SimpleVirtualFunction,
  755. // without this trick you'd need to write:
  756. // MyDelegate(static_cast<CBaseClass *>(&d), &CDerivedClass::SimpleVirtualFunction);
  757. // but with the trick you can write
  758. // MyDelegate(&d, &CDerivedClass::SimpleVirtualFunction);
  759. // RetType is the type the compiler uses in compiling the template. For VC6,
  760. // it cannot be void. DesiredRetType is the real type which is returned from
  761. // all of the functions. It can be void.
  762. // Implicit conversion to "bool" is achieved using the safe_bool idiom,
  763. // using member data pointers (MDP). This allows "if (dg)..." syntax
  764. // Because some compilers (eg codeplay) don't have a unique value for a zero
  765. // MDP, an extra padding member is added to the SafeBool struct.
  766. // Some compilers (eg VC6) won't implicitly convert from 0 to an MDP, so
  767. // in that case the static function constructor is not made explicit; this
  768. // allows "if (dg==0) ..." to compile.
  769. //N=0
  770. template<class RetType = detail::DefaultVoid>
  771. class FastDelegate0 {
  772. private:
  773. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  774. typedef DesiredRetType(*StaticFunctionPtr)();
  775. typedef RetType(*UnvoidStaticFunctionPtr)();
  776. typedef RetType(detail::GenericClass::* GenericMemFn)();
  777. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  778. ClosureType m_Closure;
  779. public:
  780. // Typedefs to aid generic programming
  781. typedef FastDelegate0 type;
  782. // Construction and comparison functions
  783. FastDelegate0() { clear(); }
  784. FastDelegate0(const FastDelegate0& x) {
  785. m_Closure.CopyFrom(this, x.m_Closure);
  786. }
  787. void operator = (const FastDelegate0& x) {
  788. m_Closure.CopyFrom(this, x.m_Closure);
  789. }
  790. bool operator ==(const FastDelegate0& x) const {
  791. return m_Closure.IsEqual(x.m_Closure);
  792. }
  793. bool operator !=(const FastDelegate0& x) const {
  794. return !m_Closure.IsEqual(x.m_Closure);
  795. }
  796. bool operator <(const FastDelegate0& x) const {
  797. return m_Closure.IsLess(x.m_Closure);
  798. }
  799. bool operator >(const FastDelegate0& x) const {
  800. return x.m_Closure.IsLess(m_Closure);
  801. }
  802. // Binding to non-const member functions
  803. template < class X, class Y >
  804. FastDelegate0(Y* pthis, DesiredRetType(X::* function_to_bind)()) {
  805. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  806. }
  807. template < class X, class Y >
  808. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)()) {
  809. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  810. }
  811. // Binding to const member functions.
  812. template < class X, class Y >
  813. FastDelegate0(const Y* pthis, DesiredRetType(X::* function_to_bind)() const) {
  814. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  815. }
  816. template < class X, class Y >
  817. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)() const) {
  818. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  819. }
  820. // Static functions. We convert them into a member function call.
  821. // This constructor also provides implicit conversion
  822. FastDelegate0(DesiredRetType(*function_to_bind)()) {
  823. bind(function_to_bind);
  824. }
  825. // for efficiency, prevent creation of a temporary
  826. void operator = (DesiredRetType(*function_to_bind)()) {
  827. bind(function_to_bind);
  828. }
  829. inline void bind(DesiredRetType(*function_to_bind)()) {
  830. m_Closure.bindstaticfunc(this, &FastDelegate0::InvokeStaticFunction,
  831. function_to_bind);
  832. }
  833. // Invoke the delegate
  834. RetType operator() () const {
  835. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))();
  836. }
  837. // Implicit conversion to "bool" using the safe_bool idiom
  838. private:
  839. typedef struct SafeBoolStruct {
  840. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  841. StaticFunctionPtr m_nonzero;
  842. } UselessTypedef;
  843. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  844. public:
  845. operator unspecified_bool_type() const {
  846. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  847. }
  848. // necessary to allow ==0 to work despite the safe_bool idiom
  849. inline bool operator==(StaticFunctionPtr funcptr) {
  850. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  851. }
  852. inline bool operator!=(StaticFunctionPtr funcptr) {
  853. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  854. }
  855. inline bool operator ! () const { // Is it bound to anything?
  856. return !m_Closure;
  857. }
  858. inline bool empty() const {
  859. return !m_Closure;
  860. }
  861. void clear() { m_Closure.clear(); }
  862. // Conversion to and from the DelegateMemento storage class
  863. const DelegateMemento& GetMemento() { return m_Closure; }
  864. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  865. private: // Invoker for static functions
  866. RetType InvokeStaticFunction() const {
  867. return (*(m_Closure.GetStaticFunction()))();
  868. }
  869. };
  870. //N=1
  871. template<class Param1, class RetType = detail::DefaultVoid>
  872. class FastDelegate1 {
  873. private:
  874. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  875. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1);
  876. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1);
  877. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1);
  878. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  879. ClosureType m_Closure;
  880. public:
  881. // Typedefs to aid generic programming
  882. typedef FastDelegate1 type;
  883. // Construction and comparison functions
  884. FastDelegate1() { clear(); }
  885. FastDelegate1(const FastDelegate1& x) {
  886. m_Closure.CopyFrom(this, x.m_Closure);
  887. }
  888. void operator = (const FastDelegate1& x) {
  889. m_Closure.CopyFrom(this, x.m_Closure);
  890. }
  891. bool operator ==(const FastDelegate1& x) const {
  892. return m_Closure.IsEqual(x.m_Closure);
  893. }
  894. bool operator !=(const FastDelegate1& x) const {
  895. return !m_Closure.IsEqual(x.m_Closure);
  896. }
  897. bool operator <(const FastDelegate1& x) const {
  898. return m_Closure.IsLess(x.m_Closure);
  899. }
  900. bool operator >(const FastDelegate1& x) const {
  901. return x.m_Closure.IsLess(m_Closure);
  902. }
  903. // Binding to non-const member functions
  904. template < class X, class Y >
  905. FastDelegate1(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1)) {
  906. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  907. }
  908. template < class X, class Y >
  909. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1)) {
  910. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  911. }
  912. // Binding to const member functions.
  913. template < class X, class Y >
  914. FastDelegate1(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1) const) {
  915. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  916. }
  917. template < class X, class Y >
  918. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1) const) {
  919. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  920. }
  921. // Static functions. We convert them into a member function call.
  922. // This constructor also provides implicit conversion
  923. FastDelegate1(DesiredRetType(*function_to_bind)(Param1 p1)) {
  924. bind(function_to_bind);
  925. }
  926. // for efficiency, prevent creation of a temporary
  927. void operator = (DesiredRetType(*function_to_bind)(Param1 p1)) {
  928. bind(function_to_bind);
  929. }
  930. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1)) {
  931. m_Closure.bindstaticfunc(this, &FastDelegate1::InvokeStaticFunction,
  932. function_to_bind);
  933. }
  934. // Invoke the delegate
  935. RetType operator() (Param1 p1) const {
  936. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1);
  937. }
  938. // Implicit conversion to "bool" using the safe_bool idiom
  939. private:
  940. typedef struct SafeBoolStruct {
  941. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  942. StaticFunctionPtr m_nonzero;
  943. } UselessTypedef;
  944. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  945. public:
  946. operator unspecified_bool_type() const {
  947. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  948. }
  949. // necessary to allow ==0 to work despite the safe_bool idiom
  950. inline bool operator==(StaticFunctionPtr funcptr) {
  951. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  952. }
  953. inline bool operator!=(StaticFunctionPtr funcptr) {
  954. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  955. }
  956. inline bool operator ! () const { // Is it bound to anything?
  957. return !m_Closure;
  958. }
  959. inline bool empty() const {
  960. return !m_Closure;
  961. }
  962. void clear() { m_Closure.clear(); }
  963. // Conversion to and from the DelegateMemento storage class
  964. const DelegateMemento& GetMemento() { return m_Closure; }
  965. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  966. private: // Invoker for static functions
  967. RetType InvokeStaticFunction(Param1 p1) const {
  968. return (*(m_Closure.GetStaticFunction()))(p1);
  969. }
  970. };
  971. //N=2
  972. template<class Param1, class Param2, class RetType = detail::DefaultVoid>
  973. class FastDelegate2 {
  974. private:
  975. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  976. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2);
  977. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2);
  978. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2);
  979. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  980. ClosureType m_Closure;
  981. public:
  982. // Typedefs to aid generic programming
  983. typedef FastDelegate2 type;
  984. // Construction and comparison functions
  985. FastDelegate2() { clear(); }
  986. FastDelegate2(const FastDelegate2& x) {
  987. m_Closure.CopyFrom(this, x.m_Closure);
  988. }
  989. void operator = (const FastDelegate2& x) {
  990. m_Closure.CopyFrom(this, x.m_Closure);
  991. }
  992. bool operator ==(const FastDelegate2& x) const {
  993. return m_Closure.IsEqual(x.m_Closure);
  994. }
  995. bool operator !=(const FastDelegate2& x) const {
  996. return !m_Closure.IsEqual(x.m_Closure);
  997. }
  998. bool operator <(const FastDelegate2& x) const {
  999. return m_Closure.IsLess(x.m_Closure);
  1000. }
  1001. bool operator >(const FastDelegate2& x) const {
  1002. return x.m_Closure.IsLess(m_Closure);
  1003. }
  1004. // Binding to non-const member functions
  1005. template < class X, class Y >
  1006. FastDelegate2(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2)) {
  1007. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1008. }
  1009. template < class X, class Y >
  1010. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2)) {
  1011. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1012. }
  1013. // Binding to const member functions.
  1014. template < class X, class Y >
  1015. FastDelegate2(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2) const) {
  1016. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1017. }
  1018. template < class X, class Y >
  1019. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2) const) {
  1020. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1021. }
  1022. // Static functions. We convert them into a member function call.
  1023. // This constructor also provides implicit conversion
  1024. FastDelegate2(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2)) {
  1025. bind(function_to_bind);
  1026. }
  1027. // for efficiency, prevent creation of a temporary
  1028. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2)) {
  1029. bind(function_to_bind);
  1030. }
  1031. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2)) {
  1032. m_Closure.bindstaticfunc(this, &FastDelegate2::InvokeStaticFunction,
  1033. function_to_bind);
  1034. }
  1035. // Invoke the delegate
  1036. RetType operator() (Param1 p1, Param2 p2) const {
  1037. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2);
  1038. }
  1039. // Implicit conversion to "bool" using the safe_bool idiom
  1040. private:
  1041. typedef struct SafeBoolStruct {
  1042. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1043. StaticFunctionPtr m_nonzero;
  1044. } UselessTypedef;
  1045. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1046. public:
  1047. operator unspecified_bool_type() const {
  1048. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1049. }
  1050. // necessary to allow ==0 to work despite the safe_bool idiom
  1051. inline bool operator==(StaticFunctionPtr funcptr) {
  1052. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1053. }
  1054. inline bool operator!=(StaticFunctionPtr funcptr) {
  1055. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1056. }
  1057. inline bool operator ! () const { // Is it bound to anything?
  1058. return !m_Closure;
  1059. }
  1060. inline bool empty() const {
  1061. return !m_Closure;
  1062. }
  1063. void clear() { m_Closure.clear(); }
  1064. // Conversion to and from the DelegateMemento storage class
  1065. const DelegateMemento& GetMemento() { return m_Closure; }
  1066. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1067. private: // Invoker for static functions
  1068. RetType InvokeStaticFunction(Param1 p1, Param2 p2) const {
  1069. return (*(m_Closure.GetStaticFunction()))(p1, p2);
  1070. }
  1071. };
  1072. //N=3
  1073. template<class Param1, class Param2, class Param3, class RetType = detail::DefaultVoid>
  1074. class FastDelegate3 {
  1075. private:
  1076. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1077. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
  1078. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3);
  1079. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2, Param3 p3);
  1080. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1081. ClosureType m_Closure;
  1082. public:
  1083. // Typedefs to aid generic programming
  1084. typedef FastDelegate3 type;
  1085. // Construction and comparison functions
  1086. FastDelegate3() { clear(); }
  1087. FastDelegate3(const FastDelegate3& x) {
  1088. m_Closure.CopyFrom(this, x.m_Closure);
  1089. }
  1090. void operator = (const FastDelegate3& x) {
  1091. m_Closure.CopyFrom(this, x.m_Closure);
  1092. }
  1093. bool operator ==(const FastDelegate3& x) const {
  1094. return m_Closure.IsEqual(x.m_Closure);
  1095. }
  1096. bool operator !=(const FastDelegate3& x) const {
  1097. return !m_Closure.IsEqual(x.m_Closure);
  1098. }
  1099. bool operator <(const FastDelegate3& x) const {
  1100. return m_Closure.IsLess(x.m_Closure);
  1101. }
  1102. bool operator >(const FastDelegate3& x) const {
  1103. return x.m_Closure.IsLess(m_Closure);
  1104. }
  1105. // Binding to non-const member functions
  1106. template < class X, class Y >
  1107. FastDelegate3(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1108. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1109. }
  1110. template < class X, class Y >
  1111. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1112. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1113. }
  1114. // Binding to const member functions.
  1115. template < class X, class Y >
  1116. FastDelegate3(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
  1117. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1118. }
  1119. template < class X, class Y >
  1120. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const) {
  1121. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1122. }
  1123. // Static functions. We convert them into a member function call.
  1124. // This constructor also provides implicit conversion
  1125. FastDelegate3(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1126. bind(function_to_bind);
  1127. }
  1128. // for efficiency, prevent creation of a temporary
  1129. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1130. bind(function_to_bind);
  1131. }
  1132. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3)) {
  1133. m_Closure.bindstaticfunc(this, &FastDelegate3::InvokeStaticFunction,
  1134. function_to_bind);
  1135. }
  1136. // Invoke the delegate
  1137. RetType operator() (Param1 p1, Param2 p2, Param3 p3) const {
  1138. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3);
  1139. }
  1140. // Implicit conversion to "bool" using the safe_bool idiom
  1141. private:
  1142. typedef struct SafeBoolStruct {
  1143. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1144. StaticFunctionPtr m_nonzero;
  1145. } UselessTypedef;
  1146. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1147. public:
  1148. operator unspecified_bool_type() const {
  1149. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1150. }
  1151. // necessary to allow ==0 to work despite the safe_bool idiom
  1152. inline bool operator==(StaticFunctionPtr funcptr) {
  1153. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1154. }
  1155. inline bool operator!=(StaticFunctionPtr funcptr) {
  1156. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1157. }
  1158. inline bool operator ! () const { // Is it bound to anything?
  1159. return !m_Closure;
  1160. }
  1161. inline bool empty() const {
  1162. return !m_Closure;
  1163. }
  1164. void clear() { m_Closure.clear(); }
  1165. // Conversion to and from the DelegateMemento storage class
  1166. const DelegateMemento& GetMemento() { return m_Closure; }
  1167. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1168. private: // Invoker for static functions
  1169. RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3) const {
  1170. return (*(m_Closure.GetStaticFunction()))(p1, p2, p3);
  1171. }
  1172. };
  1173. //N=4
  1174. template<class Param1, class Param2, class Param3, class Param4, class RetType = detail::DefaultVoid>
  1175. class FastDelegate4 {
  1176. private:
  1177. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1178. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
  1179. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
  1180. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4);
  1181. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1182. ClosureType m_Closure;
  1183. public:
  1184. // Typedefs to aid generic programming
  1185. typedef FastDelegate4 type;
  1186. // Construction and comparison functions
  1187. FastDelegate4() { clear(); }
  1188. FastDelegate4(const FastDelegate4& x) {
  1189. m_Closure.CopyFrom(this, x.m_Closure);
  1190. }
  1191. void operator = (const FastDelegate4& x) {
  1192. m_Closure.CopyFrom(this, x.m_Closure);
  1193. }
  1194. bool operator ==(const FastDelegate4& x) const {
  1195. return m_Closure.IsEqual(x.m_Closure);
  1196. }
  1197. bool operator !=(const FastDelegate4& x) const {
  1198. return !m_Closure.IsEqual(x.m_Closure);
  1199. }
  1200. bool operator <(const FastDelegate4& x) const {
  1201. return m_Closure.IsLess(x.m_Closure);
  1202. }
  1203. bool operator >(const FastDelegate4& x) const {
  1204. return x.m_Closure.IsLess(m_Closure);
  1205. }
  1206. // Binding to non-const member functions
  1207. template < class X, class Y >
  1208. FastDelegate4(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1209. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1210. }
  1211. template < class X, class Y >
  1212. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1213. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1214. }
  1215. // Binding to const member functions.
  1216. template < class X, class Y >
  1217. FastDelegate4(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
  1218. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1219. }
  1220. template < class X, class Y >
  1221. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
  1222. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1223. }
  1224. // Static functions. We convert them into a member function call.
  1225. // This constructor also provides implicit conversion
  1226. FastDelegate4(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1227. bind(function_to_bind);
  1228. }
  1229. // for efficiency, prevent creation of a temporary
  1230. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1231. bind(function_to_bind);
  1232. }
  1233. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  1234. m_Closure.bindstaticfunc(this, &FastDelegate4::InvokeStaticFunction,
  1235. function_to_bind);
  1236. }
  1237. // Invoke the delegate
  1238. RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
  1239. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4);
  1240. }
  1241. // Implicit conversion to "bool" using the safe_bool idiom
  1242. private:
  1243. typedef struct SafeBoolStruct {
  1244. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1245. StaticFunctionPtr m_nonzero;
  1246. } UselessTypedef;
  1247. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1248. public:
  1249. operator unspecified_bool_type() const {
  1250. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1251. }
  1252. // necessary to allow ==0 to work despite the safe_bool idiom
  1253. inline bool operator==(StaticFunctionPtr funcptr) {
  1254. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1255. }
  1256. inline bool operator!=(StaticFunctionPtr funcptr) {
  1257. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1258. }
  1259. inline bool operator ! () const { // Is it bound to anything?
  1260. return !m_Closure;
  1261. }
  1262. inline bool empty() const {
  1263. return !m_Closure;
  1264. }
  1265. void clear() { m_Closure.clear(); }
  1266. // Conversion to and from the DelegateMemento storage class
  1267. const DelegateMemento& GetMemento() { return m_Closure; }
  1268. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1269. private: // Invoker for static functions
  1270. RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const {
  1271. return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4);
  1272. }
  1273. };
  1274. //N=5
  1275. template<class Param1, class Param2, class Param3, class Param4, class Param5, class RetType = detail::DefaultVoid>
  1276. class FastDelegate5 {
  1277. private:
  1278. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1279. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
  1280. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
  1281. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5);
  1282. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1283. ClosureType m_Closure;
  1284. public:
  1285. // Typedefs to aid generic programming
  1286. typedef FastDelegate5 type;
  1287. // Construction and comparison functions
  1288. FastDelegate5() { clear(); }
  1289. FastDelegate5(const FastDelegate5& x) {
  1290. m_Closure.CopyFrom(this, x.m_Closure);
  1291. }
  1292. void operator = (const FastDelegate5& x) {
  1293. m_Closure.CopyFrom(this, x.m_Closure);
  1294. }
  1295. bool operator ==(const FastDelegate5& x) const {
  1296. return m_Closure.IsEqual(x.m_Closure);
  1297. }
  1298. bool operator !=(const FastDelegate5& x) const {
  1299. return !m_Closure.IsEqual(x.m_Closure);
  1300. }
  1301. bool operator <(const FastDelegate5& x) const {
  1302. return m_Closure.IsLess(x.m_Closure);
  1303. }
  1304. bool operator >(const FastDelegate5& x) const {
  1305. return x.m_Closure.IsLess(m_Closure);
  1306. }
  1307. // Binding to non-const member functions
  1308. template < class X, class Y >
  1309. FastDelegate5(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1310. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1311. }
  1312. template < class X, class Y >
  1313. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1314. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1315. }
  1316. // Binding to const member functions.
  1317. template < class X, class Y >
  1318. FastDelegate5(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
  1319. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1320. }
  1321. template < class X, class Y >
  1322. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
  1323. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1324. }
  1325. // Static functions. We convert them into a member function call.
  1326. // This constructor also provides implicit conversion
  1327. FastDelegate5(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1328. bind(function_to_bind);
  1329. }
  1330. // for efficiency, prevent creation of a temporary
  1331. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1332. bind(function_to_bind);
  1333. }
  1334. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  1335. m_Closure.bindstaticfunc(this, &FastDelegate5::InvokeStaticFunction,
  1336. function_to_bind);
  1337. }
  1338. // Invoke the delegate
  1339. RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
  1340. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5);
  1341. }
  1342. // Implicit conversion to "bool" using the safe_bool idiom
  1343. private:
  1344. typedef struct SafeBoolStruct {
  1345. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1346. StaticFunctionPtr m_nonzero;
  1347. } UselessTypedef;
  1348. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1349. public:
  1350. operator unspecified_bool_type() const {
  1351. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1352. }
  1353. // necessary to allow ==0 to work despite the safe_bool idiom
  1354. inline bool operator==(StaticFunctionPtr funcptr) {
  1355. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1356. }
  1357. inline bool operator!=(StaticFunctionPtr funcptr) {
  1358. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1359. }
  1360. inline bool operator ! () const { // Is it bound to anything?
  1361. return !m_Closure;
  1362. }
  1363. inline bool empty() const {
  1364. return !m_Closure;
  1365. }
  1366. void clear() { m_Closure.clear(); }
  1367. // Conversion to and from the DelegateMemento storage class
  1368. const DelegateMemento& GetMemento() { return m_Closure; }
  1369. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1370. private: // Invoker for static functions
  1371. RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const {
  1372. return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5);
  1373. }
  1374. };
  1375. //N=6
  1376. template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType = detail::DefaultVoid>
  1377. class FastDelegate6 {
  1378. private:
  1379. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1380. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
  1381. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
  1382. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6);
  1383. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1384. ClosureType m_Closure;
  1385. public:
  1386. // Typedefs to aid generic programming
  1387. typedef FastDelegate6 type;
  1388. // Construction and comparison functions
  1389. FastDelegate6() { clear(); }
  1390. FastDelegate6(const FastDelegate6& x) {
  1391. m_Closure.CopyFrom(this, x.m_Closure);
  1392. }
  1393. void operator = (const FastDelegate6& x) {
  1394. m_Closure.CopyFrom(this, x.m_Closure);
  1395. }
  1396. bool operator ==(const FastDelegate6& x) const {
  1397. return m_Closure.IsEqual(x.m_Closure);
  1398. }
  1399. bool operator !=(const FastDelegate6& x) const {
  1400. return !m_Closure.IsEqual(x.m_Closure);
  1401. }
  1402. bool operator <(const FastDelegate6& x) const {
  1403. return m_Closure.IsLess(x.m_Closure);
  1404. }
  1405. bool operator >(const FastDelegate6& x) const {
  1406. return x.m_Closure.IsLess(m_Closure);
  1407. }
  1408. // Binding to non-const member functions
  1409. template < class X, class Y >
  1410. FastDelegate6(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1411. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1412. }
  1413. template < class X, class Y >
  1414. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1415. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1416. }
  1417. // Binding to const member functions.
  1418. template < class X, class Y >
  1419. FastDelegate6(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
  1420. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1421. }
  1422. template < class X, class Y >
  1423. inline void bind(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const) {
  1424. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1425. }
  1426. // Static functions. We convert them into a member function call.
  1427. // This constructor also provides implicit conversion
  1428. FastDelegate6(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1429. bind(function_to_bind);
  1430. }
  1431. // for efficiency, prevent creation of a temporary
  1432. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1433. bind(function_to_bind);
  1434. }
  1435. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6)) {
  1436. m_Closure.bindstaticfunc(this, &FastDelegate6::InvokeStaticFunction,
  1437. function_to_bind);
  1438. }
  1439. // Invoke the delegate
  1440. RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
  1441. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6);
  1442. }
  1443. // Implicit conversion to "bool" using the safe_bool idiom
  1444. private:
  1445. typedef struct SafeBoolStruct {
  1446. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1447. StaticFunctionPtr m_nonzero;
  1448. } UselessTypedef;
  1449. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1450. public:
  1451. operator unspecified_bool_type() const {
  1452. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1453. }
  1454. // necessary to allow ==0 to work despite the safe_bool idiom
  1455. inline bool operator==(StaticFunctionPtr funcptr) {
  1456. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1457. }
  1458. inline bool operator!=(StaticFunctionPtr funcptr) {
  1459. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1460. }
  1461. inline bool operator ! () const { // Is it bound to anything?
  1462. return !m_Closure;
  1463. }
  1464. inline bool empty() const {
  1465. return !m_Closure;
  1466. }
  1467. void clear() { m_Closure.clear(); }
  1468. // Conversion to and from the DelegateMemento storage class
  1469. const DelegateMemento& GetMemento() { return m_Closure; }
  1470. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1471. private: // Invoker for static functions
  1472. RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const {
  1473. return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6);
  1474. }
  1475. };
  1476. //N=7
  1477. template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType = detail::DefaultVoid>
  1478. class FastDelegate7 {
  1479. private:
  1480. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1481. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
  1482. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
  1483. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7);
  1484. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1485. ClosureType m_Closure;
  1486. public:
  1487. // Typedefs to aid generic programming
  1488. typedef FastDelegate7 type;
  1489. // Construction and comparison functions
  1490. FastDelegate7() { clear(); }
  1491. FastDelegate7(const FastDelegate7& x) {
  1492. m_Closure.CopyFrom(this, x.m_Closure);
  1493. }
  1494. void operator = (const FastDelegate7& x) {
  1495. m_Closure.CopyFrom(this, x.m_Closure);
  1496. }
  1497. bool operator ==(const FastDelegate7& x) const {
  1498. return m_Closure.IsEqual(x.m_Closure);
  1499. }
  1500. bool operator !=(const FastDelegate7& x) const {
  1501. return !m_Closure.IsEqual(x.m_Closure);
  1502. }
  1503. bool operator <(const FastDelegate7& x) const {
  1504. return m_Closure.IsLess(x.m_Closure);
  1505. }
  1506. bool operator >(const FastDelegate7& x) const {
  1507. return x.m_Closure.IsLess(m_Closure);
  1508. }
  1509. // Binding to non-const member functions
  1510. template < class X, class Y >
  1511. FastDelegate7(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1512. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1513. }
  1514. template < class X, class Y >
  1515. inline void bind(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1516. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1517. }
  1518. // Binding to const member functions.
  1519. template < class X, class Y >
  1520. FastDelegate7(const Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const) {
  1521. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1522. }
  1523. template < class X, class Y >
  1524. 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) {
  1525. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1526. }
  1527. // Static functions. We convert them into a member function call.
  1528. // This constructor also provides implicit conversion
  1529. FastDelegate7(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1530. bind(function_to_bind);
  1531. }
  1532. // for efficiency, prevent creation of a temporary
  1533. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1534. bind(function_to_bind);
  1535. }
  1536. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7)) {
  1537. m_Closure.bindstaticfunc(this, &FastDelegate7::InvokeStaticFunction,
  1538. function_to_bind);
  1539. }
  1540. // Invoke the delegate
  1541. RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
  1542. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7);
  1543. }
  1544. // Implicit conversion to "bool" using the safe_bool idiom
  1545. private:
  1546. typedef struct SafeBoolStruct {
  1547. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1548. StaticFunctionPtr m_nonzero;
  1549. } UselessTypedef;
  1550. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1551. public:
  1552. operator unspecified_bool_type() const {
  1553. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1554. }
  1555. // necessary to allow ==0 to work despite the safe_bool idiom
  1556. inline bool operator==(StaticFunctionPtr funcptr) {
  1557. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1558. }
  1559. inline bool operator!=(StaticFunctionPtr funcptr) {
  1560. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1561. }
  1562. inline bool operator ! () const { // Is it bound to anything?
  1563. return !m_Closure;
  1564. }
  1565. inline bool empty() const {
  1566. return !m_Closure;
  1567. }
  1568. void clear() { m_Closure.clear(); }
  1569. // Conversion to and from the DelegateMemento storage class
  1570. const DelegateMemento& GetMemento() { return m_Closure; }
  1571. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1572. private: // Invoker for static functions
  1573. RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const {
  1574. return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7);
  1575. }
  1576. };
  1577. //N=8
  1578. template<class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType = detail::DefaultVoid>
  1579. class FastDelegate8 {
  1580. private:
  1581. typedef typename detail::DefaultVoidToVoid<RetType>::type DesiredRetType;
  1582. typedef DesiredRetType(*StaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
  1583. typedef RetType(*UnvoidStaticFunctionPtr)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
  1584. typedef RetType(detail::GenericClass::* GenericMemFn)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8);
  1585. typedef detail::ClosurePtr<GenericMemFn, StaticFunctionPtr, UnvoidStaticFunctionPtr> ClosureType;
  1586. ClosureType m_Closure;
  1587. public:
  1588. // Typedefs to aid generic programming
  1589. typedef FastDelegate8 type;
  1590. // Construction and comparison functions
  1591. FastDelegate8() { clear(); }
  1592. FastDelegate8(const FastDelegate8& x) {
  1593. m_Closure.CopyFrom(this, x.m_Closure);
  1594. }
  1595. void operator = (const FastDelegate8& x) {
  1596. m_Closure.CopyFrom(this, x.m_Closure);
  1597. }
  1598. bool operator ==(const FastDelegate8& x) const {
  1599. return m_Closure.IsEqual(x.m_Closure);
  1600. }
  1601. bool operator !=(const FastDelegate8& x) const {
  1602. return !m_Closure.IsEqual(x.m_Closure);
  1603. }
  1604. bool operator <(const FastDelegate8& x) const {
  1605. return m_Closure.IsLess(x.m_Closure);
  1606. }
  1607. bool operator >(const FastDelegate8& x) const {
  1608. return x.m_Closure.IsLess(m_Closure);
  1609. }
  1610. // Binding to non-const member functions
  1611. template < class X, class Y >
  1612. FastDelegate8(Y* pthis, DesiredRetType(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
  1613. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1614. }
  1615. template < class X, class Y >
  1616. 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)) {
  1617. m_Closure.bindmemfunc(detail::implicit_cast<X*>(pthis), function_to_bind);
  1618. }
  1619. // Binding to const member functions.
  1620. template < class X, class Y >
  1621. 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) {
  1622. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1623. }
  1624. template < class X, class Y >
  1625. 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) {
  1626. m_Closure.bindconstmemfunc(detail::implicit_cast<const X*>(pthis), function_to_bind);
  1627. }
  1628. // Static functions. We convert them into a member function call.
  1629. // This constructor also provides implicit conversion
  1630. FastDelegate8(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
  1631. bind(function_to_bind);
  1632. }
  1633. // for efficiency, prevent creation of a temporary
  1634. void operator = (DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
  1635. bind(function_to_bind);
  1636. }
  1637. inline void bind(DesiredRetType(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8)) {
  1638. m_Closure.bindstaticfunc(this, &FastDelegate8::InvokeStaticFunction,
  1639. function_to_bind);
  1640. }
  1641. // Invoke the delegate
  1642. RetType operator() (Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const {
  1643. return (m_Closure.GetClosureThis()->*(m_Closure.GetClosureMemPtr()))(p1, p2, p3, p4, p5, p6, p7, p8);
  1644. }
  1645. // Implicit conversion to "bool" using the safe_bool idiom
  1646. private:
  1647. typedef struct SafeBoolStruct {
  1648. int a_data_pointer_to_this_is_0_on_buggy_compilers;
  1649. StaticFunctionPtr m_nonzero;
  1650. } UselessTypedef;
  1651. typedef StaticFunctionPtr SafeBoolStruct::* unspecified_bool_type;
  1652. public:
  1653. operator unspecified_bool_type() const {
  1654. return empty() ? 0 : &SafeBoolStruct::m_nonzero;
  1655. }
  1656. // necessary to allow ==0 to work despite the safe_bool idiom
  1657. inline bool operator==(StaticFunctionPtr funcptr) {
  1658. return m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1659. }
  1660. inline bool operator!=(StaticFunctionPtr funcptr) {
  1661. return !m_Closure.IsEqualToStaticFuncPtr(funcptr);
  1662. }
  1663. inline bool operator ! () const { // Is it bound to anything?
  1664. return !m_Closure;
  1665. }
  1666. inline bool empty() const {
  1667. return !m_Closure;
  1668. }
  1669. void clear() { m_Closure.clear(); }
  1670. // Conversion to and from the DelegateMemento storage class
  1671. const DelegateMemento& GetMemento() { return m_Closure; }
  1672. void SetMemento(const DelegateMemento& any) { m_Closure.CopyFrom(this, any); }
  1673. private: // Invoker for static functions
  1674. RetType InvokeStaticFunction(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const {
  1675. return (*(m_Closure.GetStaticFunction()))(p1, p2, p3, p4, p5, p6, p7, p8);
  1676. }
  1677. };
  1678. ////////////////////////////////////////////////////////////////////////////////
  1679. // Fast Delegates, part 4:
  1680. //
  1681. // FastDelegate<> class (Original author: Jody Hagins)
  1682. // Allows boost::function style syntax like:
  1683. // FastDelegate< double (int, long) >
  1684. // instead of:
  1685. // FastDelegate2< int, long, double >
  1686. //
  1687. ////////////////////////////////////////////////////////////////////////////////
  1688. #ifdef FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  1689. // Declare FastDelegate as a class template. It will be specialized
  1690. // later for all number of arguments.
  1691. template <typename Signature>
  1692. class FastDelegate;
  1693. //N=0
  1694. // Specialization to allow use of
  1695. // FastDelegate< R ( ) >
  1696. // instead of
  1697. // FastDelegate0 < R >
  1698. template<typename R>
  1699. class FastDelegate< R() >
  1700. // Inherit from FastDelegate0 so that it can be treated just like a FastDelegate0
  1701. : public FastDelegate0 < R >
  1702. {
  1703. public:
  1704. // Make using the base type a bit easier via typedef.
  1705. typedef FastDelegate0 < R > BaseType;
  1706. // Allow users access to the specific type of this delegate.
  1707. typedef FastDelegate SelfType;
  1708. // Mimic the base class constructors.
  1709. FastDelegate() : BaseType() {}
  1710. template < class X, class Y >
  1711. FastDelegate(Y* pthis,
  1712. R(X::* function_to_bind)())
  1713. : BaseType(pthis, function_to_bind) {
  1714. }
  1715. template < class X, class Y >
  1716. FastDelegate(const Y* pthis,
  1717. R(X::* function_to_bind)() const)
  1718. : BaseType(pthis, function_to_bind)
  1719. {
  1720. }
  1721. FastDelegate(R(*function_to_bind)())
  1722. : BaseType(function_to_bind) {
  1723. }
  1724. void operator = (const BaseType& x) {
  1725. *static_cast<BaseType*>(this) = x;
  1726. }
  1727. };
  1728. //N=1
  1729. // Specialization to allow use of
  1730. // FastDelegate< R ( Param1 ) >
  1731. // instead of
  1732. // FastDelegate1 < Param1, R >
  1733. template<typename R, class Param1>
  1734. class FastDelegate< R(Param1) >
  1735. // Inherit from FastDelegate1 so that it can be treated just like a FastDelegate1
  1736. : public FastDelegate1 < Param1, R >
  1737. {
  1738. public:
  1739. // Make using the base type a bit easier via typedef.
  1740. typedef FastDelegate1 < Param1, R > BaseType;
  1741. // Allow users access to the specific type of this delegate.
  1742. typedef FastDelegate SelfType;
  1743. // Mimic the base class constructors.
  1744. FastDelegate() : BaseType() {}
  1745. template < class X, class Y >
  1746. FastDelegate(Y* pthis,
  1747. R(X::* function_to_bind)(Param1 p1))
  1748. : BaseType(pthis, function_to_bind) {
  1749. }
  1750. template < class X, class Y >
  1751. FastDelegate(const Y* pthis,
  1752. R(X::* function_to_bind)(Param1 p1) const)
  1753. : BaseType(pthis, function_to_bind)
  1754. {
  1755. }
  1756. FastDelegate(R(*function_to_bind)(Param1 p1))
  1757. : BaseType(function_to_bind) {
  1758. }
  1759. void operator = (const BaseType& x) {
  1760. *static_cast<BaseType*>(this) = x;
  1761. }
  1762. };
  1763. //N=2
  1764. // Specialization to allow use of
  1765. // FastDelegate< R ( Param1, Param2 ) >
  1766. // instead of
  1767. // FastDelegate2 < Param1, Param2, R >
  1768. template<typename R, class Param1, class Param2>
  1769. class FastDelegate< R(Param1, Param2) >
  1770. // Inherit from FastDelegate2 so that it can be treated just like a FastDelegate2
  1771. : public FastDelegate2 < Param1, Param2, R >
  1772. {
  1773. public:
  1774. // Make using the base type a bit easier via typedef.
  1775. typedef FastDelegate2 < Param1, Param2, R > BaseType;
  1776. // Allow users access to the specific type of this delegate.
  1777. typedef FastDelegate SelfType;
  1778. // Mimic the base class constructors.
  1779. FastDelegate() : BaseType() {}
  1780. template < class X, class Y >
  1781. FastDelegate(Y* pthis,
  1782. R(X::* function_to_bind)(Param1 p1, Param2 p2))
  1783. : BaseType(pthis, function_to_bind) {
  1784. }
  1785. template < class X, class Y >
  1786. FastDelegate(const Y* pthis,
  1787. R(X::* function_to_bind)(Param1 p1, Param2 p2) const)
  1788. : BaseType(pthis, function_to_bind)
  1789. {
  1790. }
  1791. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2))
  1792. : BaseType(function_to_bind) {
  1793. }
  1794. void operator = (const BaseType& x) {
  1795. *static_cast<BaseType*>(this) = x;
  1796. }
  1797. };
  1798. //N=3
  1799. // Specialization to allow use of
  1800. // FastDelegate< R ( Param1, Param2, Param3 ) >
  1801. // instead of
  1802. // FastDelegate3 < Param1, Param2, Param3, R >
  1803. template<typename R, class Param1, class Param2, class Param3>
  1804. class FastDelegate< R(Param1, Param2, Param3) >
  1805. // Inherit from FastDelegate3 so that it can be treated just like a FastDelegate3
  1806. : public FastDelegate3 < Param1, Param2, Param3, R >
  1807. {
  1808. public:
  1809. // Make using the base type a bit easier via typedef.
  1810. typedef FastDelegate3 < Param1, Param2, Param3, R > BaseType;
  1811. // Allow users access to the specific type of this delegate.
  1812. typedef FastDelegate SelfType;
  1813. // Mimic the base class constructors.
  1814. FastDelegate() : BaseType() {}
  1815. template < class X, class Y >
  1816. FastDelegate(Y* pthis,
  1817. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3))
  1818. : BaseType(pthis, function_to_bind) {
  1819. }
  1820. template < class X, class Y >
  1821. FastDelegate(const Y* pthis,
  1822. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3) const)
  1823. : BaseType(pthis, function_to_bind)
  1824. {
  1825. }
  1826. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3))
  1827. : BaseType(function_to_bind) {
  1828. }
  1829. void operator = (const BaseType& x) {
  1830. *static_cast<BaseType*>(this) = x;
  1831. }
  1832. };
  1833. //N=4
  1834. // Specialization to allow use of
  1835. // FastDelegate< R ( Param1, Param2, Param3, Param4 ) >
  1836. // instead of
  1837. // FastDelegate4 < Param1, Param2, Param3, Param4, R >
  1838. template<typename R, class Param1, class Param2, class Param3, class Param4>
  1839. class FastDelegate< R(Param1, Param2, Param3, Param4) >
  1840. // Inherit from FastDelegate4 so that it can be treated just like a FastDelegate4
  1841. : public FastDelegate4 < Param1, Param2, Param3, Param4, R >
  1842. {
  1843. public:
  1844. // Make using the base type a bit easier via typedef.
  1845. typedef FastDelegate4 < Param1, Param2, Param3, Param4, R > BaseType;
  1846. // Allow users access to the specific type of this delegate.
  1847. typedef FastDelegate SelfType;
  1848. // Mimic the base class constructors.
  1849. FastDelegate() : BaseType() {}
  1850. template < class X, class Y >
  1851. FastDelegate(Y* pthis,
  1852. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4))
  1853. : BaseType(pthis, function_to_bind) {
  1854. }
  1855. template < class X, class Y >
  1856. FastDelegate(const Y* pthis,
  1857. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const)
  1858. : BaseType(pthis, function_to_bind)
  1859. {
  1860. }
  1861. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4))
  1862. : BaseType(function_to_bind) {
  1863. }
  1864. void operator = (const BaseType& x) {
  1865. *static_cast<BaseType*>(this) = x;
  1866. }
  1867. };
  1868. //N=5
  1869. // Specialization to allow use of
  1870. // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5 ) >
  1871. // instead of
  1872. // FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
  1873. template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5>
  1874. class FastDelegate< R(Param1, Param2, Param3, Param4, Param5) >
  1875. // Inherit from FastDelegate5 so that it can be treated just like a FastDelegate5
  1876. : public FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R >
  1877. {
  1878. public:
  1879. // Make using the base type a bit easier via typedef.
  1880. typedef FastDelegate5 < Param1, Param2, Param3, Param4, Param5, R > BaseType;
  1881. // Allow users access to the specific type of this delegate.
  1882. typedef FastDelegate SelfType;
  1883. // Mimic the base class constructors.
  1884. FastDelegate() : BaseType() {}
  1885. template < class X, class Y >
  1886. FastDelegate(Y* pthis,
  1887. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5))
  1888. : BaseType(pthis, function_to_bind) {
  1889. }
  1890. template < class X, class Y >
  1891. FastDelegate(const Y* pthis,
  1892. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const)
  1893. : BaseType(pthis, function_to_bind)
  1894. {
  1895. }
  1896. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5))
  1897. : BaseType(function_to_bind) {
  1898. }
  1899. void operator = (const BaseType& x) {
  1900. *static_cast<BaseType*>(this) = x;
  1901. }
  1902. };
  1903. //N=6
  1904. // Specialization to allow use of
  1905. // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6 ) >
  1906. // instead of
  1907. // FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
  1908. template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6>
  1909. class FastDelegate< R(Param1, Param2, Param3, Param4, Param5, Param6) >
  1910. // Inherit from FastDelegate6 so that it can be treated just like a FastDelegate6
  1911. : public FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R >
  1912. {
  1913. public:
  1914. // Make using the base type a bit easier via typedef.
  1915. typedef FastDelegate6 < Param1, Param2, Param3, Param4, Param5, Param6, R > BaseType;
  1916. // Allow users access to the specific type of this delegate.
  1917. typedef FastDelegate SelfType;
  1918. // Mimic the base class constructors.
  1919. FastDelegate() : BaseType() {}
  1920. template < class X, class Y >
  1921. FastDelegate(Y* pthis,
  1922. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6))
  1923. : BaseType(pthis, function_to_bind) {
  1924. }
  1925. template < class X, class Y >
  1926. FastDelegate(const Y* pthis,
  1927. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6) const)
  1928. : BaseType(pthis, function_to_bind)
  1929. {
  1930. }
  1931. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6))
  1932. : BaseType(function_to_bind) {
  1933. }
  1934. void operator = (const BaseType& x) {
  1935. *static_cast<BaseType*>(this) = x;
  1936. }
  1937. };
  1938. //N=7
  1939. // Specialization to allow use of
  1940. // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7 ) >
  1941. // instead of
  1942. // FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
  1943. template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7>
  1944. class FastDelegate< R(Param1, Param2, Param3, Param4, Param5, Param6, Param7) >
  1945. // Inherit from FastDelegate7 so that it can be treated just like a FastDelegate7
  1946. : public FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R >
  1947. {
  1948. public:
  1949. // Make using the base type a bit easier via typedef.
  1950. typedef FastDelegate7 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, R > BaseType;
  1951. // Allow users access to the specific type of this delegate.
  1952. typedef FastDelegate SelfType;
  1953. // Mimic the base class constructors.
  1954. FastDelegate() : BaseType() {}
  1955. template < class X, class Y >
  1956. FastDelegate(Y* pthis,
  1957. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7))
  1958. : BaseType(pthis, function_to_bind) {
  1959. }
  1960. template < class X, class Y >
  1961. FastDelegate(const Y* pthis,
  1962. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7) const)
  1963. : BaseType(pthis, function_to_bind)
  1964. {
  1965. }
  1966. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7))
  1967. : BaseType(function_to_bind) {
  1968. }
  1969. void operator = (const BaseType& x) {
  1970. *static_cast<BaseType*>(this) = x;
  1971. }
  1972. };
  1973. //N=8
  1974. // Specialization to allow use of
  1975. // FastDelegate< R ( Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8 ) >
  1976. // instead of
  1977. // FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R >
  1978. template<typename R, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8>
  1979. class FastDelegate< R(Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8) >
  1980. // Inherit from FastDelegate8 so that it can be treated just like a FastDelegate8
  1981. : public FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R >
  1982. {
  1983. public:
  1984. // Make using the base type a bit easier via typedef.
  1985. typedef FastDelegate8 < Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, R > BaseType;
  1986. // Allow users access to the specific type of this delegate.
  1987. typedef FastDelegate SelfType;
  1988. // Mimic the base class constructors.
  1989. FastDelegate() : BaseType() {}
  1990. template < class X, class Y >
  1991. FastDelegate(Y* pthis,
  1992. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8))
  1993. : BaseType(pthis, function_to_bind) {
  1994. }
  1995. template < class X, class Y >
  1996. FastDelegate(const Y* pthis,
  1997. R(X::* function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8) const)
  1998. : BaseType(pthis, function_to_bind)
  1999. {
  2000. }
  2001. FastDelegate(R(*function_to_bind)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5, Param6 p6, Param7 p7, Param8 p8))
  2002. : BaseType(function_to_bind) {
  2003. }
  2004. void operator = (const BaseType& x) {
  2005. *static_cast<BaseType*>(this) = x;
  2006. }
  2007. };
  2008. #endif //FASTDELEGATE_ALLOW_FUNCTION_TYPE_SYNTAX
  2009. ////////////////////////////////////////////////////////////////////////////////
  2010. // Fast Delegates, part 5:
  2011. //
  2012. // MakeDelegate() helper function
  2013. //
  2014. // MakeDelegate(&x, &X::func) returns a fastdelegate of the type
  2015. // necessary for calling x.func() with the correct number of arguments.
  2016. // This makes it possible to eliminate many typedefs from user code.
  2017. //
  2018. ////////////////////////////////////////////////////////////////////////////////
  2019. // Also declare overloads of a MakeDelegate() global function to
  2020. // reduce the need for typedefs.
  2021. // We need seperate overloads for const and non-const member functions.
  2022. // Also, because of the weird rule about the class of derived member function pointers,
  2023. // implicit downcasts may need to be applied later to the 'this' pointer.
  2024. // That's why two classes (X and Y) appear in the definitions. Y must be implicitly
  2025. // castable to X.
  2026. // Workaround for VC6. VC6 needs void return types converted into DefaultVoid.
  2027. // GCC 3.2 and later won't compile this unless it's preceded by 'typename',
  2028. // but VC6 doesn't allow 'typename' in this context.
  2029. // So, I have to use a macro.
  2030. #ifdef FASTDLGT_VC6
  2031. #define FASTDLGT_RETTYPE detail::VoidToDefaultVoid<RetType>::type
  2032. #else
  2033. #define FASTDLGT_RETTYPE RetType
  2034. #endif
  2035. //N=0
  2036. template <class X, class Y, class RetType>
  2037. FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)()) {
  2038. return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
  2039. }
  2040. template <class X, class Y, class RetType>
  2041. FastDelegate0<FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)() const) {
  2042. return FastDelegate0<FASTDLGT_RETTYPE>(x, func);
  2043. }
  2044. //N=1
  2045. template <class X, class Y, class Param1, class RetType>
  2046. FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1)) {
  2047. return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
  2048. }
  2049. template <class X, class Y, class Param1, class RetType>
  2050. FastDelegate1<Param1, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1) const) {
  2051. return FastDelegate1<Param1, FASTDLGT_RETTYPE>(x, func);
  2052. }
  2053. //N=2
  2054. template <class X, class Y, class Param1, class Param2, class RetType>
  2055. FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2)) {
  2056. return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
  2057. }
  2058. template <class X, class Y, class Param1, class Param2, class RetType>
  2059. FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2) const) {
  2060. return FastDelegate2<Param1, Param2, FASTDLGT_RETTYPE>(x, func);
  2061. }
  2062. //N=3
  2063. template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
  2064. FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2, Param3 p3)) {
  2065. return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
  2066. }
  2067. template <class X, class Y, class Param1, class Param2, class Param3, class RetType>
  2068. FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2, Param3 p3) const) {
  2069. return FastDelegate3<Param1, Param2, Param3, FASTDLGT_RETTYPE>(x, func);
  2070. }
  2071. //N=4
  2072. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
  2073. FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4)) {
  2074. return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
  2075. }
  2076. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class RetType>
  2077. FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4) const) {
  2078. return FastDelegate4<Param1, Param2, Param3, Param4, FASTDLGT_RETTYPE>(x, func);
  2079. }
  2080. //N=5
  2081. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
  2082. FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5)) {
  2083. return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
  2084. }
  2085. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class RetType>
  2086. FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE> MakeDelegate(Y* x, RetType(X::* func)(Param1 p1, Param2 p2, Param3 p3, Param4 p4, Param5 p5) const) {
  2087. return FastDelegate5<Param1, Param2, Param3, Param4, Param5, FASTDLGT_RETTYPE>(x, func);
  2088. }
  2089. //N=6
  2090. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
  2091. FastDelegate6<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)) {
  2092. return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
  2093. }
  2094. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class RetType>
  2095. FastDelegate6<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) {
  2096. return FastDelegate6<Param1, Param2, Param3, Param4, Param5, Param6, FASTDLGT_RETTYPE>(x, func);
  2097. }
  2098. //N=7
  2099. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
  2100. FastDelegate7<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)) {
  2101. return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
  2102. }
  2103. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class RetType>
  2104. FastDelegate7<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) {
  2105. return FastDelegate7<Param1, Param2, Param3, Param4, Param5, Param6, Param7, FASTDLGT_RETTYPE>(x, func);
  2106. }
  2107. //N=8
  2108. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType>
  2109. FastDelegate8<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)) {
  2110. return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func);
  2111. }
  2112. template <class X, class Y, class Param1, class Param2, class Param3, class Param4, class Param5, class Param6, class Param7, class Param8, class RetType>
  2113. FastDelegate8<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) {
  2114. return FastDelegate8<Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, FASTDLGT_RETTYPE>(x, func);
  2115. }
  2116. // clean up after ourselves...
  2117. #undef FASTDLGT_RETTYPE
  2118. } // namespace fastdelegate
  2119. #endif // !defined(FASTDELEGATE_H)