This commit is contained in:
Михаил Капелько
2024-06-02 23:52:12 +03:00
parent 17a2615351
commit 49919cfea2
4 changed files with 107 additions and 55 deletions

115
v4/ctx.h
View File

@@ -7,77 +7,82 @@
template <class T> class ctx_Controller { template <class T> class ctx_Controller {
T context; T context;
std::list<void *(T)> callbacks; std::list<void *(T)> callbacks;
/*
std::list<T *(T)> functions; std::list<T *(T)> functions;
bool isProcessingQueue = false; bool isProcessingQueue = false;
std::list<T> queue; std::list<T> queue;
*/
ctx_Controller(const T &c) { public:
context = c; ctx_Controller(const T &c) {
} context = c;
}
void executeFunctions() { /*
T c = queue.front(); void executeFunctions() {
queue.pop_front(); T c = queue.front();
for (const auto &f : functions) { queue.pop_front();
T ctx = f(c); for (const auto &f : functions) {
if (ctx.recentField != "none") { T ctx = f(c);
queue.push_back(ctx); 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; void processQueue() {
context.setField(c.recentField, c.field(c.recentField)); // Decline recursion.
reportContext(); if (isProcessingQueue) {
} return;
void processQueue() {
// Decline recursion.
if (isProcessingQueue) {
return;
}
isProcessingQueue = true;
while (!queue.empty()) {
executeFunctions();
}
isProcessingQueue = false;
}
void registerCallback(void (*cb)(T)) {
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);
} }
}; isProcessingQueue = true;
callbacks.push_back(execCB); while (!queue.empty()) {
} executeFunctions();
}
isProcessingQueue = false;
}
void registerFunction(T (*f)(T)) { void registerCallback(void (*cb)(T)) {
functions.push_back(f); callbacks.push_back(cb);
} }
void registerFunctions(const std::list<T *(T)> funcs) { void registerFieldCallback(const std::string &fieldName, void (*cb)(T)) {
for (const auto &f : funcs) { 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); functions.push_back(f);
} }
}
void reportContext() { void registerFunctions(const std::list<T *(T)> funcs) {
for (const auto &cb : callbacks) { for (const auto &f : funcs) {
cb(context); functions.push_back(f);
}
} }
}
template <typename U> void set(const std::string &fieldName, const U &value) { void reportContext() {
T c = context; for (const auto &cb : callbacks) {
c.setField(fieldName, value); cb(context);
c.recentField = fieldName; }
queue.push_back(c); }
processQueue();
} template <typename U> 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 #endif // ctx_HEADER

36
v4/ctx_test2.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include <string>
#include "ctx.h"
#include "memory_Context.h"
std::string ctx_test_Controller_executeFunctions_set() {
auto c = memory_createContext();
ctx_Controller<memory_Context> 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";
}

8
v4/ctx_test2.h Normal file
View File

@@ -0,0 +1,8 @@
#include <string>
#ifndef ctx_test_HEADER
#define ctx_test_HEADER
std::string ctx_test_Controller_executeFunctions_set();
#endif // ctx_test_HEADER

View File

@@ -1,11 +1,14 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include "ctx_test2.h"
#include "main.h" #include "main.h"
#include "memory_Context.h" #include "memory_Context.h"
int main() { int main() {
std::cout std::cout
<< ctx_test_Controller_executeFunctions_set()
<< std::endl
<< llm_test_isDigit_digit() << llm_test_isDigit_digit()
<< std::endl << std::endl
<< llm_test_isDigit_notDigit() << llm_test_isDigit_notDigit()