← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin] Rev 18: eliminate some superfluous casts and don't remove hubs twice.

 

------------------------------------------------------------
revno: 18
committer: crise <crise@xxxxxxxxxx>
branch nick: ScriptPlugin
timestamp: Fri 2013-06-14 01:48:21 +0300
message:
  eliminate some superfluous casts and don't remove hubs twice.
modified:
  src/Plugin.cpp
  src/ScriptInstance.cpp
  src/ScriptInstance.h


--
lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin
https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin

Your team Dcplusplus-team is subscribed to branch lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin/+edit-subscription
=== modified file 'src/Plugin.cpp'
--- src/Plugin.cpp	2013-06-13 21:33:08 +0000
+++ src/Plugin.cpp	2013-06-13 22:48:21 +0000
@@ -53,12 +53,10 @@
 Plugin::~Plugin() {
 	if(L) {
 		if(!hubs.empty()) {
-			for(auto i: hubs) {
-				Hubs::handle()->remove_hub(i.second);
+			// This is only relevant for runtime unloads
+			for(auto i: hubs)
 				Hubs::handle()->release(i.second);
-			}
 
-			Logger::log("Script plugin warning: scripts do not correctly remove hubs they add!");
 			hubs.clear();
 		}
 
@@ -191,12 +189,8 @@
 }
 
 void Plugin::destroyHub(HubDataPtr hHub) {
-	auto i = hubs.find(hHub->url);
-	if(i != hubs.end()) {
-		Hubs::handle()->remove_hub(i->second);
-		Hubs::handle()->release(i->second);
-		hubs.erase(i);
-	}
+	// onHubDisconnected cleans up the copy from createHub
+	Hubs::handle()->remove_hub(hHub);
 }
 
 HubDataPtr Plugin::addHub(HubDataPtr hHub) {
@@ -226,7 +220,6 @@
 	if(j != chatCache.end())
 			chatCache.erase(j);
 
-	Hubs::handle()->remove_hub(i->second);
 	Hubs::handle()->release(i->second);
 	hubs.erase(i);
 	return true;
@@ -326,17 +319,18 @@
 	Lock l(cs);
 	// TODO DC++ may trigger this incorrectly for disconnected hubs...
 	if ((hHub = findHub(hHub->url)) != nullptr)
-		return MakeCall("dcpp", "OnCommandEnter", 1, hHub, string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+		return MakeCall("dcpp", "OnCommandEnter", 1, hHub, string(message)) ? GetLuaBool() : false;
 	return false;
 }
 
 bool Plugin::onHubConnected(HubDataPtr hHub) {
+	Lock l(cs);
 	MakeCall(GetHubType(hHub), "OnHubAdded", 0, addHub(hHub));
-
 	return false;
 }
 
 bool Plugin::onHubDisconnected(HubDataPtr hHub) {
+	Lock l(cs);
 	// TODO DC++ may trigger this incorrectly (for hubs where OnHubAdded was never invoked), if socket creation fails...
 	if ((hHub = findHub(hHub->url)) != nullptr) {
 		MakeCall(GetHubType(hHub), "OnHubRemoved", 0, hHub);
@@ -348,7 +342,7 @@
 
 bool Plugin::onHubDataIn(HubDataPtr hHub, char* message) {
 	Lock l(cs);
-	return MakeCall(GetHubType(hHub), "DataArrival", 1, findHub(hHub->url), string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+	return MakeCall(GetHubType(hHub), "DataArrival", 1, findHub(hHub->url), string(message)) ? GetLuaBool() : false;
 }
 
 bool Plugin::onHubDataOut(HubDataPtr hHub, char* message, bool& bBreak) {
@@ -379,12 +373,12 @@
 
 bool Plugin::onConnectionDataIn(ConnectionDataPtr hConn, char* message) {
 	Lock l(cs);
-	return MakeCall("dcpp", "UserDataIn", 1, hConn, string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+	return MakeCall("dcpp", "UserDataIn", 1, hConn, string(message)) ? GetLuaBool() : false;
 }
 
 bool Plugin::onConnectionDataOut(ConnectionDataPtr hConn, char* message) {
 	Lock l(cs);
-	return MakeCall("dcpp", "UserDataOut", 1, hConn, string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+	return MakeCall("dcpp", "UserDataOut", 1, hConn, string(message)) ? GetLuaBool() : false;
 }
 
 bool Plugin::onFormatChat(UserDataPtr hUser, StringDataPtr line, bool& bBreak) {

=== modified file 'src/ScriptInstance.cpp'
--- src/ScriptInstance.cpp	2013-06-03 21:13:54 +0000
+++ src/ScriptInstance.cpp	2013-06-13 22:48:21 +0000
@@ -78,14 +78,14 @@
 }
 
 // Get value from top of stack, check if should cancel message.
-Bool ScriptInstance::GetLuaBool() {
+bool ScriptInstance::GetLuaBool() {
 	bool ret = false;
 	if (lua_gettop(L) > 0) {
 		ret = !lua_isnil(L, -1);
 		lua_pop(L, 1);
 	}
 
-	return ret ? True : False;
+	return ret;
 }
 
 void ScriptInstance::EvaluateChunk(const string& chunk) {

=== modified file 'src/ScriptInstance.h'
--- src/ScriptInstance.h	2013-06-03 21:13:54 +0000
+++ src/ScriptInstance.h	2013-06-13 22:48:21 +0000
@@ -54,7 +54,7 @@
 
 	bool CheckFunction(const string& table, const string& method);
 	string GetHubType(HubDataPtr aHub) { return (aHub->protocol == PROTOCOL_ADC ? "adch" : "nmdch"); }
-	Bool GetLuaBool();
+	bool GetLuaBool();
 
 	static lua_State* L;
 	static CriticalSection cs;