A 2D tile-based sandbox game.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #pragma once
  2. #include <optional>
  3. #include <functional>
  4. #include <memory>
  5. #include <chrono>
  6. #include <type_traits>
  7. #include <stddef.h>
  8. namespace Swan {
  9. // Inherit from this class to make a class non-copyable
  10. class NonCopyable {
  11. protected:
  12. NonCopyable() = default;
  13. NonCopyable(NonCopyable &&) = default;
  14. NonCopyable(const NonCopyable &) = delete;
  15. NonCopyable &operator=(const NonCopyable &) = delete;
  16. };
  17. template<typename T, typename Del = void (*)(T *)>
  18. using RaiiPtr = std::unique_ptr<T, Del>;
  19. template<typename T, typename Del>
  20. RaiiPtr<T, Del> makeRaiiPtr(T *val, Del d) {
  21. return std::unique_ptr<T, Del>(val, d);
  22. }
  23. template<typename Func>
  24. class Deferred: NonCopyable {
  25. public:
  26. Deferred(Func func): func_(func) {}
  27. Deferred(Deferred &&def) noexcept: func_(def.func_) { def.active_ = false; }
  28. ~Deferred() { if (active_) func_(); }
  29. Deferred &operator=(Deferred &&def) noexcept {
  30. func_ = def.func_;
  31. def.active_ = false;
  32. return *this;
  33. }
  34. private:
  35. Func func_;
  36. bool active_ = true;
  37. };
  38. template<typename Func>
  39. Deferred<Func> makeDeferred(Func func) {
  40. return Deferred(func);
  41. }
  42. // Ret can't be a reference, because C++ doesn't support optional<T&>.
  43. template<typename Ret, typename Func = std::function<std::optional<Ret>()>>
  44. class Iter {
  45. public:
  46. class It {
  47. public:
  48. It(std::optional<Ret> next, Func &func): next_(std::move(next)), func_(func) {}
  49. bool operator==(const It &other) {
  50. return next_ == std::nullopt && other.next_ == std::nullopt;
  51. }
  52. bool operator!=(const It &other) {
  53. return !(*this == other);
  54. }
  55. void operator++() {
  56. next_ = std::move(func_());
  57. }
  58. Ret operator*() {
  59. return std::move(*next_);
  60. }
  61. private:
  62. std::optional<Ret> next_;
  63. Func &func_;
  64. };
  65. Iter(Func func): func_(func) {}
  66. operator Iter<Ret, std::function<std::optional<Ret>()>>() {
  67. return Iter<Ret, std::function<std::optional<Ret>()>>(func_);
  68. }
  69. It begin() {
  70. return It(func_(), func_);
  71. }
  72. It end() {
  73. return It(std::nullopt, func_);
  74. }
  75. private:
  76. Func func_;
  77. };
  78. template<typename InputIterator, typename Func>
  79. auto map(InputIterator first, InputIterator last, Func func) {
  80. using RetT = decltype(func(*first));
  81. auto l = [=]() mutable -> std::optional<RetT> {
  82. if (first == last)
  83. return std::nullopt;
  84. RetT r = func(*first);
  85. ++first;
  86. return r;
  87. };
  88. return Iter<RetT, decltype(l)>(l);
  89. }
  90. template<typename InputIterator, typename Func>
  91. auto filter(InputIterator first, InputIterator last, Func pred) {
  92. using RetT = std::remove_reference_t<decltype(*first)>;
  93. auto l = [=]() mutable -> std::optional<RetT> {
  94. if (first == last)
  95. return std::nullopt;
  96. while (!pred(*first)) {
  97. ++first;
  98. if (first == last)
  99. return std::nullopt;
  100. }
  101. RetT r = *first;
  102. ++first;
  103. return r;
  104. };
  105. return Iter<RetT, decltype(l)>(l);
  106. }
  107. template<typename InputIterator, typename Func>
  108. auto mapFilter(InputIterator first, InputIterator last, Func func) {
  109. using RetT = std::remove_reference_t<decltype(*func(*first))>;
  110. auto l = [=]() mutable -> std::optional<RetT> {
  111. if (first == last)
  112. return std::nullopt;
  113. std::optional<RetT> r;
  114. while ((r = func(*first)) == std::nullopt) {
  115. ++first;
  116. if (first == last)
  117. return std::nullopt;
  118. }
  119. ++first;
  120. return r;
  121. };
  122. return Iter<RetT, decltype(l)>(l);
  123. }
  124. }