servnotf.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /**
  4. *******************************************************************************
  5. * Copyright (C) 2001-2014, International Business Machines Corporation and *
  6. * others. All Rights Reserved. *
  7. *******************************************************************************
  8. */
  9. #ifndef ICUNOTIF_H
  10. #define ICUNOTIF_H
  11. #include "unicode/utypes.h"
  12. #if UCONFIG_NO_SERVICE
  13. U_NAMESPACE_BEGIN
  14. /*
  15. * Allow the declaration of APIs with pointers to BreakIterator
  16. * even when break iteration is removed from the build.
  17. */
  18. class ICUNotifier;
  19. U_NAMESPACE_END
  20. #else
  21. #include "unicode/uobject.h"
  22. #include "unicode/unistr.h"
  23. #include "mutex.h"
  24. #include "uvector.h"
  25. U_NAMESPACE_BEGIN
  26. class U_COMMON_API EventListener : public UObject {
  27. public:
  28. virtual ~EventListener();
  29. public:
  30. static UClassID U_EXPORT2 getStaticClassID();
  31. virtual UClassID getDynamicClassID() const;
  32. public:
  33. #ifdef SERVICE_DEBUG
  34. virtual UnicodeString& debug(UnicodeString& result) const {
  35. return debugClass(result);
  36. }
  37. virtual UnicodeString& debugClass(UnicodeString& result) const {
  38. return result.append((UnicodeString)"Key");
  39. }
  40. #endif
  41. };
  42. /**
  43. * <p>Abstract implementation of a notification facility. Clients add
  44. * EventListeners with addListener and remove them with removeListener.
  45. * Notifiers call notifyChanged when they wish to notify listeners.
  46. * This queues the listener list on the notification thread, which
  47. * eventually dequeues the list and calls notifyListener on each
  48. * listener in the list.</p>
  49. *
  50. * <p>Subclasses override acceptsListener and notifyListener
  51. * to add type-safe notification. AcceptsListener should return
  52. * true if the listener is of the appropriate type; ICUNotifier
  53. * itself will ensure the listener is non-null and that the
  54. * identical listener is not already registered with the Notifier.
  55. * NotifyListener should cast the listener to the appropriate
  56. * type and call the appropriate method on the listener.
  57. */
  58. class U_COMMON_API ICUNotifier : public UMemory {
  59. private: UVector* listeners;
  60. public:
  61. ICUNotifier(void);
  62. virtual ~ICUNotifier(void);
  63. /**
  64. * Add a listener to be notified when notifyChanged is called.
  65. * The listener must not be null. AcceptsListener must return
  66. * true for the listener. Attempts to concurrently
  67. * register the identical listener more than once will be
  68. * silently ignored.
  69. */
  70. virtual void addListener(const EventListener* l, UErrorCode& status);
  71. /**
  72. * Stop notifying this listener. The listener must
  73. * not be null. Attemps to remove a listener that is
  74. * not registered will be silently ignored.
  75. */
  76. virtual void removeListener(const EventListener* l, UErrorCode& status);
  77. /**
  78. * ICU doesn't spawn its own threads. All listeners are notified in
  79. * the thread of the caller. Misbehaved listeners can therefore
  80. * indefinitely block the calling thread. Callers should beware of
  81. * deadlock situations.
  82. */
  83. virtual void notifyChanged(void);
  84. protected:
  85. /**
  86. * Subclasses implement this to return TRUE if the listener is
  87. * of the appropriate type.
  88. */
  89. virtual UBool acceptsListener(const EventListener& l) const = 0;
  90. /**
  91. * Subclasses implement this to notify the listener.
  92. */
  93. virtual void notifyListener(EventListener& l) const = 0;
  94. };
  95. U_NAMESPACE_END
  96. /* UCONFIG_NO_SERVICE */
  97. #endif
  98. /* ICUNOTIF_H */
  99. #endif