queue_in_actor.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "actor.h"
  3. #include "queue_for_actor.h"
  4. #include <functional>
  5. namespace NActor {
  6. template <typename TItem>
  7. class IQueueInActor {
  8. public:
  9. virtual void EnqueueAndScheduleV(const TItem& item) = 0;
  10. virtual void DequeueAllV() = 0;
  11. virtual void DequeueAllLikelyEmptyV() = 0;
  12. virtual ~IQueueInActor() {
  13. }
  14. };
  15. template <typename TThis, typename TItem, typename TActorTag = TDefaultTag, typename TQueueTag = TDefaultTag>
  16. class TQueueInActor: public IQueueInActor<TItem> {
  17. typedef TQueueInActor<TThis, TItem, TActorTag, TQueueTag> TSelf;
  18. public:
  19. // TODO: make protected
  20. TQueueForActor<TItem> QueueInActor;
  21. private:
  22. TActor<TThis, TActorTag>* GetActor() {
  23. return GetThis();
  24. }
  25. TThis* GetThis() {
  26. return static_cast<TThis*>(this);
  27. }
  28. void ProcessItem(const TItem& item) {
  29. GetThis()->ProcessItem(TActorTag(), TQueueTag(), item);
  30. }
  31. public:
  32. void EnqueueAndNoSchedule(const TItem& item) {
  33. QueueInActor.Enqueue(item);
  34. }
  35. void EnqueueAndSchedule(const TItem& item) {
  36. EnqueueAndNoSchedule(item);
  37. GetActor()->Schedule();
  38. }
  39. void EnqueueAndScheduleV(const TItem& item) override {
  40. EnqueueAndSchedule(item);
  41. }
  42. void Clear() {
  43. QueueInActor.Clear();
  44. }
  45. void DequeueAll() {
  46. QueueInActor.DequeueAll(std::bind(&TSelf::ProcessItem, this, std::placeholders::_1));
  47. }
  48. void DequeueAllV() override {
  49. return DequeueAll();
  50. }
  51. void DequeueAllLikelyEmpty() {
  52. QueueInActor.DequeueAllLikelyEmpty(std::bind(&TSelf::ProcessItem, this, std::placeholders::_1));
  53. }
  54. void DequeueAllLikelyEmptyV() override {
  55. return DequeueAllLikelyEmpty();
  56. }
  57. bool IsEmpty() {
  58. return QueueInActor.IsEmpty();
  59. }
  60. };
  61. }