123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #ifndef TS_MAP_H
- #define TS_MAP_H
-
-
- #pragma once
- #include <map>
- #include <mutex>
- #include <condition_variable>
-
- template <typename KEY, typename VALUE>
- class ts_map
- {
- public:
- ts_map() = default;
- ts_map(const ts_map<KEY, VALUE>&) = delete;
- virtual ~ts_map() { clear(); }
-
- public:
- void emplaceOrOverwrite(KEY key, VALUE value){
- std::scoped_lock lock(muxMap);
- auto it = map.find(key);
- if(it != map.end()){
- it->second = value;
- }
- else{
- map.emplace(key, value);
- }
- }
-
- void emplace(KEY key, VALUE&& value){
- std::scoped_lock lock(muxMap);
- map.emplace(key, std::move(value));
- }
-
- auto find(KEY key){
- std::scoped_lock lock(muxMap);
-
- return map.find(key);
- }
-
-
- // Returns true if Queue has no items
- bool empty()
- {
- std::scoped_lock lock(muxMap);
- return map.empty();
- }
-
- // Returns number of items in Queue
- size_t size()
- {
- std::scoped_lock lock(muxMap);
- return map.size();
- }
-
- // Clears Queue
- void clear()
- {
- std::scoped_lock lock(muxMap);
- std::map<KEY, VALUE> empty;
- std::swap(map, empty);
- }
-
- void wait()
- {
- while (empty())
- {
- std::unique_lock<std::mutex> ul(muxBlocking);
- cvBlocking.wait(ul);
- }
- }
-
- auto begin() const{
- return map.begin();
- }
-
-
- auto end() const{
- return map.end();
- }
-
- protected:
- std::mutex muxMap;
- std::map<KEY, VALUE> map;
- std::condition_variable cvBlocking;
- std::mutex muxBlocking;
-
- };
-
- #endif // TS_MAP_H
|