event_queue.cpp 5.0 KB

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