Sync from Azure to LF
[ric-plt/e2.git] / RIC-E2-TERMINATION / mapWrapper.h
1 /*
2  * Copyright 2019 AT&T Intellectual Property
3  * Copyright 2019 Nokia
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 //
19 // Created by adi ENZEL on 8/28/19.
20 //
21
22 #ifndef E2_MAPWRAPPER_H
23 #define E2_MAPWRAPPER_H
24
25 #include <unordered_map>
26 #include <mutex>
27 #include <shared_mutex>
28 #include <thread>
29 #include <string>
30 #include <iostream>
31
32 using namespace std;
33
34 class mapWrapper {
35 public:
36     void *find(char *key) {
37         shared_lock<shared_timed_mutex> read(fence);
38         if (mdclog_level_get() >= MDCLOG_DEBUG) {
39
40         }
41         auto entry = keyMap.find(key);
42         if (entry == keyMap.end()) {
43             return nullptr;
44         }
45         return entry->second;
46     }
47
48     void setkey(char *key, void *val) {
49         unique_lock<shared_timed_mutex> write(fence);
50         keyMap[key] = val;
51     }
52
53     void *erase(char *key) {
54         unique_lock<shared_timed_mutex> write(fence);
55         return (void *)keyMap.erase(key);
56     }
57
58     void clear() {
59         unique_lock<shared_timed_mutex> write(fence);
60         keyMap.clear();
61     }
62
63     void getKeys(vector<char *> &v) {
64         shared_lock<shared_timed_mutex> read(fence);
65         for (auto const &e : keyMap) {
66             v.emplace_back((char *)e.first.c_str());
67         }
68     }
69
70
71
72 private:
73     std::unordered_map<string, void *> keyMap;
74     shared_timed_mutex fence;
75
76 };
77 #endif //E2_MAPWRAPPER_H