Signaler.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/wwlib/Signaler.h $
  22. *
  23. * DESCRIPTION
  24. * Lightweight two-way notification system. This class allows loose coupling
  25. * communication between two classes. The only details that need to be know
  26. * by both classes is the Signaler class it self and the type of signal they
  27. * communicate to each other.
  28. *
  29. * PROGRAMMER
  30. * Denzil E. Long, Jr.
  31. * $Author: Denzil_l $
  32. *
  33. * VERSION INFO
  34. * $Modtime: 11/16/01 11:19a $
  35. * $Revision: 4 $
  36. *
  37. ******************************************************************************/
  38. #ifndef __SIGNALER_H__
  39. #define __SIGNALER_H__
  40. template<typename T> class Signaler
  41. {
  42. public:
  43. void SignalMe(Signaler<T>& target)
  44. {if (mConnection != &target) {Disconnect(); Connect(target); target.Connect(*this);}}
  45. void StopSignaling(Signaler<T>& target)
  46. {if (mConnection == &target) {Disconnect();}}
  47. void SendSignal(T& signal)
  48. {if (mConnection) {mConnection->ReceiveSignal(signal);}}
  49. virtual void ReceiveSignal(T&)
  50. {}
  51. virtual void SignalDropped(Signaler<T>& signaler)
  52. {mConnection = NULL;}
  53. protected:
  54. Signaler() :
  55. mConnection(NULL)
  56. {}
  57. virtual ~Signaler()
  58. {Disconnect();}
  59. void Connect(Signaler<T>& source)
  60. {mConnection = &source;}
  61. void Disconnect(void)
  62. {if (mConnection) {mConnection->SignalDropped(*this);} mConnection = NULL;}
  63. // Prevent copy and assignment
  64. Signaler(const Signaler&);
  65. const Signaler& operator=(const Signaler&);
  66. private:
  67. Signaler<T>* mConnection;
  68. };
  69. #endif // __SIGNALER_H__