Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.8KB

  1. #include <list>
  2. #include <string>
  3. #ifndef ctx_HEADER
  4. #define ctx_HEADER
  5. template <class T> class ctx_Controller {
  6. T context;
  7. std::list<void *(T)> callbacks;
  8. /*
  9. std::list<T *(T)> functions;
  10. bool isProcessingQueue = false;
  11. std::list<T> queue;
  12. */
  13. public:
  14. ctx_Controller(const T &c) {
  15. context = c;
  16. }
  17. /*
  18. void executeFunctions() {
  19. T c = queue.front();
  20. queue.pop_front();
  21. for (const auto &f : functions) {
  22. T ctx = f(c);
  23. if (ctx.recentField != "none") {
  24. queue.push_back(ctx);
  25. }
  26. }
  27. context.recentField = c.recentField;
  28. context.setField(c.recentField, c.field(c.recentField));
  29. reportContext();
  30. }
  31. void processQueue() {
  32. // Decline recursion.
  33. if (isProcessingQueue) {
  34. return;
  35. }
  36. isProcessingQueue = true;
  37. while (!queue.empty()) {
  38. executeFunctions();
  39. }
  40. isProcessingQueue = false;
  41. }
  42. void registerCallback(void (*cb)(T)) {
  43. callbacks.push_back(cb);
  44. }
  45. void registerFieldCallback(const std::string &fieldName, void (*cb)(T)) {
  46. auto execCB = [fieldName, cb](T c) {
  47. if (c.recentField == fieldName) {
  48. cb(c);
  49. }
  50. };
  51. callbacks.push_back(execCB);
  52. }
  53. void registerFunction(T (*f)(T)) {
  54. functions.push_back(f);
  55. }
  56. void registerFunctions(const std::list<T *(T)> funcs) {
  57. for (const auto &f : funcs) {
  58. functions.push_back(f);
  59. }
  60. }
  61. void reportContext() {
  62. for (const auto &cb : callbacks) {
  63. cb(context);
  64. }
  65. }
  66. template <typename U> void set(const std::string &fieldName, const U &value) {
  67. T c = context;
  68. c.setField(fieldName, value);
  69. c.recentField = fieldName;
  70. queue.push_back(c);
  71. processQueue();
  72. }
  73. */
  74. };
  75. #endif // ctx_HEADER