]> git.donarmstrong.com Git - lilypond.git/commitdiff
Issue 4842/2: Add Method_instance class
authorDavid Kastrup <dak@gnu.org>
Thu, 28 Apr 2016 23:34:28 +0000 (01:34 +0200)
committerDavid Kastrup <dak@gnu.org>
Sun, 8 May 2016 15:17:00 +0000 (17:17 +0200)
This is a lightweight container class combining an SCM method call
with a particular instance into a C++ callable.

lily/callback.cc
lily/include/callback.hh

index fac40a02ded106a8dd465e47ef67acb167e434cc..baab7b06144f85f75a412754263a6f52ff35e33b 100644 (file)
@@ -22,3 +22,4 @@
 const char * const Callback_wrapper::type_p_name_ = 0;
 const char * const Callback2_wrapper::type_p_name_ = 0;
 const char * const Callback0_wrapper::type_p_name_ = 0;
+const char * const Method_instance::type_p_name_ = 0;
index 7151d04478cf400fe7bc61c56fbf05973b50e0ad..963f4eb59cdf633435b16aa0a479068981c628b8 100644 (file)
@@ -146,4 +146,45 @@ public:
   }
 };
 
+// The following will usually be used unsmobbified, relying on its
+// constituents being protected independently.
+
+class Method_instance : public Simple_smob<Method_instance>
+{
+  SCM method_, instance_;
+public:
+  static const char * const type_p_name_; // = 0
+  LY_DECLARE_SMOB_PROC (&Method_instance::call, 0, 0, 1)
+  SCM call (SCM rest)
+  {
+    return scm_apply_1 (method_, instance_, rest);
+  }
+
+  Method_instance (SCM method, SCM instance)
+    : method_ (method), instance_ (instance)
+  { }
+  Method_instance (SCM method, Smob_core *instance)
+    : method_ (method), instance_ (instance->self_scm ())
+  { }
+  SCM method () const { return method_; }
+  SCM instance () const { return instance_; }
+  SCM operator () () const
+  {
+    return scm_call_1 (method_, instance_);
+  }
+  SCM operator () (SCM arg) const
+  {
+    return scm_call_2 (method_, instance_, arg);
+  }
+  SCM operator () (SCM arg1, SCM arg2) const
+  {
+    return scm_call_3 (method_, instance_, arg1, arg2);
+  }
+  SCM mark_smob () const
+  {
+    scm_gc_mark (method_);
+    return instance_;
+  }
+};
+
 #endif