Callback.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __CALLBACK_H__
  21. #define __CALLBACK_H__
  22. /*
  23. ================================================================================================
  24. This file defines a set of template functors for generating callbacks, specifically
  25. the OnChange handlers in the CVar system.
  26. ================================================================================================
  27. */
  28. /*
  29. ================================================
  30. idCallback
  31. ================================================
  32. */
  33. class idCallback {
  34. public:
  35. virtual ~idCallback() {}
  36. virtual void Call() = 0;
  37. virtual idCallback * Clone() const = 0;
  38. };
  39. /*
  40. ================================================
  41. idCallbackStatic
  42. Callback class that forwards the call to a c-style function
  43. ================================================
  44. */
  45. class idCallbackStatic : public idCallback {
  46. public:
  47. idCallbackStatic( void (*f)() ) {
  48. this->f = f;
  49. }
  50. void Call() {
  51. f();
  52. }
  53. idCallback * Clone() const {
  54. //idScopedGlobalHeap everythingHereGoesInTheGlobalHeap;
  55. return new (TAG_FUNC_CALLBACK) idCallbackStatic( f );
  56. }
  57. private:
  58. void (*f)();
  59. };
  60. /*
  61. ================================================
  62. idCallbackBindMem
  63. Callback class that forwards the call to a member function
  64. ================================================
  65. */
  66. template< class T >
  67. class idCallbackBindMem : public idCallback {
  68. public:
  69. idCallbackBindMem( T * t, void (T::*f)() ) {
  70. this->t = t;
  71. this->f = f;
  72. }
  73. void Call() {
  74. (t->*f)();
  75. }
  76. idCallback * Clone() const {
  77. return new (TAG_FUNC_CALLBACK) idCallbackBindMem( t, f );
  78. }
  79. private:
  80. T * t;
  81. void (T::*f)();
  82. };
  83. /*
  84. ================================================
  85. idCallbackBindMemArg1
  86. Callback class that forwards the call to a member function with an additional constant parameter
  87. ================================================
  88. */
  89. template< class T, typename A1 >
  90. class idCallbackBindMemArg1 : public idCallback {
  91. public:
  92. idCallbackBindMemArg1( T * t_, void (T::*f_)( A1 ), A1 a1_ ) :
  93. t( t_ ),
  94. f( f_ ),
  95. a1( a1_ ) {
  96. }
  97. void Call() {
  98. (t->*f)( a1 );
  99. }
  100. idCallback * Clone() const {
  101. return new (TAG_FUNC_CALLBACK) idCallbackBindMemArg1( t, f, a1 );
  102. }
  103. private:
  104. T * t;
  105. void (T::*f)( A1 );
  106. A1 a1;
  107. // hack to get to compile on the 360 with reference arguments
  108. // with this on the PC, the MakeCallback function fails compilation because it's returning a copy
  109. // therefore, the Arg1 callbacks can't have arguments that are references
  110. //DISALLOW_COPY_AND_ASSIGN( idCallbackBindMemArg1 );
  111. };
  112. /*
  113. ================================================================================================
  114. These are needed because we can't derive the type of an object from the type passed to the
  115. constructor. If it weren't for these, we'd have to manually specify the type:
  116. idCallbackBindMem<idFoo>( this, &idFoo::MyFunction );
  117. becomes:
  118. MakeCallback( this, &idFoo::MyFunction );
  119. ================================================================================================
  120. */
  121. /*
  122. ========================
  123. MakeCallback
  124. ========================
  125. */
  126. ID_INLINE_EXTERN idCallbackStatic MakeCallback( void (*f)(void) ) {
  127. return idCallbackStatic( f );
  128. }
  129. /*
  130. ========================
  131. MakeCallback
  132. ========================
  133. */
  134. template < class T >
  135. ID_INLINE_EXTERN idCallbackBindMem<T> MakeCallback( T * t, void (T::*f)(void) ) {
  136. return idCallbackBindMem<T>( t, f );
  137. }
  138. /*
  139. ========================
  140. MakeCallback
  141. ========================
  142. */
  143. template < class T, typename A1 >
  144. ID_INLINE_EXTERN idCallbackBindMemArg1<T, A1> MakeCallback( T * t, void (T::*f)( A1 ), A1 a1 ) {
  145. return idCallbackBindMemArg1<T, A1>( t, f, a1 );
  146. }
  147. #endif // __CALLBACK_H__