message_queue.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* message_queue.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. #ifndef MESSAGE_QUEUE_H
  30. #define MESSAGE_QUEUE_H
  31. #include "object.h"
  32. #include "os/mutex.h"
  33. #include "os/thread_safe.h"
  34. class MessageQueue {
  35. _THREAD_SAFE_CLASS_
  36. enum {
  37. DEFAULT_QUEUE_SIZE_KB=1024
  38. };
  39. Mutex *mutex;
  40. enum {
  41. TYPE_CALL,
  42. TYPE_NOTIFICATION,
  43. TYPE_SET,
  44. FLAG_SHOW_ERROR=1<<14,
  45. FLAG_MASK=FLAG_SHOW_ERROR-1
  46. };
  47. struct Message {
  48. ObjectID instance_ID;
  49. StringName target;
  50. int16_t type;
  51. union {
  52. int16_t notification;
  53. int16_t args;
  54. };
  55. };
  56. uint8_t *buffer;
  57. uint32_t buffer_end;
  58. uint32_t buffer_max_used;
  59. uint32_t buffer_size;
  60. void _call_function(Object* p_target,const StringName& p_func,const Variant *p_args,int p_argcount,bool p_show_error);
  61. static MessageQueue *singleton;
  62. public:
  63. static MessageQueue *get_singleton();
  64. Error push_call(ObjectID p_id,const StringName& p_method,const Variant** p_args,int p_argcount,bool p_show_error=false);
  65. Error push_call(ObjectID p_id, const StringName& p_method, VARIANT_ARG_LIST);
  66. Error push_notification(ObjectID p_id, int p_notification);
  67. Error push_set(ObjectID p_id, const StringName& p_prop, const Variant& p_value);
  68. Error push_call(Object *p_object, const StringName& p_method, VARIANT_ARG_LIST);
  69. Error push_notification(Object *p_object, int p_notification);
  70. Error push_set(Object *p_object, const StringName& p_prop, const Variant& p_value);
  71. bool print();
  72. void statistics();
  73. void flush();
  74. int get_max_buffer_usage() const;
  75. MessageQueue();
  76. ~MessageQueue();
  77. };
  78. #endif // MESSAGE_QUEUE_H