← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3126: plugin SDK: start a C++ wrapper to control hooks; no longer requires stateless lambdas

 

------------------------------------------------------------
revno: 3126
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Sat 2012-11-10 18:16:16 +0100
message:
  plugin SDK: start a C++ wrapper to control hooks; no longer requires stateless lambdas
added:
  pluginsdk/cpp/pluginsdk/Hooks.cpp
  pluginsdk/cpp/pluginsdk/Hooks.h
modified:
  plugins/Dev/Plugin.cpp
  plugins/Dev/Plugin.h
  plugins/Dev/pluginsdk.cpp
  pluginsdk/c/pluginsdk/Config.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'plugins/Dev/Plugin.cpp'
--- plugins/Dev/Plugin.cpp	2012-11-08 12:41:54 +0000
+++ plugins/Dev/Plugin.cpp	2012-11-10 17:16:16 +0000
@@ -21,11 +21,13 @@
 
 #include <pluginsdk/Config.h>
 #include <pluginsdk/Core.h>
+#include <pluginsdk/Hooks.h>
 #include <pluginsdk/Logger.h>
 #include <pluginsdk/Util.h>
 
 using dcapi::Config;
 using dcapi::Core;
+using dcapi::Hooks;
 using dcapi::Logger;
 using dcapi::Util;
 
@@ -77,8 +79,8 @@
 }
 
 void Plugin::addHooks() {
-	events[HOOK_NETWORK_HUB_IN] = hooks->bind_hook(HOOK_NETWORK_HUB_IN, [](dcptr_t pObject, dcptr_t pData, dcptr_t, Bool*) {
-		return instance->onHubDataIn(reinterpret_cast<HubDataPtr>(pObject), reinterpret_cast<char*>(pData)); }, nullptr);
+	Hooks::onHubDataIn([this](HubDataPtr hHub, const char* message, bool&) { return onHubDataIn(hHub, message); });
+	/*
 	events[HOOK_NETWORK_HUB_OUT] = hooks->bind_hook(HOOK_NETWORK_HUB_OUT, [](dcptr_t pObject, dcptr_t pData, dcptr_t, Bool*) {
 		return instance->onHubDataOut(reinterpret_cast<HubDataPtr>(pObject), reinterpret_cast<char*>(pData)); }, nullptr);
 	events[HOOK_NETWORK_CONN_IN] = hooks->bind_hook(HOOK_NETWORK_CONN_IN, [](dcptr_t pObject, dcptr_t pData, dcptr_t, Bool*) {
@@ -88,13 +90,11 @@
 	events[HOOK_UI_PROCESS_CHAT_CMD] = hooks->bind_hook(HOOK_UI_PROCESS_CHAT_CMD, [](dcptr_t pObject, dcptr_t pData, dcptr_t, Bool*) {
 		auto cmd = reinterpret_cast<CommandDataPtr>(pData);
 		if(cmd->isPrivate) { return False; }
-		return instance->onChatCommand(reinterpret_cast<HubDataPtr>(pObject), cmd); }, nullptr);
+		return instance->onChatCommand(reinterpret_cast<HubDataPtr>(pObject), cmd); }, nullptr);*/
 }
 
 void Plugin::clearHooks() {
-	for(auto& i: events)
-		hooks->release_hook(i.second);
-	events.clear();
+	Hooks::clear();
 }
 
 void Plugin::start() {
@@ -112,12 +112,10 @@
 }
 
 void Plugin::onLoad(DCCorePtr core, bool install, Bool& loadRes) {
-	hooks = reinterpret_cast<DCHooksPtr>(core->query_interface(DCINTF_HOOKS, DCINTF_HOOKS_VER));
-
 	hubs = reinterpret_cast<DCHubPtr>(core->query_interface(DCINTF_DCPP_HUBS, DCINTF_DCPP_HUBS_VER));
 	ui = reinterpret_cast<DCUIPtr>(core->query_interface(DCINTF_DCPP_UI, DCINTF_DCPP_UI_VER));
 
-	if(!Util::init(core) || !Config::init(core) || !Logger::init(core) || !hooks || !hubs || !ui) {
+	if(!Config::init(core) || !Hooks::init(core) || !Logger::init(core) || !Util::init(core) || !hubs || !ui) {
 		loadRes = False;
 		return;
 	}
@@ -138,7 +136,7 @@
 
 void Plugin::onSwitched() {
 	auto oldCommand = commandName;
-	if(events.empty()) {
+	if(Hooks::empty()) {
 		start();
 	} else {
 		close();

=== modified file 'plugins/Dev/Plugin.h'
--- plugins/Dev/Plugin.h	2012-11-08 12:41:54 +0000
+++ plugins/Dev/Plugin.h	2012-11-10 17:16:16 +0000
@@ -19,11 +19,8 @@
 #ifndef PLUGINS_DEV_PLUGIN_H
 #define PLUGINS_DEV_PLUGIN_H
 
-#include <map>
-
 #include "Dialog.h"
 
-using std::map;
 using std::string;
 
 class Plugin
@@ -51,9 +48,6 @@
 	Bool onConnectionDataOut(ConnectionDataPtr hConn, const char* message);
 	Bool onChatCommand(HubDataPtr hub, CommandDataPtr cmd);
 
-	map<string, subsHandle> events;
-
-	DCHooksPtr hooks;
 	DCHubPtr hubs;
 	DCUIPtr ui;
 
@@ -61,8 +55,6 @@
 
 	string commandName;
 
-	/** @todo switch to dcpp::Singleton when <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51494>
-	is fixed */
 	static Plugin* instance;
 };
 

=== modified file 'plugins/Dev/pluginsdk.cpp'
--- plugins/Dev/pluginsdk.cpp	2012-11-04 19:15:24 +0000
+++ plugins/Dev/pluginsdk.cpp	2012-11-10 17:16:16 +0000
@@ -5,5 +5,6 @@
 
 #include <pluginsdk/Core.cpp>
 #include <pluginsdk/Config.cpp>
+#include <pluginsdk/Hooks.cpp>
 #include <pluginsdk/Logger.cpp>
 #include <pluginsdk/Util.cpp>

=== modified file 'pluginsdk/c/pluginsdk/Config.h'
--- pluginsdk/c/pluginsdk/Config.h	2012-11-10 03:44:55 +0000
+++ pluginsdk/c/pluginsdk/Config.h	2012-11-10 17:16:16 +0000
@@ -21,6 +21,18 @@
 #ifndef PLUGINSDK_CONFIG_H
 #define PLUGINSDK_CONFIG_H
 
+// TODO need this for size_t...
+#ifdef _WIN32
+
+#include <windows.h>
+#include <tchar.h>
+
+#else
+
+#include <unistd.h>
+
+#endif
+
 #include <stdint.h>
 
 #include <pluginsdk/PluginDefs.h>

=== added file 'pluginsdk/cpp/pluginsdk/Hooks.cpp'
--- pluginsdk/cpp/pluginsdk/Hooks.cpp	1970-01-01 00:00:00 +0000
+++ pluginsdk/cpp/pluginsdk/Hooks.cpp	2012-11-10 17:16:16 +0000
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2012 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* Helpers around the DCCore interface. */
+
+#include "Hooks.h"
+
+namespace dcapi {
+
+DCHooksPtr Hooks::hooks;
+map<string, pair<subsHandle, Hooks::Callback>> Hooks::events;
+
+bool Hooks::init(DCCorePtr core) {
+	init(reinterpret_cast<DCHooksPtr>(core->query_interface(DCINTF_HOOKS, DCINTF_HOOKS_VER)));
+	return hooks;
+}
+void Hooks::init(DCHooksPtr coreHooks) { hooks = coreHooks; }
+
+void Hooks::onHubDataIn(function<bool (HubDataPtr, char*, bool&)> f) {
+	addEvent(HOOK_NETWORK_HUB_IN, [f](dcptr_t pObject, dcptr_t pData, bool& bBreak) {
+		return f(reinterpret_cast<HubDataPtr>(pObject), reinterpret_cast<char*>(pData), bBreak); });
+}
+
+bool Hooks::empty() {
+	return events.empty();
+}
+
+void Hooks::clear() {
+	for(auto& i: events)
+		hooks->release_hook(i.second.first);
+	events.clear();
+}
+
+void Hooks::addEvent(const char* id, Callback f) {
+	// insert first to construct map keys etc; then create the hook, using the map key as pCommon.
+	auto it = events.insert(make_pair(id, make_pair(nullptr, f))).first;
+	it->second.first = hooks->bind_hook(id, eventCallback, reinterpret_cast<dcptr_t>(const_cast<char*>(it->first.c_str())));
+}
+
+Bool DCAPI Hooks::eventCallback(dcptr_t pObject, dcptr_t pData, dcptr_t pCommon, Bool* bBreak) {
+	bool bbBreak = *bBreak;
+	auto ret = events[reinterpret_cast<char*>(pCommon)].second(pObject, pData, bbBreak) ? True : False;
+	*bBreak = bbBreak ? True : False;
+	return ret;
+}
+
+} // namespace dcapi

=== added file 'pluginsdk/cpp/pluginsdk/Hooks.h'
--- pluginsdk/cpp/pluginsdk/Hooks.h	1970-01-01 00:00:00 +0000
+++ pluginsdk/cpp/pluginsdk/Hooks.h	2012-11-10 17:16:16 +0000
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2012 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* Helpers around the DCHooks interface. */
+
+#ifndef PLUGINSDK_HOOKS_H
+#define PLUGINSDK_HOOKS_H
+
+#include <cstdint>
+#include <functional>
+#include <map>
+#include <string>
+#include <utility>
+
+#include <pluginsdk/PluginDefs.h>
+
+namespace dcapi {
+
+using std::function;
+using std::map;
+using std::string;
+using std::pair;
+
+class Hooks
+{
+public:
+	static bool init(DCCorePtr core);
+	static void init(DCHooksPtr coreHooks);
+
+	static void onHubDataIn(function<bool (HubDataPtr, char*, bool&)> f);
+
+	static bool empty();
+	static void clear();
+
+private:
+	typedef function<bool (dcptr_t, dcptr_t, bool&)> Callback;
+	static void addEvent(const char* id, Callback f);
+	static Bool DCAPI eventCallback(dcptr_t pObject, dcptr_t pData, dcptr_t pCommon, Bool* bBreak);
+
+	static DCHooksPtr hooks;
+
+	static map<string, pair<subsHandle, Callback>> events;
+};
+
+} // namespace dcapi
+
+#endif