From 0d84b677f16e2cb5491b65a48762c720abb7e2be Mon Sep 17 00:00:00 2001
From: Dominic Kempf <dominic.kempf@iwr.uni-heidelberg.de>
Date: Mon, 22 Oct 2018 16:58:10 +0200
Subject: [PATCH] Add a first implementation of hooks

---
 python/dune/perftool/generation/__init__.py  |  4 ++++
 python/dune/perftool/generation/hooks.py     | 16 ++++++++++++++++
 python/dune/perftool/pdelab/localoperator.py |  3 +++
 3 files changed, 23 insertions(+)
 create mode 100644 python/dune/perftool/generation/hooks.py

diff --git a/python/dune/perftool/generation/__init__.py b/python/dune/perftool/generation/__init__.py
index e541e713..d2712471 100644
--- a/python/dune/perftool/generation/__init__.py
+++ b/python/dune/perftool/generation/__init__.py
@@ -31,6 +31,10 @@ from dune.perftool.generation.cpp import (base_class,
                                           template_parameter,
                                           )
 
+from dune.perftool.generation.hooks import (register_hook,
+                                            run_hook,
+                                            )
+
 from dune.perftool.generation.loopy import (barrier,
                                             constantarg,
                                             domain,
diff --git a/python/dune/perftool/generation/hooks.py b/python/dune/perftool/generation/hooks.py
new file mode 100644
index 00000000..bd3fc0a1
--- /dev/null
+++ b/python/dune/perftool/generation/hooks.py
@@ -0,0 +1,16 @@
+""" All the infrastructure code related to adding hooks to the code generation process """
+
+
+_hooks = {}
+
+
+def register_hook(hookname, func):
+    current = _hooks.setdefault(hookname, ())
+    current = list(current)
+    current.append(func)
+    _hooks[hookname] = tuple(current)
+
+
+def run_hook(hookname, *args, **kwargs):
+    for hook in _hooks[hookname]:
+        hook(*args, **kwargs)
diff --git a/python/dune/perftool/pdelab/localoperator.py b/python/dune/perftool/pdelab/localoperator.py
index 1f808ef1..f5ac0b8d 100644
--- a/python/dune/perftool/pdelab/localoperator.py
+++ b/python/dune/perftool/pdelab/localoperator.py
@@ -28,6 +28,7 @@ from dune.perftool.generation import (backend,
                                       post_include,
                                       retrieve_cache_functions,
                                       retrieve_cache_items,
+                                      run_hook,
                                       template_parameter,
                                       )
 from dune.perftool.cgen.clazz import (AccessModifier,
@@ -452,6 +453,8 @@ def visit_integral(integral):
     visitor = get_visitor(measure, subdomain_id)
     visitor.accumulate(integrand)
 
+    run_hook("after_visit", visitor)
+
 
 def generate_kernel(integrals):
     logger = logging.getLogger(__name__)
-- 
GitLab