diff --git a/v4/ctx.h b/v4/ctx.h index 087faa6..36d70db 100644 --- a/v4/ctx.h +++ b/v4/ctx.h @@ -7,77 +7,82 @@ template class ctx_Controller { T context; std::list callbacks; + /* std::list functions; bool isProcessingQueue = false; std::list queue; + */ - ctx_Controller(const T &c) { - context = c; - } + public: + ctx_Controller(const T &c) { + context = c; + } - void executeFunctions() { - T c = queue.front(); - queue.pop_front(); - for (const auto &f : functions) { - T ctx = f(c); - if (ctx.recentField != "none") { - queue.push_back(ctx); + /* + void executeFunctions() { + T c = queue.front(); + queue.pop_front(); + for (const auto &f : functions) { + T ctx = f(c); + if (ctx.recentField != "none") { + queue.push_back(ctx); + } } + + context.recentField = c.recentField; + context.setField(c.recentField, c.field(c.recentField)); + reportContext(); } - context.recentField = c.recentField; - context.setField(c.recentField, c.field(c.recentField)); - reportContext(); - } + void processQueue() { + // Decline recursion. + if (isProcessingQueue) { + return; + } + isProcessingQueue = true; + while (!queue.empty()) { + executeFunctions(); + } + isProcessingQueue = false; + } - void processQueue() { - // Decline recursion. - if (isProcessingQueue) { - return; + void registerCallback(void (*cb)(T)) { + callbacks.push_back(cb); } - isProcessingQueue = true; - while (!queue.empty()) { - executeFunctions(); + + 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); } - isProcessingQueue = false; - } - void registerCallback(void (*cb)(T)) { - callbacks.push_back(cb); - } + void registerFunction(T (*f)(T)) { + functions.push_back(f); + } - void registerFieldCallback(const std::string &fieldName, void (*cb)(T)) { - auto execCB = [fieldName, cb](T c) { - if (c.recentField == fieldName) { - cb(c); + void registerFunctions(const std::list funcs) { + for (const auto &f : funcs) { + functions.push_back(f); } - }; - 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 (const auto &cb : callbacks) { - cb(context); + void reportContext() { + for (const auto &cb : callbacks) { + cb(context); + } } - } - template void set(const std::string &fieldName, const U &value) { - T c = context; - c.setField(fieldName, value); - c.recentField = fieldName; - queue.push_back(c); - processQueue(); - } + template void set(const std::string &fieldName, const U &value) { + T c = context; + c.setField(fieldName, value); + c.recentField = fieldName; + queue.push_back(c); + processQueue(); + } + */ }; #endif // ctx_HEADER diff --git a/v4/ctx_test2.cpp b/v4/ctx_test2.cpp new file mode 100644 index 0000000..1ca011b --- /dev/null +++ b/v4/ctx_test2.cpp @@ -0,0 +1,36 @@ +#include +#include "ctx.h" +#include "memory_Context.h" + +std::string ctx_test_Controller_executeFunctions_set() { + auto c = memory_createContext(); + ctx_Controller ctrl(c); + /* + // Disable automatic invocation of executeFunctions. + ctrl.isProcessingQueue = true; + ctrl.set("input", "123"); + + auto processInput = [](const memory_Context &c) -> memory_Context { + if (c.recentField == "input") { + c.outputHelp = "Checked"; + c.recentField = "outputHelp"; + return c; + } + c.recentField = "none"; + return c; + }; + + ctrl.registerFunction(processInput); + // Apply 'input'. + ctrl.executeFunctions(); + // Apply 'outputHelp'. + ctrl.executeFunctions(); + if ( + c.input == "123" && + c.outputHelp == "Checked" + ) { + return "OK: ctx_Controller_executeFunctions_set"; + } + */ + return "ERR: ctx_Controller_executeFunctions_set"; +} diff --git a/v4/ctx_test2.h b/v4/ctx_test2.h new file mode 100644 index 0000000..99230ca --- /dev/null +++ b/v4/ctx_test2.h @@ -0,0 +1,8 @@ +#include + +#ifndef ctx_test_HEADER +#define ctx_test_HEADER + +std::string ctx_test_Controller_executeFunctions_set(); + +#endif // ctx_test_HEADER diff --git a/v4/main.cpp b/v4/main.cpp index 3a4c0ce..05bac98 100644 --- a/v4/main.cpp +++ b/v4/main.cpp @@ -1,11 +1,14 @@ #include #include #include +#include "ctx_test2.h" #include "main.h" #include "memory_Context.h" int main() { std::cout + << ctx_test_Controller_executeFunctions_set() + << std::endl << llm_test_isDigit_digit() << std::endl << llm_test_isDigit_notDigit()