SubscriptionRegistryProxy.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SubscriptionRegistryProxy.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <vcmi/events/Event.h>
  12. #include <vcmi/events/EventBus.h>
  13. #include <vcmi/events/SubscriptionRegistry.h>
  14. #include "../../LuaWrapper.h"
  15. #include "../../LuaStack.h"
  16. #include "../../LuaReference.h"
  17. namespace scripting
  18. {
  19. namespace api
  20. {
  21. namespace events
  22. {
  23. class EventSubscriptionProxy : public UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>
  24. {
  25. public:
  26. using Wrapper = UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>;
  27. static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
  28. };
  29. template <typename EventProxy>
  30. class SubscriptionRegistryProxy
  31. {
  32. public:
  33. using EventType = typename EventProxy::ObjectType;
  34. using RegistryType = ::events::SubscriptionRegistry<EventType>;
  35. static_assert(std::is_base_of<::events::Event, EventType>::value, "Invalid template parameter");
  36. static int subscribeBefore(lua_State * L)
  37. {
  38. LuaStack S(L);
  39. // subscription = subscribeBefore(eventBus, callback)
  40. //TODO: use capture by move from c++14
  41. auto callbackRef = std::make_shared<LuaReference>(L);
  42. ::events::EventBus * eventBus = nullptr;
  43. if(!S.tryGet(1, eventBus))
  44. {
  45. S.push("No event bus");
  46. return 1;
  47. }
  48. S.clear();
  49. RegistryType * registry = EventType::getRegistry();
  50. typename EventType::PreHandler callback = [=](EventType & event)
  51. {
  52. LuaStack S(L);
  53. callbackRef->push();
  54. S.push(&event);
  55. if(lua_pcall(L, 1, 0, 0) != 0)
  56. {
  57. std::string msg;
  58. S.tryGet(1, msg);
  59. logMod->error("Script callback error: %s", msg);
  60. }
  61. S.clear();
  62. };
  63. std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeBefore(eventBus, std::move(callback));
  64. S.push(std::move(subscription));
  65. return 1;
  66. }
  67. static int subscribeAfter(lua_State * L)
  68. {
  69. LuaStack S(L);
  70. //TODO: use capture by move from c++14
  71. auto callbackRef = std::make_shared<LuaReference>(L);
  72. ::events::EventBus * eventBus = nullptr;
  73. if(!S.tryGet(1, eventBus))
  74. {
  75. S.push("No event bus");
  76. return 1;
  77. }
  78. S.clear();
  79. RegistryType * registry = EventType::getRegistry();
  80. typename EventType::PostHandler callback = [=](const EventType & event)
  81. {
  82. LuaStack S(L);
  83. callbackRef->push();
  84. S.push(const_cast<EventType *>(&event)); //FIXME:
  85. if(lua_pcall(L, 1, 0, 0) != 0)
  86. {
  87. std::string msg;
  88. S.tryGet(1, msg);
  89. logMod->error("Script callback error: %s", msg);
  90. }
  91. S.clear();
  92. };
  93. std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeAfter(eventBus, std::move(callback));
  94. S.push(std::move(subscription));
  95. return 1;
  96. }
  97. };
  98. }
  99. }
  100. }