This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
/
sptr_wrapper.h
79 lines (63 loc) · 2.02 KB
/
sptr_wrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <jni.h>
#include "handle.h"
/// This class provides a compatibility bridge between JNI and \code{boost::shared_ptr<T>}; this is
/// needed in order to prevent lifecycle problems, such as a native smart pointer that expires before
/// the Java object that references it.
template <typename T>
class sptr_wrapper
{
private:
boost::shared_ptr<T> sptr;
public:
template <typename ...ARGS>
explicit sptr_wrapper(ARGS... a)
{
this->sptr = boost::make_shared<T>(a...);
}
explicit sptr_wrapper(boost::shared_ptr<T> sptr)
{
this->sptr = sptr;
}
virtual ~sptr_wrapper() noexcept = default;
void set_from_sptr(boost::shared_ptr<T> sptr)
{
this.sptr = sptr;
}
/// \brief{Sets the address of \code{this} wrapper as a handle for a java object within a JNI environment.}
void instantiate(JNIEnv *env, jobject obj)
{
set_handle<sptr_wrapper>(env, obj, this);
}
/// \returns{the address of the wrapper as a \code{jlong}.}
jlong instance()
{
return boost::reinterpret_pointer_cast<jlong>(this);
}
boost::shared_ptr<T> get_sptr() const
{
return sptr;
}
/// @param env The JNI environment.
/// @param java_instance The object for which to find the corresponding wrapper.
// \returns{the \code{shared_ptr} associated to a Java instance within a JNI environment.}
static boost::shared_ptr<T> get_sptr(JNIEnv *env, jobject java_instance)
{
return get_wrapper(env, java_instance)->get_sptr();
}
/// @param env The JNI environment.
/// @param java_instance The object for which to find the corresponding wrapper.
// \returns{the pointer to the wrapper associated to a Java instance within a JNI environment.}
static sptr_wrapper *get_wrapper(JNIEnv *env, jobject java_instance)
{
return get_handle<sptr_wrapper<T>>(env, java_instance);
}
static void dispose(JNIEnv *env, jobject java_instance)
{
auto wrapper = get_wrapper(env, java_instance);
delete wrapper;
set_handle<sptr_wrapper<T>>(env, java_instance, nullptr);
}
};