This commit is contained in:
Михаил Капелько
2024-06-07 22:04:18 +03:00
parent 9acfa6592b
commit 37f6f30680
5 changed files with 36 additions and 5 deletions

View File

@@ -6,7 +6,7 @@
#define ctx_HEADER #define ctx_HEADER
template <class T> class ctx_Controller { template <class T> class ctx_Controller {
std::list<std::function<T(T)> > callbacks; std::list<std::function<void(T)> > callbacks;
std::list<std::function<T(T)> > functions; std::list<std::function<T(T)> > functions;
std::queue<T> queue; std::queue<T> queue;
@@ -49,12 +49,14 @@ template <class T> class ctx_Controller {
functions.push_back(f); functions.push_back(f);
} }
/* void registerCallback(std::function<void(T)> cb) {
void registerCallback(void (*cb)(T)) {
callbacks.push_back(cb); callbacks.push_back(cb);
} }
void registerFieldCallback(const std::string &fieldName, void (*cb)(T)) { void registerFieldCallback(
const std::string &fieldName,
std::function<void(T)> cb
) {
auto execCB = [fieldName, cb](T c) { auto execCB = [fieldName, cb](T c) {
if (c.recentField == fieldName) { if (c.recentField == fieldName) {
cb(c); cb(c);
@@ -63,6 +65,7 @@ template <class T> class ctx_Controller {
callbacks.push_back(execCB); callbacks.push_back(execCB);
} }
/*
void registerFunctions(const std::list<T *(T)> funcs) { void registerFunctions(const std::list<T *(T)> funcs) {
for (const auto &f : funcs) { for (const auto &f : funcs) {
functions.push_back(f); functions.push_back(f);

View File

@@ -63,3 +63,28 @@ std::string test_ctx_Controller_processQueue() {
return "ERR: ctx_Controller_processQueue"; return "ERR: ctx_Controller_processQueue";
} }
std::string test_ctx_Controller_registerFieldCallback_match() {
auto c = memory_createContext();
ctx_Controller<memory_Context> ctrl(c);
c.input = "123";
c.recentField = "input";
std::string callbackInput = "";
auto setCallbackInput = [&callbackInput](memory_Context c) {
if (
c.recentField == "input"
) {
callbackInput = c.input;
}
};
ctrl.registerFieldCallback("input", setCallbackInput);
ctrl.reportContext();
if (
ctrl.context.input == callbackInput
) {
return "OK: ctx_Controller_registerFieldCallback_match";
}
return "ERR: ctx_Controller_registerFieldCallback_match";
}

View File

@@ -5,5 +5,6 @@
std::string test_ctx_Controller_executeFunctions_set(); std::string test_ctx_Controller_executeFunctions_set();
std::string test_ctx_Controller_processQueue(); std::string test_ctx_Controller_processQueue();
std::string test_ctx_Controller_registerFieldCallback_match();
#endif // ctx_test_HEADER #endif // ctx_test_HEADER

View File

@@ -87,7 +87,7 @@ def ctx_test_Controller_registerFieldCallback_match(
): ):
return "OK: ctx_Controller_registerFieldCallback_match" return "OK: ctx_Controller_registerFieldCallback_match"
#} #}
return f"ERR: ctx_Controller_registerFieldCallback_match" return "ERR: ctx_Controller_registerFieldCallback_match"
#} #}
def ctx_test_Controller_registerFieldCallback_mismatch( def ctx_test_Controller_registerFieldCallback_mismatch(

View File

@@ -11,6 +11,8 @@ int main() {
<< std::endl << std::endl
<< test_ctx_Controller_processQueue() << test_ctx_Controller_processQueue()
<< std::endl << std::endl
<< test_ctx_Controller_registerFieldCallback_match()
<< std::endl
<< llm_test_isDigit_digit() << llm_test_isDigit_digit()
<< std::endl << std::endl
<< llm_test_isDigit_notDigit() << llm_test_isDigit_notDigit()