Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A header describing use of the delegate system. It's hiding
  4. // the highly complex implementation details of the delegate system
  5. //
  6. // $NoKeywords: $
  7. //
  8. //===========================================================================//
  9. #ifndef UTLDELEGATE_H
  10. #define UTLDELEGATE_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. //-----------------------------------------------------------------------------
  15. // The delegate system: A method of invoking methods, whether they are
  16. // member methods of classes, static methods of classes, or free functions,
  17. // dealing with all the nastiness in differences between how the calls have
  18. // to happen yet works in a highly optimal fashion. For details, see
  19. //
  20. // http://www.codeproject.com/cpp/FastDelegate.asp
  21. //
  22. // The delegate design pattern is described here
  23. //
  24. // http://en.wikipedia.org/wiki/Delegation_(programming)
  25. //-----------------------------------------------------------------------------
  26. #ifdef UTLDELEGATE_USAGE_DEMONSTRATION
  27. //-----------------------------------------------------------------------------
  28. // Here, we show how to use this system (the ifdef UTLDELEGATE_USAGE_DEMONSTRATION is used to get syntax coloring).
  29. //-----------------------------------------------------------------------------
  30. // First, define the functions you wish to call.
  31. int Test1( char *pString, float x );
  32. class CTestClass
  33. {
  34. public:
  35. void Test2();
  36. static float Test3( int x );
  37. };
  38. void Test()
  39. {
  40. CTestClass testClass;
  41. // CUtlDelegate is a class that can be used to invoke methods of classes
  42. // or static functions in a highly efficient manner.
  43. // There are a couple ways to hook up a delegate. One is in a constructor
  44. // Note that the template parameter of CUtlFastDelegate looks like the
  45. // function type: first, you have the return type, then ( parameter list )
  46. CUtlDelegate< int ( char *, float ) > delegate1( &Test1 );
  47. // Another way is to use the UtlMakeDelegate method, allowing you to
  48. // define the delegate later. Note that UtlMakeDelegate does *not* do a heap allocation
  49. CUtlDelegate< void () > delegate2;
  50. delegate2 = UtlMakeDelegate( &testClass, &CTestClass::Test2 );
  51. // A third method is to use the Bind() method of CUtlFastDelegate
  52. // Note that you do not pass in the class pointer for static functions
  53. CUtlDelegate< float ( int ) > delegate3;
  54. delegate3.Bind( &CTestClass::Test3 );
  55. // Use the () operator to invoke the function calls.
  56. int x = delegate1( "hello", 1.0f );
  57. delegate2();
  58. float y = delegate3( 5 );
  59. // Use the Clear() method to unbind a delegate.
  60. delegate1.Clear();
  61. // You can use operator! or IsEmpty() to see if a delegate is bound
  62. if ( !delegate1.IsEmpty() )
  63. {
  64. delegate1( "hello2" );
  65. }
  66. // Delegates maintain an internal non-templatized representation of the
  67. // functions they are bound to called CUtlAbstractDelegate. These are
  68. // useful when keeping a list of untyped delegates or when passing
  69. // delegates across interface boundaries.
  70. const CUtlAbstractDelegate &abstractDelegate3 = delegate3.GetAbstractDelegate();
  71. CUtlDelegate< float ( int ) > delegate4;
  72. delegate4.SetAbstractDelegate( abstractDelegate3 );
  73. delegate4( 10 );
  74. }
  75. #endif // UTLDELEGATE_USAGE_DEMONSTRATION
  76. // Looking in this file may cause blindness.
  77. #include "tier1/utldelegateimpl.h"
  78. #endif // UTLDELEGATE_H