diff --git a/v4/ctx.h b/v4/ctx.h index 36d70db..a6f27cd 100644 --- a/v4/ctx.h +++ b/v4/ctx.h @@ -1,31 +1,30 @@ #include +#include #include #ifndef ctx_HEADER #define ctx_HEADER template class ctx_Controller { - T context; - std::list callbacks; - /* - std::list functions; - bool isProcessingQueue = false; - std::list queue; - */ + std::list > callbacks; + std::list > functions; + std::queue queue; public: + T context; + bool isProcessingQueue = false; + ctx_Controller(const T &c) { context = c; } - /* void executeFunctions() { - T c = queue.front(); - queue.pop_front(); + auto c = queue.front(); + queue.pop(); for (const auto &f : functions) { - T ctx = f(c); + auto ctx = f(c); if (ctx.recentField != "none") { - queue.push_back(ctx); + queue.push(ctx); } } @@ -46,6 +45,11 @@ template class ctx_Controller { isProcessingQueue = false; } + void registerFunction(std::function f) { + functions.push_back(f); + } + + /* void registerCallback(void (*cb)(T)) { callbacks.push_back(cb); } @@ -59,30 +63,29 @@ template class ctx_Controller { 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); } } - template void set(const std::string &fieldName, const U &value) { - T c = context; + template void set( + const std::string &fieldName, + const U &value + ) { + auto c = context; c.setField(fieldName, value); c.recentField = fieldName; - queue.push_back(c); + queue.push(c); processQueue(); } - */ }; #endif // ctx_HEADER diff --git a/v4/ctx_test2.cpp b/v4/ctx_test2.cpp index 1ca011b..e22c7bc 100644 --- a/v4/ctx_test2.cpp +++ b/v4/ctx_test2.cpp @@ -2,15 +2,17 @@ #include "ctx.h" #include "memory_Context.h" +#include + 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"); + std::cout << "input: '" << ctrl.context.input << "'" << std::endl; - auto processInput = [](const memory_Context &c) -> memory_Context { + auto processInput = [](memory_Context c) -> memory_Context { if (c.recentField == "input") { c.outputHelp = "Checked"; c.recentField = "outputHelp"; @@ -25,12 +27,12 @@ std::string ctx_test_Controller_executeFunctions_set() { ctrl.executeFunctions(); // Apply 'outputHelp'. ctrl.executeFunctions(); + std::cout << "input: '" << ctrl.context.input << "'" << std::endl; if ( - c.input == "123" && - c.outputHelp == "Checked" + ctrl.context.input == "123" && + ctrl.context.outputHelp == "Checked" ) { return "OK: ctx_Controller_executeFunctions_set"; } - */ return "ERR: ctx_Controller_executeFunctions_set"; } diff --git a/v4/memory_Context.cpp b/v4/memory_Context.cpp index cd3ce1c..d21fb82 100644 --- a/v4/memory_Context.cpp +++ b/v4/memory_Context.cpp @@ -1,24 +1,5 @@ #include "memory_Context.h" - -template T memory_Context::field(const std::string &fieldName) { - if (fieldName == "didLaunch") { - return didLaunch; - } - - // TODO - - return 0; -} - -template void memory_Context::setField(const std::string &fieldName, T value) { - if (fieldName == "didLaunch") { - didLaunch = value; - } - - // TODO -} - memory_Context memory_createContext() { return memory_Context(); } diff --git a/v4/memory_Context.h b/v4/memory_Context.h index ea99ff3..b2d551d 100644 --- a/v4/memory_Context.h +++ b/v4/memory_Context.h @@ -26,8 +26,97 @@ struct memory_Context { std::vector selectedItems; bool victory = false; - template T field(const std::string &fieldName); - template void setField(const std::string &fieldName, T value); + template T field(const std::string &fieldName) { + if (fieldName == "didLaunch") { + return *reinterpret_cast(&didLaunch); + } else if (fieldName == "exit") { + return *reinterpret_cast(&exit); + } else if (fieldName == "input") { + return *reinterpret_cast(&input); + } else if (fieldName == "hiddenItems") { + return *reinterpret_cast(&hiddenItems); + } else if (fieldName == "mismatchedItems") { + return *reinterpret_cast(&mismatchedItems); + } else if (fieldName == "outputGoOn") { + return *reinterpret_cast(&outputGoOn); + } else if (fieldName == "outputGreeting") { + return *reinterpret_cast(&outputGreeting); + } else if (fieldName == "outputHelp") { + return *reinterpret_cast(&outputHelp); + } else if (fieldName == "outputMatchedItems") { + return *reinterpret_cast(&outputMatchedItems); + } else if (fieldName == "outputMismatchedItems") { + return *reinterpret_cast(&outputMismatchedItems); + } else if (fieldName == "outputPromptSelection") { + return *reinterpret_cast(&outputPromptSelection); + } else if (fieldName == "outputVictory") { + return *reinterpret_cast(&outputVictory); + } else if (fieldName == "playfieldItems") { + return *reinterpret_cast(&playfieldItems); + } else if (fieldName == "playfieldSize") { + return *reinterpret_cast(&playfieldSize); + } else if (fieldName == "selectedId") { + return *reinterpret_cast(&selectedId); + } else if (fieldName == "selectedItems") { + return *reinterpret_cast(&selectedItems); + } + // victory + return *reinterpret_cast(&victory); + } + + void setField(const std::string &fieldName, bool value) { + if (fieldName == "didLaunch") { + didLaunch = value; + } else if (fieldName == "exit") { + exit = value; + } else if (fieldName == "victory") { + victory = value; + } + } + + void setField(const std::string &fieldName, const std::string &value) { + if (fieldName == "input") { + input = value; + } else if (fieldName == "outputGoOn") { + outputGoOn = value; + } else if (fieldName == "outputGreeting") { + outputGreeting = value; + } else if (fieldName == "outputHelp") { + outputHelp = value; + } else if (fieldName == "outputMatchedItems") { + outputMatchedItems = value; + } else if (fieldName == "outputMismatchedItems") { + outputMismatchedItems = value; + } else if (fieldName == "outputPromptSelection") { + outputPromptSelection = value; + } else if (fieldName == "outputVictory") { + outputVictory = value; + } + } + + void setField(const std::string &fieldName, const std::vector &value) { + if (fieldName == "hiddenItems") { + hiddenItems = value; + } else if (fieldName == "mismatchedItems") { + mismatchedItems = value; + } else if (fieldName == "selectedItems") { + selectedItems = value; + } + } + + void setField(const std::string &fieldName, const std::map &value) { + if (fieldName == "playfieldItems") { + playfieldItems = value; + } + } + + void setField(const std::string &fieldName, int value) { + if (fieldName == "playfieldSize") { + playfieldSize = value; + } else if (fieldName == "selectedId") { + selectedId = value; + } + } }; memory_Context memory_createContext();