Skip to content

Commit

Permalink
Remove old style events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 committed Jul 26, 2013
1 parent b2e4c74 commit e7c7e57
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 102 deletions.
3 changes: 0 additions & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ Refactoring
and is good style anyway.
* StateManager / State needs to be completely rewritten: It's way to
complicated without providing any benefits and, worse, lowers flexibility.
* Maybe replace jd.connect with a Lua function <-> luabind::object() converter
so that jd.connect() is not necessary anymore and
fooComponent:onBar(f) can be used instead.
* Get rid of LuaComponent::bindLuaPart(): Maybe luabind::adopt() is now
capable of replacing it.
* Exceptions: Use boost::exception/error_info system or at least a proper
Expand Down
8 changes: 8 additions & 0 deletions base.jd/lua/lib/evt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ local tabutil = require 'tabutil'
local util = require 'util'
local oo = require 'oo'

-- Compatibility
function jd.connect(component, event, callback)
-- When we get event="foo", we need to call the components onFoo function,
-- i.e. the first letter becomes uppercase.
local event = event:gsub("^%l", string.upper)
return component["on" .. event](component, callback)
end

local M = { }

do
Expand Down
18 changes: 0 additions & 18 deletions src/compsys/BasicMetaComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ class BasicMetaComponent: public MetaComponent {
}
};

#define JD_EVT_METACOMPONENT(c, bc) \
namespace { \
class c##Meta: public bc { \
public: \
virtual ssig::ConnectionBase* connectEvent( \
lua_State*, \
Component*, \
std::string const& name) const; \
}; \
} // anonymous namespace


#define JD_BASIC_COMPONENT_IMPL(c) JD_COMPONENT_IMPL(c, BasicMetaComponent<c>)

#define JD_BASIC_EVT_COMPONENT_IMPL(c) \
JD_EVT_METACOMPONENT(c, BasicMetaComponent<c>) \
JD_COMPONENT_IMPL(c, c##Meta)



#endif
26 changes: 0 additions & 26 deletions src/compsys/MetaComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ static luabind::object Component_parent(Component const& c, lua_State* L)
return luabind::object();
}

static ssig::ConnectionBase* connectEvent(
lua_State* L,
Component& sender,
std::string const& eventname,
luabind::object const& receiver)
{
receiver.push(L);
ssig::ConnectionBase* result = sender.metaComponent().connectEvent(
L, &sender, eventname);
lua_pop(L, 1);
if (!result)
throw luaU::Error("Event \"" + eventname + "\" not found!");
return result;
}

class wrap_Component: public Component, public luabind::wrap_base {
public:
wrap_Component(lua_State* L, Entity& parent, std::string const& classname)
Expand Down Expand Up @@ -145,7 +130,6 @@ static void init(LuaVm& vm)
.def(tostring(const_self))
.LHISREFVALID2(Component),
def("registerComponent", &registerMetaComponent),
def("connect", &connectEvent, adopt(result)),
class_<ssig::ConnectionBase, wrap_ConnectionBase>("ConnectionBase")
.def(constructor<>())
.def("disconnect", &ssig::ConnectionBase::disconnect)
Expand Down Expand Up @@ -200,13 +184,3 @@ void LuaMetaComponent::castDown(lua_State* L, Component* c) const
luabind::object o(L, c->ref<wrap_Component>());
o.push(L);
}

ssig::ConnectionBase* LuaMetaComponent::connectEvent(lua_State* L, Component* c, std::string const& name) const
{
using namespace luabind;
object receiver(from_stack(L, -1));
object callback = globals(L)[jd::moduleName]["callback_connectLuaEvent"];
if (type(callback) != LUA_TFUNCTION)
throw std::runtime_error("no callback for event connection defined (must be a plain function)");
return object_cast<ssig::ConnectionBase*>(callback(c, name, receiver)[adopt(result)]);
}
10 changes: 0 additions & 10 deletions src/compsys/MetaComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


struct lua_State;
namespace ssig { class ConnectionBase; }
class Component;

class InvalidMetaComponentName: public std::runtime_error {
Expand All @@ -34,13 +33,6 @@ class InvalidMetaComponentName: public std::runtime_error {
assert("not scriptable!" && false);
throw std::runtime_error("castDown() is not implemented.");
}

// receiver is on top of stack; return value disconnects automatically on deletion
virtual ssig::ConnectionBase* connectEvent(lua_State*, Component*, std::string const& name) const
{
(void)name;
return nullptr;
}
};

// Two MetaComponent instances are equal if and only if they have the same
Expand All @@ -58,8 +50,6 @@ class LuaMetaComponent: public MetaComponent {
std::string const& name() const override;

void castDown(lua_State* L, Component* c) const override;
ssig::ConnectionBase* connectEvent(
lua_State* L, Component* c, std::string const& name) const override;

private:
std::string const m_name;
Expand Down
44 changes: 8 additions & 36 deletions src/luaexport/LuaEventHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,23 @@
#include <luabind/adopt_policy.hpp>
#include <ssig.hpp>

#define LUA_EVENT_HELPERS_MAX_ARGS SSIG_MAX_ARGS

#define JD_EVENT_ENTRY(ename, r, ...) \
if (name == BOOST_STRINGIZE(ename)) \
return makeConnection(cc->connect_##ename(boost::bind( \
LuaFunction<r>(recv), __VA_ARGS__))); \
else { }

#define JD_EVENT_ENTRY0(ename, r) \
if (name == BOOST_STRINGIZE(ename)) \
return makeConnection(cc->connect_##ename(boost::bind( \
&luabind::call_function<r>, recv))); \
else { }

#define JD_EVENT_TABLE_BEGIN(ct) \
ssig::ConnectionBase* ct##Meta::connectEvent( \
lua_State* L, \
Component* c, \
std::string const& name) const \
{ \
ct* cc = c->as<ct>(); \
luabind::object recv(luabind::from_stack(L, -1)); \
using boost::ref; using boost::cref;

#define JD_EVENT_TABLE_END return nullptr; }

#define JD_EVENT(name, cname) def("on" #cname, &LHCURCLASS::connect_##name)

template<typename Signature>
ssig::ConnectionBase* makeConnection(
ssig::Connection<Signature> const& con)
{
return new ssig::ScopedConnection<Signature>(con);
}

namespace luabind {
template <typename Signature>
struct default_converter<ssig::Connection<Signature>>
: native_converter_base<ssig::Connection<Signature>>
{
void to(lua_State* L, ssig::Connection<Signature> const& value)
{
luabind::object o(L, makeConnection(value), luabind::adopt(luabind::result));
luabind::object o(L, makeConnection(value),
luabind::adopt(luabind::result));
o.push(L);
}
private:
ssig::ConnectionBase* makeConnection(
ssig::Connection<Signature> const& con)
{
return new ssig::ScopedConnection<Signature>(con);
}
};

template <typename Signature>
Expand Down
9 changes: 0 additions & 9 deletions src/luaexport/LuaExportHelpers.cpp

This file was deleted.

0 comments on commit e7c7e57

Please sign in to comment.