#ifndef PROXYSERVER_CONN_MAP_H #define PROXYSERVER_CONN_MAP_H #include #include #include #include #include template class SafeMap { public: void add(const Key& key, std::unique_ptr &value) { std::unique_lock lock(mutex_); map_[key] = std::move(value); } Value* get(const Key& key) const { std::shared_lock lock(mutex_); auto it = map_.find(key); if (it != map_.end()) { return it->second.get(); } return nullptr; } void remove(const Key& key) { std::unique_lock lock(mutex_); map_.erase(key); } void clear() { std::unique_lock lock(mutex_); map_.clear(); } typename std::map>::iterator begin() { return map_.begin(); } typename std::map>::iterator end() { return map_.end(); } protected: mutable std::shared_timed_mutex mutex_; std::map> map_; }; template class TcpConnMap : public SafeMap { public: static TcpConnMap& getInstance() { static TcpConnMap instance; // 在首次使用时创建 return instance; } TcpConnMap(TcpConnMap const&) = delete; void operator=(TcpConnMap const&) = delete; private: TcpConnMap() = default; }; template class UdpConnMap : public SafeMap { public: static UdpConnMap& getInstance() { static UdpConnMap instance; // 在首次使用时创建 return instance; } UdpConnMap(UdpConnMap const&) = delete; void operator=(UdpConnMap const&) = delete; private: UdpConnMap() = default; }; #endif //PROXYSERVER_CONN_MAP_H