callable.h 7.4 KB

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