Research portable Memory game | Исследовать портируемую игру Память
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

67 řádky
1.3KB

  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. std::list<T *(T)> functions;
  9. bool isProcessingQueue = false;
  10. std::list<T> queue;
  11. ctx_Controller(const T &c) {
  12. context = c;
  13. }
  14. void executeFunctions() {
  15. T c = queue.front();
  16. queue.pop_front();
  17. for (int i = 0; i < functions.size(); ++i) {
  18. T ctx = functions[i](c);
  19. if (ctx.recentField != "none") {
  20. queue.push_back(ctx);
  21. }
  22. }
  23. context = c;
  24. reportContext();
  25. }
  26. void processQueue() {
  27. // Decline recursion.
  28. if (isProcessingQueue) {
  29. return;
  30. }
  31. isProcessingQueue = true;
  32. while (!queue.empty()) {
  33. executeFunctions();
  34. }
  35. isProcessingQueue = false;
  36. }
  37. void registerCallback(void (*cb)(T)) {
  38. callbacks.push_back(cb);
  39. }
  40. void registerFunction(T (*f)(T)) {
  41. functions.push_back(f);
  42. }
  43. void reportContext() {
  44. for (int i = 0; i < callbacks.size(); ++i) {
  45. callbacks[i](context);
  46. }
  47. }
  48. template <typename U> void set(const std::string &fieldName, const U &value) {
  49. T c = context;
  50. c.setField(fieldName, value);
  51. c.recentField = fieldName;
  52. queue.push_back(c);
  53. processQueue();
  54. }
  55. };
  56. #endif // ctx_HEADER