1 /*
2  * Copyright (C) 2023 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 BLUETOOTH_OBJECT_MAP_H
17 #define BLUETOOTH_OBJECT_MAP_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 
24 constexpr uint32_t MAX_MAP_SIZE = 100;
25 template <typename T, int maxSize = MAX_MAP_SIZE>
26 class BluetoothObjectMap final {
27 public:
28     BluetoothObjectMap() = default;
~BluetoothObjectMap()29     ~BluetoothObjectMap()
30     {
31         std::lock_guard<std::mutex> lock(lock_);
32         objectsMap.clear();
33     }
34 
AddObject(T object)35     int AddObject(T object)
36     {
37         std::lock_guard<std::mutex> lock(lock_);
38         if (objectIncrease > maxSize) {
39             objectIncrease = 1;
40         }
41         objectsMap.insert(std::make_pair(objectIncrease, object));
42         return objectIncrease++;
43     }
44 
RemoveObject(int objectId)45     void RemoveObject(int objectId)
46     {
47         std::lock_guard<std::mutex> lock(lock_);
48         objectsMap.erase(objectId);
49     }
50 
RemoveAllObject(void)51     void RemoveAllObject(void)
52     {
53         std::lock_guard<std::mutex> lock(lock_);
54         objectsMap.clear();
55     }
56 
GetObject(int objectId)57     T GetObject(int objectId)
58     {
59         std::lock_guard<std::mutex> lock(lock_);
60         auto iter = objectsMap.find(objectId);
61         if (iter == objectsMap.end()) {
62             return nullptr;
63         }
64         return iter->second;
65     }
66 
GetObject()67     T GetObject()
68     {
69         std::lock_guard<std::mutex> lock(lock_);
70         for (int i = 1; i < maxSize; i++) {
71             auto iter = objectsMap.find(i);
72             if (iter != objectsMap.end()) {
73                 return iter->second;
74             }
75         }
76         return nullptr;
77     }
78 
AddLimitedObject(T object)79     int AddLimitedObject(T object)
80     {
81         std::lock_guard<std::mutex> lock(lock_);
82         int i;
83         for (i = 1; i < maxSize; i++) {
84             if (objectsMap.find(i) == objectsMap.end()) {
85                 break;
86             }
87         }
88         if (i == maxSize) {
89             return -1;
90         }
91         objectsMap.insert(std::make_pair(i, object));
92         return i;
93     }
94 
95 private:
96     std::mutex lock_;
97     std::map<int, T> objectsMap;
98     int objectIncrease = 1;
99 
100     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothObjectMap);
101 };
102 
103 #endif  // BLUETOOTH_OBJECT_MAP_H