From 616bf544f9305d2f9a743a4c6f0ee8ab7c8bd2f9 Mon Sep 17 00:00:00 2001 From: Dominic Kempf <dominic.kempf@iwr.uni-heidelberg.de> Date: Wed, 17 Feb 2016 16:58:40 +0100 Subject: [PATCH] Add a delegating multifunction pattern --- python/dune/perftool/ufl/delegate.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 python/dune/perftool/ufl/delegate.py diff --git a/python/dune/perftool/ufl/delegate.py b/python/dune/perftool/ufl/delegate.py new file mode 100644 index 00000000..09a6fc44 --- /dev/null +++ b/python/dune/perftool/ufl/delegate.py @@ -0,0 +1,23 @@ +""" Implement a design pattern for a multifunction that delegates to another +multifunction and gets back control if the delegate does not define a handler. +This avoids writing isinstance-if-orgies in handlers +""" + + +def delegate(Delegate, *args, **kwargs): + assert(isinstance(Delegate, type)) + assert(Delegate.expr == Delegate.undefined) + + class MyDelegate(Delegate): + def __init__(self, *a, **ka): + Delegate.__init__(self, *a, **ka) + + def expr(s, *a, **ka): + s._back(*a, **ka) + + def _handler(s, *a, **ka): + delegate_instance = MyDelegate(*args, **kwargs) + delegate_instance._back = s + return delegate_instance(*a, **ka) + + return _handler -- GitLab