@@ -1,31 +1,30 @@ | |||
#include <list> | |||
#include <queue> | |||
#include <string> | |||
#ifndef ctx_HEADER | |||
#define ctx_HEADER | |||
template <class T> class ctx_Controller { | |||
T context; | |||
std::list<void *(T)> callbacks; | |||
/* | |||
std::list<T *(T)> functions; | |||
bool isProcessingQueue = false; | |||
std::list<T> queue; | |||
*/ | |||
std::list<std::function<T(T)> > callbacks; | |||
std::list<std::function<T(T)> > functions; | |||
std::queue<T> 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 T> class ctx_Controller { | |||
isProcessingQueue = false; | |||
} | |||
void registerFunction(std::function<T(T)> f) { | |||
functions.push_back(f); | |||
} | |||
/* | |||
void registerCallback(void (*cb)(T)) { | |||
callbacks.push_back(cb); | |||
} | |||
@@ -59,30 +63,29 @@ template <class T> class ctx_Controller { | |||
callbacks.push_back(execCB); | |||
} | |||
void registerFunction(T (*f)(T)) { | |||
functions.push_back(f); | |||
} | |||
void registerFunctions(const std::list<T *(T)> funcs) { | |||
for (const auto &f : funcs) { | |||
functions.push_back(f); | |||
} | |||
} | |||
*/ | |||
void reportContext() { | |||
for (const auto &cb : callbacks) { | |||
cb(context); | |||
} | |||
} | |||
template <typename U> void set(const std::string &fieldName, const U &value) { | |||
T c = context; | |||
template <typename U> 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 |
@@ -2,15 +2,17 @@ | |||
#include "ctx.h" | |||
#include "memory_Context.h" | |||
#include <iostream> | |||
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"); | |||
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"; | |||
} |
@@ -1,24 +1,5 @@ | |||
#include "memory_Context.h" | |||
template <typename T> T memory_Context::field(const std::string &fieldName) { | |||
if (fieldName == "didLaunch") { | |||
return didLaunch; | |||
} | |||
// TODO | |||
return 0; | |||
} | |||
template <typename T> void memory_Context::setField(const std::string &fieldName, T value) { | |||
if (fieldName == "didLaunch") { | |||
didLaunch = value; | |||
} | |||
// TODO | |||
} | |||
memory_Context memory_createContext() { | |||
return memory_Context(); | |||
} |
@@ -26,8 +26,97 @@ struct memory_Context { | |||
std::vector<int> selectedItems; | |||
bool victory = false; | |||
template <typename T> T field(const std::string &fieldName); | |||
template <typename T> void setField(const std::string &fieldName, T value); | |||
template <typename T> T field(const std::string &fieldName) { | |||
if (fieldName == "didLaunch") { | |||
return *reinterpret_cast<T*>(&didLaunch); | |||
} else if (fieldName == "exit") { | |||
return *reinterpret_cast<T*>(&exit); | |||
} else if (fieldName == "input") { | |||
return *reinterpret_cast<T*>(&input); | |||
} else if (fieldName == "hiddenItems") { | |||
return *reinterpret_cast<T*>(&hiddenItems); | |||
} else if (fieldName == "mismatchedItems") { | |||
return *reinterpret_cast<T*>(&mismatchedItems); | |||
} else if (fieldName == "outputGoOn") { | |||
return *reinterpret_cast<T*>(&outputGoOn); | |||
} else if (fieldName == "outputGreeting") { | |||
return *reinterpret_cast<T*>(&outputGreeting); | |||
} else if (fieldName == "outputHelp") { | |||
return *reinterpret_cast<T*>(&outputHelp); | |||
} else if (fieldName == "outputMatchedItems") { | |||
return *reinterpret_cast<T*>(&outputMatchedItems); | |||
} else if (fieldName == "outputMismatchedItems") { | |||
return *reinterpret_cast<T*>(&outputMismatchedItems); | |||
} else if (fieldName == "outputPromptSelection") { | |||
return *reinterpret_cast<T*>(&outputPromptSelection); | |||
} else if (fieldName == "outputVictory") { | |||
return *reinterpret_cast<T*>(&outputVictory); | |||
} else if (fieldName == "playfieldItems") { | |||
return *reinterpret_cast<T*>(&playfieldItems); | |||
} else if (fieldName == "playfieldSize") { | |||
return *reinterpret_cast<T*>(&playfieldSize); | |||
} else if (fieldName == "selectedId") { | |||
return *reinterpret_cast<T*>(&selectedId); | |||
} else if (fieldName == "selectedItems") { | |||
return *reinterpret_cast<T*>(&selectedItems); | |||
} | |||
// victory | |||
return *reinterpret_cast<T*>(&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<int> &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<int, int> &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(); | |||