Path: blob/master/platform/android/api/jni_singleton.h
10278 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/variant/variant.h"3637class JNISingleton : public Object {38GDCLASS(JNISingleton, Object);3940struct MethodData {41Variant::Type ret_type;42Vector<Variant::Type> argtypes;43};4445RBMap<StringName, MethodData> method_map;46Ref<JavaObject> wrapped_object;4748public:49virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override {50if (wrapped_object.is_valid()) {51RBMap<StringName, MethodData>::Element *E = method_map.find(p_method);5253// Check the method we're looking for is in the JNISingleton map and that54// the arguments match.55bool call_error = !E || E->get().argtypes.size() != p_argcount;56if (!call_error) {57for (int i = 0; i < p_argcount; i++) {58if (!Variant::can_convert(p_args[i]->get_type(), E->get().argtypes[i])) {59call_error = true;60break;61}62}63}6465if (!call_error) {66return wrapped_object->callp(p_method, p_args, p_argcount, r_error);67}68}6970return Object::callp(p_method, p_args, p_argcount, r_error);71}7273Ref<JavaObject> get_wrapped_object() const {74return wrapped_object;75}7677void add_method(const StringName &p_name, const Vector<Variant::Type> &p_args, Variant::Type p_ret_type) {78MethodData md;79md.argtypes = p_args;80md.ret_type = p_ret_type;81method_map[p_name] = md;82}8384void add_signal(const StringName &p_name, const Vector<Variant::Type> &p_args) {85MethodInfo mi;86mi.name = p_name;87for (int i = 0; i < p_args.size(); i++) {88mi.arguments.push_back(PropertyInfo(p_args[i], "arg" + itos(i + 1)));89}90ADD_SIGNAL(mi);91}9293JNISingleton() {}9495JNISingleton(const Ref<JavaObject> &p_wrapped_object) {96wrapped_object = p_wrapped_object;97}9899~JNISingleton() {100method_map.clear();101wrapped_object.unref();102}103};104105106