summaryrefslogtreecommitdiffstats
path: root/src/BlockTypeRegistry.cpp
blob: 45ad20082536b6f86aeb48f93cf0baa8ec359eef (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#include "Globals.h"
#include "BlockTypeRegistry.h"




////////////////////////////////////////////////////////////////////////////////
// BlockInfo:

BlockInfo::BlockInfo(
	const AString & aPluginName,
	const AString & aBlockTypeName,
	std::shared_ptr<cBlockHandler> aHandler,
	const std::map<AString, AString> & aHints,
	const std::map<AString, BlockInfo::HintCallback> & aHintCallbacks
):
	mPluginName(aPluginName),
	mBlockTypeName(aBlockTypeName),
	mHandler(aHandler),
	mHints(aHints),
	mHintCallbacks(aHintCallbacks)
{
}





AString BlockInfo::hintValue(
	const AString & aHintName,
	const BlockState & aBlockState
)
{
	// Search the hint callbacks first:
	auto itrC = mHintCallbacks.find(aHintName);
	if (itrC != mHintCallbacks.end())
	{
		// Hint callback found, use it:
		return itrC->second(mBlockTypeName, aBlockState);
	}

	// Search the static hints:
	auto itr = mHints.find(aHintName);
	if (itr != mHints.end())
	{
		// Hint found, use it:
		return itr->second;
	}

	// Nothing found, return empty string:
	return AString();
}





////////////////////////////////////////////////////////////////////////////////
// BlockTypeRegistry:

void BlockTypeRegistry::registerBlockType(
	const AString & aPluginName,
	const AString & aBlockTypeName,
	std::shared_ptr<cBlockHandler> aHandler,
	const std::map<AString, AString> & aHints,
	const std::map<AString, BlockInfo::HintCallback> & aHintCallbacks
)
{
	auto blockInfo = std::make_shared<BlockInfo>(aPluginName, aBlockTypeName, aHandler, aHints, aHintCallbacks);

	// Check previous registrations:
	cCSLock lock(mCSRegistry);
	auto itr = mRegistry.find(aBlockTypeName);
	if (itr != mRegistry.end())
	{
		if (itr->second->pluginName() != aPluginName)
		{
			throw AlreadyRegisteredException(itr->second, blockInfo);
		}
	}

	// Store the registration:
	mRegistry[aBlockTypeName] = blockInfo;
}





std::shared_ptr<BlockInfo> BlockTypeRegistry::blockInfo(const AString & aBlockTypeName)
{
	cCSLock lock(mCSRegistry);
	auto itr = mRegistry.find(aBlockTypeName);
	if (itr == mRegistry.end())
	{
		return nullptr;
	}
	return itr->second;
}





void BlockTypeRegistry::removeAllByPlugin(const AString & aPluginName)
{
	cCSLock lock(mCSRegistry);
	for (auto itr = mRegistry.begin(); itr != mRegistry.end();)
	{
		if (itr->second->pluginName() == aPluginName)
		{
			itr = mRegistry.erase(itr);
		}
		else
		{
			++itr;
		}
	}
}





////////////////////////////////////////////////////////////////////////////////
// BlockTypeRegistry::AlreadyRegisteredException:

BlockTypeRegistry::AlreadyRegisteredException::AlreadyRegisteredException(
	std::shared_ptr<BlockInfo> aPreviousRegistration,
	std::shared_ptr<BlockInfo> aNewRegistration
) :
	Super(message(aPreviousRegistration, aNewRegistration)),
	mPreviousRegistration(aPreviousRegistration),
	mNewRegistration(aNewRegistration)
{
}





AString BlockTypeRegistry::AlreadyRegisteredException::message(
	std::shared_ptr<BlockInfo> aPreviousRegistration,
	std::shared_ptr<BlockInfo> aNewRegistration
)
{
	return Printf("Attempting to register BlockTypeName %s from plugin %s, while it is already registered in plugin %s",
		aNewRegistration->blockTypeName().c_str(),
		aNewRegistration->pluginName().c_str(),
		aPreviousRegistration->pluginName().c_str()
	);
}