diff --git a/python/dune/perftool/ufl/delegate.py b/python/dune/perftool/ufl/delegate.py new file mode 100644 index 0000000000000000000000000000000000000000..09a6fc44acf4953b2d3bdf5f22417ab8871754bd --- /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