delegate.H 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. * */
  13. #ifndef DELEGATE_H
  14. #define DELEGATE_H
  15. #include <functional>
  16. #ifndef NULL
  17. #define NULL ((void*)0)
  18. #endif
  19. struct GenericDelegate
  20. {
  21. void* func;
  22. void* data;
  23. template<class R, class... P> R operator()(P && ...p) {
  24. return ((R (*)(void* data, P...))func)(data,std::forward<P>(p)...);
  25. }
  26. void* operator=(void* func) {
  27. return this->func=func;
  28. }
  29. };
  30. template<class SIGNATURE> struct DelegateBase;
  31. template<class R, class... P>
  32. struct DelegateBase<R(P...)>:public GenericDelegate
  33. {
  34. /*R (*func)(void* data, P...);
  35. void* data;*/
  36. template<class... P2>
  37. R operator() (P2 && ...p) const {
  38. return ((R (*)(void* data, P...))func)(data,std::forward<P2>(p)...);
  39. }
  40. R (*operator=(R (*func)(void* data, P...)))(void* data, P...) {
  41. this->func=(void*)func;
  42. return func;
  43. }
  44. bool operator==(void* other) {
  45. return (void*)func == other;
  46. }
  47. bool operator==(const DelegateBase<R(P...)>& other) {
  48. return func == other.func && data==other.data;
  49. }
  50. bool operator!=(void* other) {
  51. return (void*)func != other;
  52. }
  53. bool operator!=(const DelegateBase<R(P...)>& other) {
  54. return func != other.func && data==other.data;
  55. }
  56. };
  57. template<class SIGNATURE> struct Delegate;
  58. template<class R, class... P>
  59. struct Delegate<R(P...)>:public DelegateBase<R(P...)>
  60. {
  61. Delegate() {
  62. this->func=NULL;
  63. }
  64. Delegate(R (*func)(void* data, P... p...), void* data): DelegateBase<R(P...)>(func,data) {}
  65. template<class X>Delegate(R (*func)(X*,P...), X* th) {
  66. this->func=(void*)(R(*)(void*,P...))func;
  67. this->data=th;
  68. }
  69. template<class X>Delegate(R (X::*func)(P...), X* th) {
  70. this->func=(void*)(R(*)(void*,P...))func;
  71. this->data=th;
  72. }
  73. Delegate(R (*func)(void*, P...)) {
  74. this->func=(void*)func;
  75. this->data=NULL;
  76. }
  77. explicit Delegate(const GenericDelegate& other) {
  78. this->func=other.func;
  79. this->data=other.data;
  80. }
  81. template<class X>Delegate(X* th) {
  82. this->func=(void*)(R(*)(void*,P...))&X::operator();
  83. this->data=th;
  84. }
  85. Delegate(std::nullptr_t n) {
  86. this->func=NULL;
  87. };
  88. R (*operator=(R (*func)(void* data, P...)))(void* data, P...) {
  89. this->func=(void*)func;
  90. return func;
  91. }
  92. };
  93. template<class R, class... P> static Delegate<R(P...)> nullDelegate() {
  94. struct {
  95. R operator()(P... p...) {
  96. return R();
  97. }
  98. } tmp;
  99. return &tmp;
  100. }
  101. template<class SIGNATURE> class DelegateChain;
  102. template<class R, class ... P>
  103. class DelegateChain<R(P...)>
  104. {
  105. public:
  106. struct item
  107. {
  108. Delegate<R(P...)> func;
  109. item* prev;
  110. item* next;
  111. template<class ... P2>
  112. inline R operator()(P2 && ...p) const {
  113. func(std::forward<P2>(p)...);
  114. }
  115. };
  116. item* last = NULL;
  117. DelegateChain() {
  118. }
  119. item* attach(Delegate<R(P...)> func) {
  120. item* it = new item { func, last, NULL };
  121. if (last != NULL) last->next = it;
  122. last = it;
  123. return it;
  124. }
  125. void detach(item* it) {
  126. if (it->next != NULL) it->next->prev = it->prev;
  127. if (it->prev != NULL) it->prev->next = it->next;
  128. if (last == it) last = it->prev;
  129. delete it;
  130. }
  131. inline R operator()(P... p) const {
  132. if (last != NULL) (*last)(std::forward<P>(p)...);
  133. }
  134. };
  135. #endif