Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
java_uplink.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef JAU_JAVA_UPLINK_HPP_
27#define JAU_JAVA_UPLINK_HPP_
28
29#include <string>
30#include <memory>
31
32#include <jau/basic_types.hpp>
33
34namespace jau::jni {
35
36 /** @defgroup JavaVM Java VM Utilities
37 * Java virtual machine support, helping accessing the JVM and converting data types.
38 *
39 * @{
40 */
41
42 /**
43 * Pure virtual JavaAnon, hiding Java JNI details from API,
44 * to be implemented by JNI module.
45 * <p>
46 * One implementation is JavaGlobalObj within the JNI module,
47 * wrapping a JNIGlobalRef instance.
48 * </p>
49 */
50 class JavaAnon {
51 public:
52 virtual ~JavaAnon() noexcept = default;
53 virtual std::string toString() const noexcept { return "JavaAnon[???]"; }
54 };
55 typedef std::shared_ptr<JavaAnon> JavaAnonRef;
56
57 /**
58 * Sharing the anonymous Java object (JavaAnon),
59 * i.e. exposing the Java object uplink to the C++ implementation.
60 */
61 class JavaUplink {
62 private:
63 JavaAnonRef javaObjectRef;
64
65 public:
66 virtual std::string toString() const noexcept { return "JavaUplink["+jau::to_hexstring(this)+"]"; }
67
68 virtual std::string get_java_class() const noexcept = 0;
69
70 std::string javaObjectToString() const noexcept {
71 if( nullptr == javaObjectRef ) {
72 return "JavaAnon[null]";
73 } else if( 0 == javaObjectRef.use_count() ) { // safe-guard for concurrent dtor
74 return "JavaAnon[empty]";
75 }
76 return javaObjectRef->toString();
77 }
78
79 const JavaAnonRef& getJavaObject() noexcept { return javaObjectRef; }
80
81 /** Assigns a new shared JavaAnon reference, replaced item might be deleted via JNI from dtor */
82 void setJavaObject(const JavaAnonRef& objRef) noexcept { javaObjectRef = objRef; }
83
84 /** Resets the shared JavaAnon reference, the replaced item might be deleted via JNI from dtor */
85 void setJavaObject() noexcept { javaObjectRef.reset(); }
86
87 /**
88 * Throws an IllegalStateException if instance is not valid
89 *
90 * Default implementation does nothing.
91 */
92 virtual void checkValidInstance() const {}
93
94 JavaUplink() noexcept = default;
95 JavaUplink(const JavaUplink &o) noexcept = default;
96 JavaUplink(JavaUplink &&o) noexcept = default;
97 JavaUplink& operator=(const JavaUplink &o) noexcept = default;
98 JavaUplink& operator=(JavaUplink &&o) noexcept = default;
99
100 virtual ~JavaUplink() noexcept {
101 javaObjectRef = nullptr;
102 }
103 };
104 typedef std::shared_ptr<JavaUplink> JavaUplinkRef;
105
106 /**@}*/
107
108} /* namespace jau */
109
110
111#endif /* JAU_JAVA_UPLINK_HPP_ */
Pure virtual JavaAnon, hiding Java JNI details from API, to be implemented by JNI module.
Definition: java_uplink.hpp:50
virtual std::string toString() const noexcept
Definition: java_uplink.hpp:53
virtual ~JavaAnon() noexcept=default
std::shared_ptr< JavaUplink > JavaUplinkRef
std::shared_ptr< JavaAnon > JavaAnonRef
Definition: java_uplink.hpp:55
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
STL namespace.