2
0

core-message-stream.template.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifndef AFUN_CORE_MESSAGE_STREAM_TEMPLATE_H
  2. #define AFUN_CORE_MESSAGE_STREAM_TEMPLATE_H
  3. #include "core-message-stream.h"
  4. namespace aFuncore {
  5. template<class T>
  6. T *MessageStream::getMessage(const std::string &type) const {
  7. Message *msg = this->_getMessage(type);
  8. T *ret = dynamic_cast<T*>(msg);
  9. return ret;
  10. }
  11. template <typename Callable, typename...T>
  12. void MessageStream::forEach(Callable func, T...arg) {
  13. for (auto &msg : stream)
  14. func(msg.second, arg...);
  15. }
  16. template<typename Callable, typename... T>
  17. void UpMessageStream::forEachAll(Callable func, T... arg) {
  18. for (const UpMessageStream *up = this; up != nullptr; up = up->old)
  19. up->MessageStream::forEach(func, arg...);
  20. }
  21. template<typename Callable, typename... T>
  22. void InterMessageStream::forEach(Callable func, T... arg) {
  23. std::unique_lock<std::mutex> mutex{lock};
  24. for (auto &msg : stream) {
  25. mutex.unlock();
  26. func(msg.second, arg...);
  27. mutex.lock();
  28. }
  29. }
  30. template<typename Callable, typename... T>
  31. void InterMessageStream::forEachLock(Callable func, T... arg) {
  32. std::unique_lock<std::mutex> mutex{lock};
  33. for (auto &msg : stream)
  34. func(msg.second, arg...);
  35. }
  36. }
  37. #endif //AFUN_CORE_MESSAGE_STREAM_TEMPLATE_H