message_queue.h
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 
32 #include "object.h"
33 #include "os/mutex.h"
34 #include "os/thread_safe.h"
35 class MessageQueue {
36 
37  _THREAD_SAFE_CLASS_
38 
39  enum {
40 
41  DEFAULT_QUEUE_SIZE_KB=1024
42  };
43 
44  Mutex *mutex;
45 
46  enum {
47  TYPE_CALL,
48  TYPE_NOTIFICATION,
49  TYPE_SET,
50  FLAG_SHOW_ERROR=1<<14,
51  FLAG_MASK=FLAG_SHOW_ERROR-1
52 
53  };
54 
55  struct Message {
56 
57  ObjectID instance_ID;
58  StringName target;
59  int16_t type;
60  union {
61  int16_t notification;
62  int16_t args;
63  };
64  };
65 
66  uint8_t *buffer;
67  uint32_t buffer_end;
68  uint32_t buffer_max_used;
69  uint32_t buffer_size;
70 
71  void _call_function(Object* p_target,const StringName& p_func,const Variant *p_args,int p_argcount,bool p_show_error);
72 
73  static MessageQueue *singleton;
74 public:
75 
76  static MessageQueue *get_singleton();
77 
78  Error push_call(ObjectID p_id,const StringName& p_method,const Variant** p_args,int p_argcount,bool p_show_error=false);
79  Error push_call(ObjectID p_id, const StringName& p_method, VARIANT_ARG_LIST);
80  Error push_notification(ObjectID p_id, int p_notification);
81  Error push_set(ObjectID p_id, const StringName& p_prop, const Variant& p_value);
82 
83  Error push_call(Object *p_object, const StringName& p_method, VARIANT_ARG_LIST);
84  Error push_notification(Object *p_object, int p_notification);
85  Error push_set(Object *p_object, const StringName& p_prop, const Variant& p_value);
86 
87  bool print();
88  void statistics();
89  void flush();
90 
91  int get_max_buffer_usage() const;
92 
93  MessageQueue();
94  ~MessageQueue();
95 };
96 
97 #endif // MESSAGE_QUEUE_H
Definition: variant.h:74
Definition: message_queue.h:35
Definition: mutex.h:45
Definition: string_db.h:48
Definition: object.h:317