allow to compare C class instances from lua

This commit is contained in:
Robin Gareus 2016-04-14 03:08:02 +02:00
parent afca178e45
commit 204c8016c7
3 changed files with 31 additions and 0 deletions

View File

@ -1005,6 +1005,7 @@ LuaBindings::dsp (lua_State* L)
.beginNamespace ("ARDOUR")
.beginClass <AudioBuffer> ("AudioBuffer")
.addEqualCheck ()
.addFunction ("data", (Sample*(AudioBuffer::*)(framecnt_t))&AudioBuffer::data)
.addFunction ("silence", &AudioBuffer::silence)
.addFunction ("apply_gain", &AudioBuffer::apply_gain)
@ -1013,12 +1014,14 @@ LuaBindings::dsp (lua_State* L)
.endClass()
.beginClass <MidiBuffer> ("MidiBuffer")
.addEqualCheck ()
.addFunction ("silence", &MidiBuffer::silence)
.addFunction ("empty", &MidiBuffer::empty)
// TODO iterators..
.endClass()
.beginClass <BufferSet> ("BufferSet")
.addEqualCheck ()
.addFunction ("get_audio", static_cast<AudioBuffer&(BufferSet::*)(size_t)>(&BufferSet::get_audio))
.addFunction ("count", static_cast<const ChanCount&(BufferSet::*)()const>(&BufferSet::count))
.endClass()

View File

@ -366,6 +366,19 @@ struct CFunc
}
};
template <class T>
struct ClassEqualCheck
{
static int f (lua_State* L)
{
T const* const t0 = Userdata::get <T> (L, 1, true);
T const* const t1 = Userdata::get <T> (L, 2, true);
Stack <bool>::push (L, t0 == t1);
return 1;
}
};
template <class T>
struct PtrNullCheck
{

View File

@ -1039,6 +1039,15 @@ private:
return addConstructor <void (*) ()> ();
}
Class <T>& addEqualCheck ()
{
PRINTDOC("Member Function", _name << "sameinstance", std::string("bool"), std::string("void (*)(" + type_name <T>() + ")"))
assert (lua_istable (L, -1));
lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
rawsetfield (L, -3, "sameinstance");
return *this;
}
};
/** C Array to/from table */
@ -1060,6 +1069,8 @@ private:
std::string(""), "int (*)(lua_State*)")
PRINTDOC ("Ext C Function", _name << "set_table",
std::string(""), "int (*)(lua_State*)")
PRINTDOC("Member Function", _name << "sameinstance",
std::string("bool"), std::string("void (*)(" + type_name <T>() + "*)"))
m_stackSize = parent->m_stackSize + 3;
parent->m_stackSize = 0;
@ -1117,6 +1128,10 @@ private:
lua_pushcclosure (L, &CFunc::setTable <T>, 0);
rawsetfield (L, -3, "set_table"); // class table
lua_pushcclosure (L, &CFunc::ClassEqualCheck <T>::f, 0);
rawsetfield (L, -3, "sameinstance");
}
else
{