Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
Helpers.hh
Go to the documentation of this file.
1#ifndef __NCPP_INTERNAL_HELPERS_HH
2#define __NCPP_INTERNAL_HELPERS_HH
3
4#include <functional>
5#include <map>
6#include <mutex>
7
9{
10 class Helpers
11 {
12 public:
13 template<typename TKey, typename TValue>
14 static TValue lookup_map_entry (std::map<TKey,TValue> *&_map, std::mutex &_mutex, TKey _key, std::function<TValue (TKey)> create_value)
15 {
16 std::lock_guard<std::mutex> lock (_mutex);
17 if (_map == nullptr) {
18 _map = new std::map<TKey,TValue> ();
19 }
20
21 TValue ret;
22 auto entry = _map->find (_key);
23 if (entry == _map->end ()) {
24 ret = create_value (_key);
25 } else {
26 ret = entry->second;
27 }
28
29 return ret;
30 }
31
32 template<typename TKey, typename TValue>
33 static void remove_map_entry (std::map<TKey,TValue> *&_map, std::mutex &_mutex, TKey _key)
34 {
35 std::lock_guard<std::mutex> lock (_mutex);
36 if (_map == nullptr)
37 return;
38
39 auto entry = _map->find (_key);
40 if (entry == _map->end ())
41 return;
42
43 _map->erase (entry);
44 }
45 };
46}
47#endif
static TValue lookup_map_entry(std::map< TKey, TValue > *&_map, std::mutex &_mutex, TKey _key, std::function< TValue(TKey)> create_value)
Definition Helpers.hh:14
static void remove_map_entry(std::map< TKey, TValue > *&_map, std::mutex &_mutex, TKey _key)
Definition Helpers.hh:33