File: SubscriptionRegistryProxy.h

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (134 lines) | stat: -rw-r--r-- 2,891 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * SubscriptionRegistryProxy.h, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */

#pragma once

#include <vcmi/events/Event.h>
#include <vcmi/events/EventBus.h>
#include <vcmi/events/SubscriptionRegistry.h>

#include "../../LuaWrapper.h"
#include "../../LuaStack.h"
#include "../../LuaReference.h"

VCMI_LIB_NAMESPACE_BEGIN

namespace scripting
{
namespace api
{
namespace events
{


class EventSubscriptionProxy : public UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>
{
public:
	using Wrapper = UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>;
	static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
};

template <typename EventProxy>
class SubscriptionRegistryProxy
{
public:
	using EventType = typename EventProxy::ObjectType;
	using RegistryType = ::events::SubscriptionRegistry<EventType>;

	static_assert(std::is_base_of<::events::Event, EventType>::value, "Invalid template parameter");

	static int subscribeBefore(lua_State * L)
	{
		LuaStack S(L);
		// subscription = subscribeBefore(eventBus, callback)

		//TODO: use capture by move from c++14
		auto callbackRef = std::make_shared<LuaReference>(L);

		::events::EventBus * eventBus = nullptr;

		if(!S.tryGet(1, eventBus))
		{
			S.push("No event bus");
			return 1;
		}

		S.clear();

		RegistryType * registry = EventType::getRegistry();

		typename EventType::PreHandler callback = [=](EventType & event)
		{
			LuaStack S(L);
			callbackRef->push();
			S.push(&event);

			if(lua_pcall(L, 1, 0, 0) != 0)
			{
				std::string msg;
				S.tryGet(1, msg);
				logMod->error("Script callback error: %s", msg);
			}

			S.clear();
		};

		std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeBefore(eventBus, std::move(callback));
		S.push(std::move(subscription));
		return 1;
	}

	static int subscribeAfter(lua_State * L)
	{
		LuaStack S(L);

		//TODO: use capture by move from c++14
		auto callbackRef = std::make_shared<LuaReference>(L);

		::events::EventBus * eventBus = nullptr;

		if(!S.tryGet(1, eventBus))
		{
			S.push("No event bus");
			return 1;
		}

		S.clear();

		RegistryType * registry = EventType::getRegistry();

		typename EventType::PostHandler callback = [=](const EventType & event)
		{
			LuaStack S(L);
			callbackRef->push();
			S.push(const_cast<EventType *>(&event)); //FIXME:

			if(lua_pcall(L, 1, 0, 0) != 0)
			{
				std::string msg;
				S.tryGet(1, msg);
				logMod->error("Script callback error: %s", msg);
			}

			S.clear();
		};

		std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeAfter(eventBus, std::move(callback));
		S.push(std::move(subscription));
		return 1;
	}
};


}
}
}

VCMI_LIB_NAMESPACE_END