/* * Copyright (C) 2010 - 2014 Eluna Lua Engine * This program is free software licensed under GPL version 3 * Please see the included DOCS/LICENSE.md for more information */ #ifndef _ELUNA_BINDING_H #define _ELUNA_BINDING_H #include "Common.h" #include "LuaEngine.h" #include "ElunaUtility.h" extern "C" { #include "lua.h" #include "lauxlib.h" }; class ElunaBind { public: typedef std::pair FunctionRefCounterPair; // (function reference, remaining calls counter) typedef std::vector FunctionRefVector; typedef UNORDERED_MAP EventToFunctionsMap; Eluna& E; const char* groupName; ElunaBind(const char* bindGroupName, Eluna& _E) : E(_E), groupName(bindGroupName) { } virtual ~ElunaBind() { Clear(); } // unregisters all registered functions and clears all registered events from the bindings virtual void Clear() { }; // Updates the counters on all temporary bindings and erases them if the counter would reach 0. virtual void UpdateTemporaryBindings() { }; }; template class EventBind : public ElunaBind { public: EventBind(const char* bindGroupName, Eluna& _E) : ElunaBind(bindGroupName, _E) { } // unregisters all registered functions and clears all registered events from the bind std::maps (reset) void Clear() override { for (EventToFunctionsMap::iterator itr = Bindings.begin(); itr != Bindings.end(); ++itr) { for (FunctionRefVector::iterator it = itr->second.begin(); it != itr->second.end(); ++it) luaL_unref(E.L, LUA_REGISTRYINDEX, (*it).first); itr->second.clear(); } Bindings.clear(); } void UpdateTemporaryBindings() override { for (EventToFunctionsMap::iterator itr = Bindings.begin(); itr != Bindings.end();) { for (FunctionRefVector::iterator it = itr->second.begin(); it != itr->second.end();) { uint32 counter = (*it).second; if (counter > 1) { counter--; (*it).second = counter; it++; } else if (counter == 1) { luaL_unref(E.L, LUA_REGISTRYINDEX, (*it).first); it = itr->second.erase(it); } else { it++; } } // If there are no more entries in the vector, erase the vector. if (itr->second.empty()) { itr = Bindings.erase(itr); } else { itr++; } } } void Insert(int eventId, int funcRef, uint32 shots) // Inserts a new registered event { Bindings[eventId].push_back(std::make_pair(funcRef, shots)); } // Checks if there are events for ID bool HasEvents(T eventId) const { if (Bindings.empty()) return false; if (Bindings.find(eventId) == Bindings.end()) return false; return true; } EventToFunctionsMap Bindings; // Binding store Bindings[eventId] = {(funcRef, counter)}; }; template class EntryBind : public ElunaBind { public: typedef UNORDERED_MAP EntryToEventsMap; EntryBind(const char* bindGroupName, Eluna& _E) : ElunaBind(bindGroupName, _E) { } // unregisters all registered functions and clears all registered events from the bindmap void Clear() override { for (EntryToEventsMap::iterator itr = Bindings.begin(); itr != Bindings.end(); ++itr) { for (EventToFunctionsMap::iterator it = itr->second.begin(); it != itr->second.end(); ++it) { for (FunctionRefVector::iterator i = it->second.begin(); i != it->second.end(); ++i) luaL_unref(E.L, LUA_REGISTRYINDEX, (*i).first); it->second.clear(); } itr->second.clear(); } Bindings.clear(); } void UpdateTemporaryBindings() override { for (EntryToEventsMap::iterator itr = Bindings.begin(); itr != Bindings.end();) { for (EventToFunctionsMap::iterator it = itr->second.begin(); it != itr->second.end();) { for (FunctionRefVector::iterator i = it->second.begin(); i != it->second.end();) { uint32 counter = (*i).second; if (counter > 1) { counter--; (*i).second = counter; i++; } else if (counter == 1) { luaL_unref(E.L, LUA_REGISTRYINDEX, (*i).first); i = it->second.erase(i); } else { i++; } } // If there are no more entries in the vector, erase the vector. if (it->second.empty()) { it = itr->second.erase(it); } else { it++; } } // If there are no more vector in the map, erase the map. if (itr->second.empty()) { itr = Bindings.erase(itr); } else { itr++; } } } void Insert(uint32 entryId, int eventId, int funcRef, uint32 shots) // Inserts a new registered event { Bindings[entryId][eventId].push_back(std::make_pair(funcRef, shots)); } // Returns true if the entry has registered binds bool HasEvents(uint32 entryId, int eventId) const { if (Bindings.empty()) return false; EntryToEventsMap::const_iterator itr = Bindings.find(entryId); if (itr == Bindings.end()) return false; return itr->second.find(eventId) != itr->second.end(); } bool HasEvents(uint32 entryId) const { if (Bindings.empty()) return false; return Bindings.find(entryId) != Bindings.end(); } EntryToEventsMap Bindings; // Binding store Bindings[entryId][eventId] = {(funcRef, counter)}; }; #endif