File indexing completed on 2024-06-23 05:08:44

0001 /*
0002     SPDX-FileCopyrightText: 2021-2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #ifndef KANDROIDEXTRAS_JNIRETURNVALUE_H
0007 #define KANDROIDEXTRAS_JNIRETURNVALUE_H
0008 
0009 #include "jniobject.h"
0010 #include "jnitypetraits.h"
0011 
0012 namespace KAndroidExtras {
0013 namespace Jni {
0014 template <typename T> class Array;
0015 }
0016 
0017 ///@cond internal
0018 namespace Internal {
0019     /** Return value wrapping helper. */
0020     template <typename RetT, typename = std::void_t<>>
0021     class return_wrapper {
0022         static_assert(!is_invalid_primitive_type<RetT>::value, "Using an incompatible primitive type!");
0023         static inline constexpr bool is_primitive = Jni::is_primitive_type<RetT>::value;
0024         static inline constexpr bool is_convertible = !std::is_same_v<typename Jni::converter<RetT>::type, void>;
0025 
0026         typedef std::conditional_t<is_primitive, RetT, QJniObject> JniReturnT;
0027 
0028     public:
0029         static inline constexpr auto toReturnValue(JniReturnT value)
0030         {
0031             if constexpr (is_convertible) {
0032                 return Jni::Object<RetT>(value);
0033             }
0034             else if constexpr (is_primitive) {
0035                 return primitive_value<RetT>::fromJni(value);
0036             }
0037             else {
0038                 return value;
0039             }
0040         }
0041     };
0042     template <typename RetT>
0043     class return_wrapper<RetT, std::void_t<typename RetT::_jni_ThisType>> {
0044     public:
0045         static inline auto toReturnValue(const QJniObject &value)
0046         {
0047             return Jni::fromHandle<RetT>(value);
0048         }
0049     };
0050     template <typename RetT>
0051     class return_wrapper<Jni::Array<RetT>> {
0052     public:
0053         static inline auto toReturnValue(const QJniObject &value)
0054         {
0055             return Jni::Array<RetT>(value);
0056         }
0057     };
0058     template <>
0059     struct return_wrapper<void> {};
0060 }
0061 ///@endcond
0062 }
0063 
0064 #endif