callable.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**************************************************************************/
  2. /* callable.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef CALLABLE_H
  31. #define CALLABLE_H
  32. #include "core/object/object_id.h"
  33. #include "core/string/string_name.h"
  34. class Object;
  35. class Variant;
  36. class CallableCustom;
  37. // This is an abstraction of things that can be called.
  38. // It is used for signals and other cases where efficient calling of functions
  39. // is required. It is designed for the standard case (object and method)
  40. // but can be optimized or customized.
  41. // Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.
  42. class Callable {
  43. alignas(8) StringName method;
  44. union {
  45. uint64_t object = 0;
  46. CallableCustom *custom;
  47. };
  48. public:
  49. struct CallError {
  50. enum Error {
  51. CALL_OK,
  52. CALL_ERROR_INVALID_METHOD,
  53. CALL_ERROR_INVALID_ARGUMENT, // expected is variant type
  54. CALL_ERROR_TOO_MANY_ARGUMENTS, // expected is number of arguments
  55. CALL_ERROR_TOO_FEW_ARGUMENTS, // expected is number of arguments
  56. CALL_ERROR_INSTANCE_IS_NULL,
  57. CALL_ERROR_METHOD_NOT_CONST,
  58. };
  59. Error error = Error::CALL_OK;
  60. int argument = 0;
  61. int expected = 0;
  62. };
  63. template <typename... VarArgs>
  64. Variant call(VarArgs... p_args) const;
  65. void callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const;
  66. void call_deferredp(const Variant **p_arguments, int p_argcount) const;
  67. Variant callv(const Array &p_arguments) const;
  68. template <typename... VarArgs>
  69. void call_deferred(VarArgs... p_args) const {
  70. Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +1 makes sure zero sized arrays are also supported.
  71. const Variant *argptrs[sizeof...(p_args) + 1];
  72. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  73. argptrs[i] = &args[i];
  74. }
  75. return call_deferredp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  76. }
  77. Error rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const;
  78. _FORCE_INLINE_ bool is_null() const {
  79. return method == StringName() && object == 0;
  80. }
  81. _FORCE_INLINE_ bool is_custom() const {
  82. return method == StringName() && custom != nullptr;
  83. }
  84. _FORCE_INLINE_ bool is_standard() const {
  85. return method != StringName();
  86. }
  87. bool is_valid() const;
  88. template <typename... VarArgs>
  89. Callable bind(VarArgs... p_args) const;
  90. Callable bindv(const Array &p_arguments);
  91. Callable bindp(const Variant **p_arguments, int p_argcount) const;
  92. Callable unbind(int p_argcount) const;
  93. Object *get_object() const;
  94. ObjectID get_object_id() const;
  95. StringName get_method() const;
  96. CallableCustom *get_custom() const;
  97. int get_argument_count(bool *r_is_valid = nullptr) const;
  98. int get_bound_arguments_count() const;
  99. void get_bound_arguments_ref(Vector<Variant> &r_arguments) const; // Internal engine use, the exposed one is below.
  100. Array get_bound_arguments() const;
  101. int get_unbound_arguments_count() const;
  102. uint32_t hash() const;
  103. const Callable *get_base_comparator() const; //used for bind/unbind to do less precise comparisons (ignoring binds) in signal connect/disconnect
  104. bool operator==(const Callable &p_callable) const;
  105. bool operator!=(const Callable &p_callable) const;
  106. bool operator<(const Callable &p_callable) const;
  107. void operator=(const Callable &p_callable);
  108. operator String() const;
  109. static Callable create(const Variant &p_variant, const StringName &p_method);
  110. Callable(const Object *p_object, const StringName &p_method);
  111. Callable(ObjectID p_object, const StringName &p_method);
  112. Callable(CallableCustom *p_custom);
  113. Callable(const Callable &p_callable);
  114. Callable() {}
  115. ~Callable();
  116. };
  117. class CallableCustom {
  118. friend class Callable;
  119. SafeRefCount ref_count;
  120. bool referenced = false;
  121. public:
  122. typedef bool (*CompareEqualFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
  123. typedef bool (*CompareLessFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
  124. //for every type that inherits, these must always be the same for this type
  125. virtual uint32_t hash() const = 0;
  126. virtual String get_as_text() const = 0;
  127. virtual CompareEqualFunc get_compare_equal_func() const = 0;
  128. virtual CompareLessFunc get_compare_less_func() const = 0;
  129. virtual bool is_valid() const;
  130. virtual StringName get_method() const;
  131. virtual ObjectID get_object() const = 0;
  132. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0;
  133. virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const;
  134. virtual const Callable *get_base_comparator() const;
  135. virtual int get_argument_count(bool &r_is_valid) const;
  136. virtual int get_bound_arguments_count() const;
  137. virtual void get_bound_arguments(Vector<Variant> &r_arguments) const;
  138. virtual int get_unbound_arguments_count() const;
  139. CallableCustom();
  140. virtual ~CallableCustom() {}
  141. };
  142. // This is just a proxy object to object signals, its only
  143. // allocated on demand by/for scripting languages so it can
  144. // be put inside a Variant, but it is not
  145. // used by the engine itself.
  146. // Enforce 16 bytes with `alignas` to avoid arch-specific alignment issues on x86 vs armv7.
  147. class Signal {
  148. alignas(8) StringName name;
  149. ObjectID object;
  150. public:
  151. _FORCE_INLINE_ bool is_null() const {
  152. return object.is_null() && name == StringName();
  153. }
  154. Object *get_object() const;
  155. ObjectID get_object_id() const;
  156. StringName get_name() const;
  157. bool operator==(const Signal &p_signal) const;
  158. bool operator!=(const Signal &p_signal) const;
  159. bool operator<(const Signal &p_signal) const;
  160. operator String() const;
  161. Error emit(const Variant **p_arguments, int p_argcount) const;
  162. Error connect(const Callable &p_callable, uint32_t p_flags = 0);
  163. void disconnect(const Callable &p_callable);
  164. bool is_connected(const Callable &p_callable) const;
  165. bool has_connections() const;
  166. Array get_connections() const;
  167. Signal(const Object *p_object, const StringName &p_name);
  168. Signal(ObjectID p_object, const StringName &p_name);
  169. Signal() {}
  170. };
  171. struct CallableComparator {
  172. const Callable &func;
  173. bool operator()(const Variant &p_l, const Variant &p_r) const;
  174. };
  175. #endif // CALLABLE_H