event_queue.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*************************************************************************/
  2. /* event_queue.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "event_queue.h"
  31. Error EventQueue::push_call(uint32_t p_instance_ID, const StringName &p_method, VARIANT_ARG_DECLARE) {
  32. uint8_t room_needed = sizeof(Event);
  33. int args = 0;
  34. if (p_arg5.get_type() != Variant::NIL)
  35. args = 5;
  36. else if (p_arg4.get_type() != Variant::NIL)
  37. args = 4;
  38. else if (p_arg3.get_type() != Variant::NIL)
  39. args = 3;
  40. else if (p_arg2.get_type() != Variant::NIL)
  41. args = 2;
  42. else if (p_arg1.get_type() != Variant::NIL)
  43. args = 1;
  44. else
  45. args = 0;
  46. room_needed += sizeof(Variant) * args;
  47. ERR_FAIL_COND_V((buffer_end + room_needed) >= buffer_size, ERR_OUT_OF_MEMORY);
  48. Event *ev = memnew_placement(&event_buffer[buffer_end], Event);
  49. ev->args = args;
  50. ev->instance_ID = p_instance_ID;
  51. ev->method = p_method;
  52. buffer_end += sizeof(Event);
  53. if (args >= 1) {
  54. Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
  55. buffer_end += sizeof(Variant);
  56. *v = p_arg1;
  57. }
  58. if (args >= 2) {
  59. Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
  60. buffer_end += sizeof(Variant);
  61. *v = p_arg2;
  62. }
  63. if (args >= 3) {
  64. Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
  65. buffer_end += sizeof(Variant);
  66. *v = p_arg3;
  67. }
  68. if (args >= 4) {
  69. Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
  70. buffer_end += sizeof(Variant);
  71. *v = p_arg4;
  72. }
  73. if (args >= 5) {
  74. Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
  75. buffer_end += sizeof(Variant);
  76. *v = p_arg5;
  77. }
  78. if (buffer_end > buffer_max_used)
  79. buffer_max_used = buffer_end;
  80. return OK;
  81. }
  82. void EventQueue::flush_events() {
  83. uint32_t read_pos = 0;
  84. while (read_pos < buffer_end) {
  85. Event *event = (Event *)&event_buffer[read_pos];
  86. Variant *args = (Variant *)(event + 1);
  87. Object *obj = ObjectDB::get_instance(event->instance_ID);
  88. if (obj) {
  89. // events don't expect a return value
  90. obj->call(event->method,
  91. (event->args >= 1) ? args[0] : Variant(),
  92. (event->args >= 2) ? args[1] : Variant(),
  93. (event->args >= 3) ? args[2] : Variant(),
  94. (event->args >= 4) ? args[3] : Variant(),
  95. (event->args >= 5) ? args[4] : Variant());
  96. }
  97. if (event->args >= 1) args[0].~Variant();
  98. if (event->args >= 2) args[1].~Variant();
  99. if (event->args >= 3) args[2].~Variant();
  100. if (event->args >= 4) args[3].~Variant();
  101. if (event->args >= 5) args[4].~Variant();
  102. event->~Event();
  103. read_pos += sizeof(Event) + sizeof(Variant) * event->args;
  104. }
  105. buffer_end = 0; // reset buffer
  106. }
  107. EventQueue::EventQueue(uint32_t p_buffer_size) {
  108. buffer_end = 0;
  109. buffer_max_used = 0;
  110. buffer_size = p_buffer_size;
  111. event_buffer = memnew_arr(uint8_t, buffer_size);
  112. }
  113. EventQueue::~EventQueue() {
  114. uint32_t read_pos = 0;
  115. while (read_pos < buffer_end) {
  116. Event *event = (Event *)&event_buffer[read_pos];
  117. Variant *args = (Variant *)(event + 1);
  118. for (int i = 0; i < event->args; i++)
  119. args[i].~Variant();
  120. event->~Event();
  121. read_pos += sizeof(Event) + sizeof(Variant) * event->args;
  122. }
  123. memdelete_arr(event_buffer);
  124. event_buffer = NULL;
  125. }