Source code of Windows XP (NT5)
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.

78 lines
1.8 KiB

  1. #include "Multicaster.h"
  2. bool Multicaster::subscribe (MulticastObserver *observer, std::string address)
  3. {
  4. AddressSpace::iterator it = m_addresses.find (address);
  5. if (it == m_addresses.end ())
  6. {
  7. m_addresses.insert (std::pair<std::string, Subscriptions> (address, Subscriptions ()));
  8. it = m_addresses.find (address);
  9. if (it == m_addresses.end ())
  10. return false;
  11. }
  12. (*it).second.push_back (observer);
  13. return true;
  14. }
  15. bool Multicaster::unsubscribe (MulticastObserver *observer, std::string address)
  16. {
  17. AddressSpace::iterator it = m_addresses.find (address);
  18. if (it == m_addresses.end ())
  19. return false;
  20. unsubscribe ((*it).second, observer);
  21. return true;
  22. }
  23. bool Multicaster::publish (MulticastObserver *publisher, std::string address, Value value)
  24. {
  25. AddressSpace::iterator it = m_addresses.find (address);
  26. if (it == m_addresses.end ())
  27. return false;
  28. Subscriptions& subscriptions = (*it).second;
  29. for (Subscriptions::iterator subit = subscriptions.begin ();
  30. subit != subscriptions.end ();
  31. ++subit) {
  32. MulticastObserver *subscriber = *subit;
  33. if (subscriber != publisher)
  34. subscriber->accept (address, value);
  35. }
  36. return true;
  37. }
  38. void Multicaster::unsubscribeFromAll (MulticastObserver *observer)
  39. {
  40. for (AddressSpace::iterator it = m_addresses.begin ();
  41. it != m_addresses.end ();
  42. ++it)
  43. unsubscribe ((*it).second, observer);
  44. }
  45. void Multicaster::unsubscribe (Subscriptions& subscriptions, MulticastObserver *observerToRemove)
  46. {
  47. for (Subscriptions::iterator it = subscriptions.begin (); it != subscriptions.end (); ) {
  48. if (*it == observerToRemove)
  49. it = subscriptions.erase (it);
  50. else
  51. it++;
  52. }
  53. }