Clean up some SonarQube warnings (trailing whitespace, etc).
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniListenerManager.h
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniOcStack.h"
24 #include <mutex>
25
26 #ifndef _JniListenerManager
27 #define _JniListenerManager
28
29 class JniOcResource;
30
31 template <class T>
32 class JniListenerManager
33 {
34 public:
35     T* addListener(JNIEnv* env, jobject jListener, JniOcResource* owner)
36     {
37         T *onEventListener = NULL;
38
39         m_mapMutex.lock();
40
41         for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
42         {
43             if (env->IsSameObject(jListener, it->first))
44             {
45                 auto refPair = it->second;
46                 onEventListener = refPair.first;
47                 refPair.second++;
48                 it->second = refPair;
49                 m_listenerMap.insert(*it);
50                 LOGD("OnEventListener: ref. count is incremented");
51                 break;
52             }
53         }
54         if (!onEventListener)
55         {
56             onEventListener = new T(env, jListener, owner);
57             jobject jgListener = env->NewGlobalRef(jListener);
58
59             if (jgListener)
60             {
61                 m_listenerMap.insert(std::pair<jobject, std::pair<T*, int>>(jgListener, std::pair<T*, int>(onEventListener, 1)));
62             }
63             else
64             {
65                 LOGD("OnEventListener: Failed to create global listener ref.");
66                 delete onEventListener;
67             }
68             LOGD("OnEventListener: new listener");
69         }
70         m_mapMutex.unlock();
71         return onEventListener;
72     }
73
74     void removeListener(JNIEnv* env, jobject jListener)
75     {
76         m_mapMutex.lock();
77         for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
78         {
79             if (env->IsSameObject(jListener, it->first))
80             {
81                 auto refPair = it->second;
82                 if (refPair.second > 1)
83                 {
84                     refPair.second--;
85                     it->second = refPair;
86                     m_listenerMap.insert(*it);
87                     LOGI("OnEventListener: ref. count is decremented");
88                 }
89                 else
90                 {
91                     env->DeleteGlobalRef(it->first);
92                     T* listener = refPair.first;
93                     delete listener;
94                     m_listenerMap.erase(it);
95
96                     LOGI("OnEventListener is removed");
97                 }
98                 break;
99             }
100         }
101         m_mapMutex.unlock();
102     }
103
104     void removeAllListeners(JNIEnv* env)
105     {
106         m_mapMutex.lock();
107
108         for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
109         {
110             env->DeleteGlobalRef(it->first);
111             auto refPair = it->second;
112             T* listener = refPair.first;
113             delete listener;
114             m_listenerMap.erase(it);
115         }
116
117         m_mapMutex.unlock();
118     }
119
120 private:
121     std::map<jobject, std::pair<T*, int>> m_listenerMap;
122     std::mutex m_mapMutex;
123 };
124
125 #endif