Research portable Memory game | Исследовать портируемую игру Память
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

4 місяці тому
4 місяці тому
4 місяці тому
4 місяці тому
4 місяці тому
4 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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