Path: blob/master/platform/android/api/jni_singleton.h
20952 views
/**************************************************************************/1/* jni_singleton.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 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/**************************************************************************/2930#pragma once3132#include "java_class_wrapper.h"3334#include "core/config/engine.h"35#include "core/templates/rb_map.h"36#include "core/variant/variant.h"3738class JNISingleton : public Object {39GDCLASS(JNISingleton, Object);4041struct MethodData {42Variant::Type ret_type;43Vector<Variant::Type> argtypes;44};4546RBMap<StringName, MethodData> method_map;47Ref<JavaObject> wrapped_object;4849protected:50static void _bind_methods() {51ClassDB::bind_method(D_METHOD("has_java_method", "method"), &JNISingleton::has_java_method);52}5354public:55virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {56// Godot methods take precedence.57Variant ret = Object::callp(p_method, p_args, p_argcount, r_error);58if (r_error.error == Callable::CallError::CALL_OK) {59return ret;60}6162// Check the method we're looking for is in the JNISingleton map.63// This is done because JNISingletons register methods differently than wrapped JavaClass / JavaObject to allow64// for access to private methods annotated with the @UsedByGodot annotation.65// In the future, we should remove access to private methods and require that JNISingletons' methods exposed to66// GDScript be all public, similarly to what we do for wrapped JavaClass / JavaObject methods. Doing so will67// also allow dropping and deprecating the @UsedByGodot annotation.68RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);69if (E) {70if (wrapped_object.is_valid()) {71return wrapped_object->callp(p_method, p_args, p_argcount, r_error);72} else {73r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;74}75}76return Variant();77}7879Ref<JavaObject> get_wrapped_object() const {80return wrapped_object;81}8283bool has_java_method(const StringName &p_method) const {84return method_map.has(p_method);85}8687void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {88MethodData md;89md.argtypes = p_args;90md.ret_type = p_ret_type;91method_map[p_name] = md;92}9394void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {95MethodInfo mi;96mi.name = p_name;97for (int i = 0; i < p_args.size(); i++) {98mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));99}100ADD_SIGNAL(mi);101}102103JNISingleton() {}104105JNISingleton(const Ref<JavaObject> &p_wrapped_object) {106wrapped_object = p_wrapped_object;107}108109~JNISingleton() {110method_map.clear();111wrapped_object.unref();112}113};114115116