From e9e0073061d531de293cd60b6f802f24c8fb742c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 2 Jun 2024 22:31:38 +0300 Subject: [PATCH] d --- v4/ctx.h | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/v4/ctx.h b/v4/ctx.h index 019dbe7..087faa6 100644 --- a/v4/ctx.h +++ b/v4/ctx.h @@ -18,13 +18,15 @@ template class ctx_Controller { void executeFunctions() { T c = queue.front(); queue.pop_front(); - for (int i = 0; i < functions.size(); ++i) { - T ctx = functions[i](c); + for (const auto &f : functions) { + T ctx = f(c); if (ctx.recentField != "none") { queue.push_back(ctx); } } - context = c; + + context.recentField = c.recentField; + context.setField(c.recentField, c.field(c.recentField)); reportContext(); } @@ -44,13 +46,28 @@ template class ctx_Controller { callbacks.push_back(cb); } + void registerFieldCallback(const std::string &fieldName, void (*cb)(T)) { + auto execCB = [fieldName, cb](T c) { + if (c.recentField == fieldName) { + cb(c); + } + }; + callbacks.push_back(execCB); + } + void registerFunction(T (*f)(T)) { functions.push_back(f); } + void registerFunctions(const std::list funcs) { + for (const auto &f : funcs) { + functions.push_back(f); + } + } + void reportContext() { - for (int i = 0; i < callbacks.size(); ++i) { - callbacks[i](context); + for (const auto &cb : callbacks) { + cb(context); } }