• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_CAMERA_NDK_CALLBACK_MAP_H
17 #define OHOS_CAMERA_NDK_CALLBACK_MAP_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <unordered_map>
22 
23 template<typename K, typename V>
24 class NDKCallbackMap {
25 public:
SetMapValue(K key,std::shared_ptr<V> value)26     bool SetMapValue(K key, std::shared_ptr<V> value)
27     {
28         if (key == nullptr) {
29             return false;
30         }
31         std::lock_guard<std::mutex> lock(mapMutex_);
32         auto it = callbackMap_.find(key);
33         if (it != callbackMap_.end()) {
34             return false;
35         }
36         callbackMap_[key] = value;
37         return true;
38     }
39 
RemoveValue(K key)40     std::shared_ptr<V> RemoveValue(K key)
41     {
42         std::shared_ptr<V> callback = nullptr;
43         std::lock_guard<std::mutex> lock(mapMutex_);
44         auto it = callbackMap_.find(key);
45         if (it != callbackMap_.end()) {
46             callback = it->second;
47             callbackMap_.erase(it);
48         }
49         return callback;
50     }
51 
52 private:
53     std::mutex mapMutex_;
54     std::unordered_map<K, std::shared_ptr<V>> callbackMap_;
55 };
56 
57 #endif