#ifndef TS_MAP_H #define TS_MAP_H #pragma once #include #include #include template class ts_map { public: ts_map() = default; ts_map(const ts_map&) = 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 empty; std::swap(map, empty); } void wait() { while (empty()) { std::unique_lock ul(muxBlocking); cvBlocking.wait(ul); } } auto begin() const{ return map.begin(); } auto end() const{ return map.end(); } protected: std::mutex muxMap; std::map map; std::condition_variable cvBlocking; std::mutex muxBlocking; }; #endif // TS_MAP_H