CARLA
AtomicMap.h
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <mutex>
10 #include <unordered_map>
11 
12 namespace carla {
13 namespace traffic_manager {
14 
15  template <typename Key, typename Value>
16  class AtomicMap {
17 
18  private:
19 
20  mutable std::mutex map_mutex;
21  std::unordered_map<Key, Value> map;
22 
23  public:
24 
25  AtomicMap() {}
26 
27  void AddEntry(const std::pair<Key, Value> &entry) {
28 
29  std::lock_guard<std::mutex> lock(map_mutex);
30  const Key& key = entry.first;
31  if (map.find(key) != map.end()) {
32  map.at(key) = entry.second;
33  } else {
34  map.insert(entry);
35  }
36  }
37 
38  bool Contains(const Key &key) const {
39 
40  std::lock_guard<std::mutex> lock(map_mutex);
41  return map.find(key) != map.end();
42  }
43 
44  const Value &GetValue(const Key &key) const {
45 
46  std::lock_guard<std::mutex> lock(map_mutex);
47  return map.at(key);
48  }
49 
50  void RemoveEntry(const Key &key) {
51 
52  std::lock_guard<std::mutex> lock(map_mutex);
53  map.erase(key);
54  }
55 
56  };
57 
58 } // namespace traffic_manager
59 } // namespace carla
void RemoveEntry(const Key &key)
Definition: AtomicMap.h:50
const Value & GetValue(const Key &key) const
Definition: AtomicMap.h:44
bool Contains(const Key &key) const
Definition: AtomicMap.h:38
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::unordered_map< Key, Value > map
Definition: AtomicMap.h:21
void AddEntry(const std::pair< Key, Value > &entry)
Definition: AtomicMap.h:27