[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/5] Add Method_instance class
From: |
David Kastrup |
Subject: |
[PATCH 3/5] Add Method_instance class |
Date: |
Tue, 3 May 2016 00:58:30 +0200 |
This is a lightweight container class combining an SCM method call
with a particular instance into a C++ callable.
---
lily/callback.cc | 1 +
lily/include/callback.hh | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+)
diff --git a/lily/callback.cc b/lily/callback.cc
index fac40a0..baab7b0 100644
--- a/lily/callback.cc
+++ b/lily/callback.cc
@@ -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;
diff --git a/lily/include/callback.hh b/lily/include/callback.hh
index 7151d04..963f4eb 100644
--- a/lily/include/callback.hh
+++ b/lily/include/callback.hh
@@ -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
--
2.7.4
- Moving a number of C++ constructs to SCM, David Kastrup, 2016/05/02
- [PATCH 3/5] Add Method_instance class,
David Kastrup <=
- [PATCH 1/5] Issue 4840: Make Translator/Performer/Engraver abstract base classes, David Kastrup, 2016/05/02
- [PATCH 4/5] Replace Engraver_dispatch_entry with Method_instance, David Kastrup, 2016/05/02
- [PATCH 2/5] Add Callback0_wrapper and Callback2_wrapper class, David Kastrup, 2016/05/02
- [PATCH 5/5] Replace Translator_method et al, David Kastrup, 2016/05/02
- Re: Moving a number of C++ constructs to SCM, David Kastrup, 2016/05/03
- Re: Moving a number of C++ constructs to SCM, James Lowe, 2016/05/04