script_instance.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**************************************************************************/
  2. /* script_instance.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 SCRIPT_INSTANCE_H
  31. #define SCRIPT_INSTANCE_H
  32. #include "core/object/ref_counted.h"
  33. class Script;
  34. class ScriptLanguage;
  35. class ScriptInstance {
  36. public:
  37. virtual bool set(const StringName &p_name, const Variant &p_value) = 0;
  38. virtual bool get(const StringName &p_name, Variant &r_ret) const = 0;
  39. virtual void get_property_list(List<PropertyInfo> *p_properties) const = 0;
  40. virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const = 0;
  41. virtual void validate_property(PropertyInfo &p_property) const = 0;
  42. virtual bool property_can_revert(const StringName &p_name) const = 0;
  43. virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const = 0;
  44. virtual Object *get_owner() { return nullptr; }
  45. virtual void get_property_state(List<Pair<StringName, Variant>> &state);
  46. virtual void get_method_list(List<MethodInfo> *p_list) const = 0;
  47. virtual bool has_method(const StringName &p_method) const = 0;
  48. virtual int get_method_argument_count(const StringName &p_method, bool *r_is_valid = nullptr) const;
  49. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = 0;
  50. template <typename... VarArgs>
  51. Variant call(const StringName &p_method, VarArgs... p_args) {
  52. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  53. const Variant *argptrs[sizeof...(p_args) + 1];
  54. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  55. argptrs[i] = &args[i];
  56. }
  57. Callable::CallError cerr;
  58. return callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), cerr);
  59. }
  60. virtual Variant call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); // implement if language supports const functions
  61. virtual void notification(int p_notification, bool p_reversed = false) = 0;
  62. virtual String to_string(bool *r_valid) {
  63. if (r_valid) {
  64. *r_valid = false;
  65. }
  66. return String();
  67. }
  68. //this is used by script languages that keep a reference counter of their own
  69. //you can make Ref<> not die when it reaches zero, so deleting the reference
  70. //depends entirely from the script
  71. virtual void refcount_incremented() {}
  72. virtual bool refcount_decremented() { return true; } //return true if it can die
  73. virtual Ref<Script> get_script() const = 0;
  74. virtual bool is_placeholder() const { return false; }
  75. virtual void property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid);
  76. virtual Variant property_get_fallback(const StringName &p_name, bool *r_valid);
  77. virtual const Variant get_rpc_config() const;
  78. virtual ScriptLanguage *get_language() = 0;
  79. virtual ~ScriptInstance();
  80. };
  81. #endif // SCRIPT_INSTANCE_H