jaulib v1.4.1
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
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#include <jau/string_util.hpp>
34
35namespace jau::jni {
36
37 /** @defgroup JavaVM Java VM Utilities
38 * Java virtual machine support, helping accessing the JVM and converting data types.
39 *
40 * @{
41 */
42
43 /**
44 * Pure virtual JavaAnon, hiding Java JNI details from API,
45 * to be implemented by JNI module.
46 * <p>
47 * One implementation is JavaGlobalObj within the JNI module,
48 * wrapping a JNIGlobalRef instance.
49 * </p>
50 */
51 class JavaAnon {
52 public:
53 virtual ~JavaAnon() noexcept = default;
54 virtual std::string toString() const noexcept { return "JavaAnon[???]"; }
55 };
56 typedef std::shared_ptr<JavaAnon> JavaAnonRef;
57
58 /**
59 * Sharing the anonymous Java object (JavaAnon),
60 * i.e. exposing the Java object uplink to the C++ implementation.
61 */
62 class JavaUplink {
63 private:
64 JavaAnonRef javaObjectRef;
65
66 public:
67 virtual std::string toString() const noexcept { return "JavaUplink["+jau::toHexString(this)+"]"; }
68
69 virtual std::string get_java_class() const noexcept = 0;
70
71 std::string javaObjectToString() const noexcept {
72 if( nullptr == javaObjectRef ) {
73 return "JavaAnon[null]";
74 } else if( 0 == javaObjectRef.use_count() ) { // safe-guard for concurrent dtor
75 return "JavaAnon[empty]";
76 }
77 return javaObjectRef->toString();
78 }
79
80 const JavaAnonRef& getJavaObject() noexcept { return javaObjectRef; }
81
82 /** Assigns a new shared JavaAnon reference, replaced item might be deleted via JNI from dtor */
83 void setJavaObject(const JavaAnonRef& objRef) noexcept { javaObjectRef = objRef; }
84
85 /** Resets the shared JavaAnon reference, the replaced item might be deleted via JNI from dtor */
86 void setJavaObject() noexcept { javaObjectRef.reset(); }
87
88 /**
89 * Throws an IllegalStateException if instance is not valid
90 *
91 * Default implementation does nothing.
92 */
93 virtual void checkValidInstance() const {}
94
95 JavaUplink() noexcept = default;
96 JavaUplink(const JavaUplink &o) noexcept = default;
97 JavaUplink(JavaUplink &&o) noexcept = default;
98 JavaUplink& operator=(const JavaUplink &o) noexcept = default;
99 JavaUplink& operator=(JavaUplink &&o) noexcept = default;
100
101 virtual ~JavaUplink() noexcept { // NOLINT(modernize-use-equals-default): Intended
102 javaObjectRef = nullptr;
103 }
104 };
105 typedef std::shared_ptr<JavaUplink> JavaUplinkRef;
106
107 /**@}*/
108
109} /* namespace jau */
110
111
112#endif /* JAU_JAVA_UPLINK_HPP_ */
Pure virtual JavaAnon, hiding Java JNI details from API, to be implemented by JNI module.
virtual std::string toString() const noexcept
virtual ~JavaAnon() noexcept=default
std::shared_ptr< JavaUplink > JavaUplinkRef
std::shared_ptr< JavaAnon > JavaAnonRef
std::string toHexString(const void *data, const nsize_t length, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt prefix=PrefixOpt::prefix) noexcept
Produce a hexadecimal string representation of the given lsb-first byte values.
STL namespace.