message_bus_impl.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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, std::any const& fn) {
  13. auto range = map_.equal_range(str_topic);
  14. if (range.first != range.second)
  15. return false;
  16. map_.emplace(str_topic, std::pair(false, fn));
  17. return true;
  18. }
  19. void message_bus_impl::bind_multiple(std::string const& str_topic, std::any const& fn) {
  20. map_.emplace(str_topic, std::make_pair(true, fn));
  21. }
  22. std::vector<std::any> message_bus_impl::find(const std::string& str_topic) {
  23. std::vector<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.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. }