Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / main / jni / util / ScopedEnv.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef RCS_JIN_SCOPEDENV_H_
22 #define RCS_JIN_SCOPEDENV_H_
23
24 #include <utility>
25
26 #include <jni.h>
27
28 #include "JNIEnvWrapper.h"
29 #include "Log.h"
30
31 extern JavaVM* g_jvm;
32
33 namespace Detail
34 {
35     inline std::pair<JNIEnv*, bool> getEnv()
36     {
37         JNIEnv* env{ };
38         bool needToDetach{ };
39
40         auto ret = g_jvm->GetEnv((void**) &env, JNI_VERSION_1_6);
41
42         switch (ret)
43         {
44             case JNI_OK:
45                 break;
46
47             case JNI_EDETACHED:
48             {
49                 auto attachRet = g_jvm->AttachCurrentThread(&env, NULL);
50
51                 if (attachRet != JNI_OK)
52                 {
53                     LOGT_E("JNI-ScopedEnv", "Failed to get the environment : %d", attachRet);
54                 }
55                 else
56                 {
57                     needToDetach = true;
58                 }
59                 break;
60             }
61             case JNI_EVERSION:
62                 LOGT_E("JNI-ScopedEnv", "JNI version not supported");
63                 break;
64
65             default:
66                 LOGT_E("JNI-ScopedEnv", "Failed to get the environment");
67                 break;
68         }
69
70         return { env, needToDetach };
71     }
72 }
73
74 class ScopedEnv
75 {
76 public:
77     ScopedEnv() noexcept :
78         m_env { },
79         m_needToDetach{ false }
80     {
81         auto val = Detail::getEnv();
82
83         m_env = val.first;
84         m_needToDetach = val.second;
85     }
86
87     ~ScopedEnv()
88     {
89         if (m_env && m_needToDetach)
90         {
91             g_jvm->DetachCurrentThread();
92         }
93     }
94
95     ScopedEnv(const ScopedEnv&) = delete;
96     ScopedEnv& operator=(const ScopedEnv&) = delete;
97
98     operator bool() const noexcept
99     {
100         return m_env;
101     }
102
103     JNIEnv* operator->() noexcept
104     {
105         return m_env;
106     }
107
108     JNIEnv* get() noexcept
109     {
110         return m_env;
111     }
112
113 private:
114     JNIEnv* m_env;
115     bool m_needToDetach;
116 };
117
118 class ScopedEnvWrapper
119 {
120 public:
121     ScopedEnvWrapper() noexcept :
122         m_env { },
123         m_needToDetach{ false }
124     {
125         auto val = Detail::getEnv();
126
127         m_env = val.first;
128         m_needToDetach = val.second;
129     }
130
131     ~ScopedEnvWrapper()
132     {
133         if (m_env && m_needToDetach)
134         {
135             g_jvm->DetachCurrentThread();
136         }
137     }
138
139     ScopedEnvWrapper(const ScopedEnvWrapper&) = delete;
140     ScopedEnvWrapper& operator=(const ScopedEnvWrapper&) = delete;
141
142     operator bool() const noexcept
143     {
144         return m_env;
145     }
146
147     JNIEnvWrapper* operator->() noexcept
148     {
149         return &m_env;
150     }
151
152     JNIEnvWrapper* get() noexcept
153     {
154         return &m_env;
155     }
156
157 private:
158     JNIEnvWrapper m_env;
159     bool m_needToDetach;
160 };
161
162 #endif // RCS_JIN_SCOPEDENV_H_