summaryrefslogtreecommitdiff
path: root/platform/android/src/attach_env.hpp
blob: 8a9f4a7f8b2651e43056916c5be03fbc2ba5a549 (plain)
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
#pragma once

#include <jni/jni.hpp>

namespace mbgl {
namespace android {

class JNIEnvDeleter {
private:
    jni::JavaVM* vm = nullptr;
    bool detach = false;

public:
    JNIEnvDeleter() = default;
    JNIEnvDeleter(jni::JavaVM& v, bool d)
        : vm(&v), detach(d) {}

    void operator()(jni::JNIEnv* p) const {
        if (p && detach) {
          assert(vm);
          vm->DetachCurrentThread();
        }
     }
};

using UniqueEnv = std::unique_ptr<jni::JNIEnv, JNIEnvDeleter>;

UniqueEnv AttachEnv();

}
}