message_bus_impl.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "message_bus_impl.h"
  2. namespace robotics::v3 {
  3. message_bus_impl::message_bus_impl() {
  4. }
  5. message_bus_impl::~message_bus_impl() {
  6. map_.clear();
  7. }
  8. message_bus_impl* message_bus_impl::instance() {
  9. static message_bus_impl g_message_bus_impl;
  10. return &g_message_bus_impl;
  11. }
  12. bool message_bus_impl::bind(std::string const& str_topic, bool observer, std::any const& fn) {
  13. auto range = map_.equal_range(str_topic);
  14. for (Iterater it = range.first; it != range.second; ++it) {
  15. if (it->second.first == false && !observer) {
  16. return false;
  17. }
  18. }
  19. map_.emplace(str_topic, std::pair(observer, fn));
  20. return true;
  21. }
  22. std::vector<std::pair<bool, std::any>> message_bus_impl::find(const std::string& str_topic) {
  23. std::vector<std::pair<bool, std::any>> result;
  24. auto range = map_.equal_range(str_topic);
  25. for (Iterater it = range.first; it != range.second; ++it) {
  26. result.push_back(it->second);
  27. }
  28. return result;
  29. }
  30. void message_bus_impl::remove(const std::string& str_topic) {
  31. auto range = map_.equal_range(str_topic);
  32. map_.erase(range.first, range.second);
  33. }
  34. }