allow to compare C class instances from lua
authorRobin Gareus <robin@gareus.org>
Thu, 14 Apr 2016 01:08:02 +0000 (03:08 +0200)
committerRobin Gareus <robin@gareus.org>
Thu, 14 Apr 2016 01:08:02 +0000 (03:08 +0200)
libs/ardour/luabindings.cc
libs/lua/LuaBridge/detail/CFunctions.h
libs/lua/LuaBridge/detail/Namespace.h

index 5dcd0964cd755dbdf4d19d294d38b2029d5b54ac..2fa6fa0604dd780cb37d0a4f45e40b4e5cd7803d 100644 (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()
index 29b361b69085730662e04c708966c40ae9e16136..6f439192163948f6ab3acec5d907df58f3f3c24b 100644 (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
   {
index 722d02e4a766065b4e1f02a822cf09ad8752b2b7..c859d8d151b1cd2790622daf4cac5f01cd1931ed 100644 (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
       {