Direct-BT  2.3.1
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 
34 namespace jau {
35 
36  /**
37  * Pure virtual JavaAnon, hiding Java JNI details from API,
38  * to be implemented by JNI module.
39  * <p>
40  * One implementation is JavaGlobalObj within the JNI module,
41  * wrapping a JNIGlobalRef instance.
42  * </p>
43  */
44  class JavaAnon {
45  public:
46  virtual ~JavaAnon() noexcept { }
47  virtual std::string toString() const noexcept { return "JavaAnon[???]"; }
48 
49  /** Clears the java reference, i.e. nulling it, without deleting the global reference via JNI. */
50  virtual void clear() noexcept = 0;
51  };
52 
53  /**
54  * Sharing the anonymous Java object (JavaAnon),
55  * i.e. exposing the Java object uplink to the C++ implementation.
56  */
57  class JavaUplink {
58  private:
59  std::shared_ptr<JavaAnon> javaObjectRef;
60 
61  public:
62  virtual std::string toString() const noexcept { return "JavaUplink["+jau::to_hexstring(this)+"]"; }
63 
64  virtual std::string get_java_class() const noexcept = 0;
65 
66  std::string javaObjectToString() const noexcept {
67  if( nullptr == javaObjectRef ) {
68  return "JavaAnon[null]";
69  } else if( 0 == javaObjectRef.use_count() ) { // safe-guard for concurrent dtor
70  return "JavaAnon[empty]";
71  }
72  return javaObjectRef->toString();
73  }
74 
75  std::shared_ptr<JavaAnon> getJavaObject() noexcept { return javaObjectRef; }
76 
77  /** Assigns a new shared JavaAnon reference, replaced item might be deleted via JNI from dtor */
78  void setJavaObject(std::shared_ptr<JavaAnon> objRef) noexcept { javaObjectRef = objRef; }
79 
80  /** Resets the shared JavaAnon reference, the replaced item might be deleted via JNI from dtor */
81  void setJavaObject() noexcept { javaObjectRef.reset(); }
82 
83  /** Clears the java reference, i.e. nulling it, without deleting the global reference via JNI. */
84  void clearJavaObject() noexcept {
85  if( nullptr != javaObjectRef ) {
86  javaObjectRef->clear();
87  }
88  }
89 
90  /**
91  * Throws an IllegalStateException if isValid() == false
92  */
93  virtual void checkValid() const {}
94 
95  virtual ~JavaUplink() noexcept {
96  javaObjectRef = nullptr;
97  }
98  };
99 
100 } /* namespace jau */
101 
102 
103 #endif /* JAU_JAVA_UPLINK_HPP_ */
jau::JavaAnon::~JavaAnon
virtual ~JavaAnon() noexcept
Definition: java_uplink.hpp:46
jau
Definition: basic_algos.hpp:34
jau::JavaAnon::toString
virtual std::string toString() const noexcept
Definition: java_uplink.hpp:47
jau::JavaAnon
Pure virtual JavaAnon, hiding Java JNI details from API, to be implemented by JNI module.
Definition: java_uplink.hpp:44
jau::to_hexstring
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
Definition: string_util.hpp:104
basic_types.hpp
jau::JavaAnon::clear
virtual void clear() noexcept=0
Clears the java reference, i.e.