variant_callable.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**************************************************************************/
  2. /* variant_callable.cpp */
  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. #include "variant_callable.h"
  31. #include "core/templates/hashfuncs.h"
  32. bool VariantCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  33. return p_a->hash() == p_b->hash();
  34. }
  35. bool VariantCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  36. return p_a->hash() < p_b->hash();
  37. }
  38. uint32_t VariantCallable::hash() const {
  39. return h;
  40. }
  41. String VariantCallable::get_as_text() const {
  42. return vformat("%s::%s (Callable)", Variant::get_type_name(variant.get_type()), method);
  43. }
  44. CallableCustom::CompareEqualFunc VariantCallable::get_compare_equal_func() const {
  45. return compare_equal;
  46. }
  47. CallableCustom::CompareLessFunc VariantCallable::get_compare_less_func() const {
  48. return compare_less;
  49. }
  50. bool VariantCallable::is_valid() const {
  51. return Variant::has_builtin_method(variant.get_type(), method);
  52. }
  53. StringName VariantCallable::get_method() const {
  54. return method;
  55. }
  56. ObjectID VariantCallable::get_object() const {
  57. return ObjectID();
  58. }
  59. int VariantCallable::get_argument_count(bool &r_is_valid) const {
  60. if (!Variant::has_builtin_method(variant.get_type(), method)) {
  61. r_is_valid = false;
  62. return 0;
  63. }
  64. r_is_valid = true;
  65. return Variant::get_builtin_method_argument_count(variant.get_type(), method);
  66. }
  67. void VariantCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  68. Variant v = variant;
  69. v.callp(method, p_arguments, p_argcount, r_return_value, r_call_error);
  70. }
  71. VariantCallable::VariantCallable(const Variant &p_variant, const StringName &p_method) {
  72. variant = p_variant;
  73. method = p_method;
  74. h = variant.hash();
  75. h = hash_murmur3_one_64(Variant::get_builtin_method_hash(variant.get_type(), method), h);
  76. }