diff options
Diffstat (limited to 'core/event_queue.cpp')
-rw-r--r-- | core/event_queue.cpp | 147 |
1 files changed, 71 insertions, 76 deletions
diff --git a/core/event_queue.cpp b/core/event_queue.cpp index 587283cc79..f9ebc82e40 100644 --- a/core/event_queue.cpp +++ b/core/event_queue.cpp @@ -28,134 +28,129 @@ /*************************************************************************/ #include "event_queue.h" - -Error EventQueue::push_call(uint32_t p_instance_ID, const StringName& p_method, VARIANT_ARG_DECLARE) { - - uint8_t room_needed=sizeof(Event); - int args=0; - if (p_arg5.get_type()!=Variant::NIL) - args=5; - else if (p_arg4.get_type()!=Variant::NIL) - args=4; - else if (p_arg3.get_type()!=Variant::NIL) - args=3; - else if (p_arg2.get_type()!=Variant::NIL) - args=2; - else if (p_arg1.get_type()!=Variant::NIL) - args=1; +Error EventQueue::push_call(uint32_t p_instance_ID, const StringName &p_method, VARIANT_ARG_DECLARE) { + + uint8_t room_needed = sizeof(Event); + int args = 0; + if (p_arg5.get_type() != Variant::NIL) + args = 5; + else if (p_arg4.get_type() != Variant::NIL) + args = 4; + else if (p_arg3.get_type() != Variant::NIL) + args = 3; + else if (p_arg2.get_type() != Variant::NIL) + args = 2; + else if (p_arg1.get_type() != Variant::NIL) + args = 1; else - args=0; + args = 0; - room_needed+=sizeof(Variant)*args; + room_needed += sizeof(Variant) * args; - ERR_FAIL_COND_V( (buffer_end+room_needed) >= buffer_size , ERR_OUT_OF_MEMORY ); - Event * ev = memnew_placement( &event_buffer[ buffer_end ], Event ); - ev->args=args; - ev->instance_ID=p_instance_ID; - ev->method=p_method; + ERR_FAIL_COND_V((buffer_end + room_needed) >= buffer_size, ERR_OUT_OF_MEMORY); + Event *ev = memnew_placement(&event_buffer[buffer_end], Event); + ev->args = args; + ev->instance_ID = p_instance_ID; + ev->method = p_method; - buffer_end+=sizeof(Event); + buffer_end += sizeof(Event); - if (args>=1) { + if (args >= 1) { - Variant * v = memnew_placement( &event_buffer[ buffer_end ], Variant ); - buffer_end+=sizeof(Variant); - *v=p_arg1; + Variant *v = memnew_placement(&event_buffer[buffer_end], Variant); + buffer_end += sizeof(Variant); + *v = p_arg1; } - if (args>=2) { + if (args >= 2) { - Variant * v = memnew_placement( &event_buffer[ buffer_end ], Variant ); - buffer_end+=sizeof(Variant); - *v=p_arg2; + Variant *v = memnew_placement(&event_buffer[buffer_end], Variant); + buffer_end += sizeof(Variant); + *v = p_arg2; } - if (args>=3) { - - Variant * v = memnew_placement( &event_buffer[ buffer_end ], Variant ); - buffer_end+=sizeof(Variant); - *v=p_arg3; + if (args >= 3) { + Variant *v = memnew_placement(&event_buffer[buffer_end], Variant); + buffer_end += sizeof(Variant); + *v = p_arg3; } - if (args>=4) { + if (args >= 4) { - Variant * v = memnew_placement( &event_buffer[ buffer_end ], Variant ); - buffer_end+=sizeof(Variant); - *v=p_arg4; + Variant *v = memnew_placement(&event_buffer[buffer_end], Variant); + buffer_end += sizeof(Variant); + *v = p_arg4; } - if (args>=5) { + if (args >= 5) { - Variant * v = memnew_placement( &event_buffer[ buffer_end ], Variant ); - buffer_end+=sizeof(Variant); - *v=p_arg5; + Variant *v = memnew_placement(&event_buffer[buffer_end], Variant); + buffer_end += sizeof(Variant); + *v = p_arg5; } if (buffer_end > buffer_max_used) - buffer_max_used=buffer_end; + buffer_max_used = buffer_end; return OK; } void EventQueue::flush_events() { - uint32_t read_pos=0; + uint32_t read_pos = 0; - while (read_pos < buffer_end ) { + while (read_pos < buffer_end) { - Event *event = (Event*)&event_buffer[ read_pos ]; - Variant *args= (Variant*)(event+1); + Event *event = (Event *)&event_buffer[read_pos]; + Variant *args = (Variant *)(event + 1); Object *obj = ObjectDB::get_instance(event->instance_ID); if (obj) { // events don't expect a return value - obj->call( event->method, - (event->args>=1) ? args[0] : Variant(), - (event->args>=2) ? args[1] : Variant(), - (event->args>=3) ? args[2] : Variant(), - (event->args>=4) ? args[3] : Variant(), - (event->args>=5) ? args[4] : Variant() ); + obj->call(event->method, + (event->args >= 1) ? args[0] : Variant(), + (event->args >= 2) ? args[1] : Variant(), + (event->args >= 3) ? args[2] : Variant(), + (event->args >= 4) ? args[3] : Variant(), + (event->args >= 5) ? args[4] : Variant()); } - if (event->args>=1) args[0].~Variant(); - if (event->args>=2) args[1].~Variant(); - if (event->args>=3) args[2].~Variant(); - if (event->args>=4) args[3].~Variant(); - if (event->args>=5) args[4].~Variant(); + if (event->args >= 1) args[0].~Variant(); + if (event->args >= 2) args[1].~Variant(); + if (event->args >= 3) args[2].~Variant(); + if (event->args >= 4) args[3].~Variant(); + if (event->args >= 5) args[4].~Variant(); event->~Event(); - read_pos+=sizeof(Event)+sizeof(Variant)*event->args; + read_pos += sizeof(Event) + sizeof(Variant) * event->args; } - buffer_end=0; // reset buffer + buffer_end = 0; // reset buffer } EventQueue::EventQueue(uint32_t p_buffer_size) { - - buffer_end=0; - buffer_max_used=0; - buffer_size=p_buffer_size; - event_buffer = memnew_arr( uint8_t, buffer_size ); - + buffer_end = 0; + buffer_max_used = 0; + buffer_size = p_buffer_size; + event_buffer = memnew_arr(uint8_t, buffer_size); } EventQueue::~EventQueue() { - uint32_t read_pos=0; + uint32_t read_pos = 0; - while (read_pos < buffer_end ) { + while (read_pos < buffer_end) { - Event *event = (Event*)&event_buffer[ read_pos ]; - Variant *args= (Variant*)(event+1); - for (int i=0;i<event->args;i++) + Event *event = (Event *)&event_buffer[read_pos]; + Variant *args = (Variant *)(event + 1); + for (int i = 0; i < event->args; i++) args[i].~Variant(); event->~Event(); - read_pos+=sizeof(Event)+sizeof(Variant)*event->args; + read_pos += sizeof(Event) + sizeof(Variant) * event->args; } memdelete_arr(event_buffer); - event_buffer=NULL; + event_buffer = NULL; } - |