4d287cc4df4035b3734285ffc85cac1dab1f1efd
[junction.git] / junction / extra / impl / MapAdapter_StdMap.h
1 /*------------------------------------------------------------------------
2   Junction: Concurrent data structures in C++
3   Copyright (c) 2016 Jeff Preshing
4
5   Distributed under the Simplified BSD License.
6   Original location: https://github.com/preshing/junction
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
12
13 #ifndef JUNCTION_EXTRA_IMPL_MAPADAPTER_STDMAP_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_STDMAP_H
15
16 #include <junction/Core.h>
17 #include <map>
18 #include <mutex>
19
20 namespace junction {
21 namespace extra {
22
23 class MapAdapter {
24 public:
25     static TURF_CONSTEXPR const char* MapName = "std::map + std::mutex";
26
27     MapAdapter(ureg) {
28     }
29
30     class ThreadContext {
31     public:
32         ThreadContext(MapAdapter&, ureg) {
33         }
34
35         void registerThread() {
36         }
37
38         void unregisterThread() {
39         }
40
41         void update() {
42         }
43     };
44
45     class Map {
46     private:
47         std::mutex m_mutex;
48         typedef std::map<u32, void*> MapType;
49         MapType m_map;
50
51     public:
52         Map(ureg) {
53         }
54
55         void insert(u32 key, void* value) {
56             std::lock_guard<std::mutex> guard(m_mutex);
57             m_map.insert(std::make_pair(key, value));
58         }
59
60         void* get(u32 key) {
61             std::lock_guard<std::mutex> guard(m_mutex);
62             MapType::iterator iter = m_map.find(key);
63             return (iter == m_map.end()) ? NULL : iter->second;
64         }
65     
66         void erase(u32 key) {
67             std::lock_guard<std::mutex> guard(m_mutex);
68             m_map.erase(key);
69         }
70
71         class Iterator {
72         private:
73             Map& m_map;
74             MapType::iterator m_iter;
75
76         public:
77             Iterator(Map& map) : m_map(map), m_iter(m_map.m_map.begin()) {
78             }
79
80             void next() {
81                 m_iter++;
82             }
83
84             bool isValid() const {
85                 return m_iter != m_map.m_map.end();
86             }
87
88             u32 getKey() const {
89                 return m_iter->first;
90             }
91
92             void* getValue() const {
93                 return m_iter->second;
94             }
95         };
96     };
97
98     static ureg getInitialCapacity(ureg) {
99         return 0;
100     }
101 };
102
103 } // namespace extra
104 } // namespace junction
105
106 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_STDMAP_H