PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/lleventemitter.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 102 lines | 47 code | 18 blank | 37 comment | 6 complexity | 8103e4954ca20c0c670b90a81a38f531 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lleventemitter.h
  3. * @brief General event emitter class
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. // header guard
  27. #ifndef LL_EVENTEMITTER_H
  28. #define LL_EVENTEMITTER_H
  29. // standard headers
  30. #include <algorithm>
  31. #include <typeinfo>
  32. #include <iostream>
  33. #include <string>
  34. #include <list>
  35. #include "stdtypes.h"
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // templatized emitter class
  38. template < class T >
  39. class eventEmitter
  40. {
  41. public:
  42. typedef typename T::EventType EventType;
  43. typedef std::list< T* > ObserverContainer;
  44. typedef void ( T::*observerMethod )( const EventType& );
  45. protected:
  46. ObserverContainer observers;
  47. public:
  48. eventEmitter () { };
  49. ~eventEmitter () { };
  50. ///////////////////////////////////////////////////////////////////////////////
  51. //
  52. BOOL addObserver ( T* observerIn )
  53. {
  54. if ( ! observerIn )
  55. return FALSE;
  56. // check if observer already exists
  57. if ( std::find ( observers.begin (), observers.end (), observerIn ) != observers.end () )
  58. return FALSE;
  59. // save it
  60. observers.push_back ( observerIn );
  61. return true;
  62. };
  63. ///////////////////////////////////////////////////////////////////////////////
  64. //
  65. BOOL remObserver ( T* observerIn )
  66. {
  67. if ( ! observerIn )
  68. return FALSE;
  69. observers.remove ( observerIn );
  70. return TRUE;
  71. };
  72. ///////////////////////////////////////////////////////////////////////////////
  73. //
  74. void update ( observerMethod method, const EventType& msgIn )
  75. {
  76. typename std::list< T* >::iterator iter = observers.begin ();
  77. while ( iter != observers.end () )
  78. {
  79. ( ( *iter )->*method ) ( msgIn );
  80. ++iter;
  81. };
  82. };
  83. };
  84. #endif // lleventemitter_h