Distribution_Service/CC_SDK/Include/basic/CCMap.h

116 lines
2.7 KiB
C
Raw Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CCMAP_H
#define CCMAP_H
#include <mutex>
#include "CCArrayList.h"
#include "CCList.h"
#include "CCVector.h"
#include "map"
template<typename K,typename V>
class CCMap:public std::map<K,V> {
std::mutex mutex_s;
public:
2026-03-24 14:43:26 +08:00
// 默认构造函数
CCMap() : std::map<K,V>() {}
// 继承 std::map 的构造函数
2025-11-11 17:46:19 +08:00
using std::map<K,V>::map;
2026-03-24 14:43:26 +08:00
2025-11-11 17:46:19 +08:00
CCMap(std::map<K,V> map) {
this->insert(map.begin(),map.end());
}
CCMap(const std::map<K,V>& map) {
this->insert(map.begin(),map.end());
}
CCMap(const CCMap<K,V>& map) {
this->insert(map.begin(),map.end());
}
CCList<V> values() {
CCList<V> list;
for(auto it=this->begin();it!=this->end();++it) {
list.add(it->second);
}
return list;
}
CCVector<V> valuesVector() {
CCVector<V> vector;
for(auto it=this->begin();it!=this->end();++it) {
vector.add(it->second);
}
return vector;
}
CTL::ArrayList<V> valuesArrayList() {
CTL::ArrayList<V> arrayList;
for(auto it=this->begin();it!=this->end();++it) {
arrayList.add(it->second);
}
return arrayList;
}
/**
*
* @param key
* @param value
*/
void put_lock(K key,V value) {
mutex_s.lock();
this->insert(std::pair<K,V>(key,value));
mutex_s.unlock();
}
/**
*
* @param key
* @param value
*/
void replace_lock(K key,V value) {
mutex_s.lock();
this->at(key) = value;
mutex_s.unlock();
}
/**
*
* @param key
*/
void remove_lock(K key) {
mutex_s.lock();
this->erase(key);
mutex_s.unlock();
}
/**
* @param key
* @return
*/
V get(K key) {
std::unique_lock<std::mutex> lock(mutex_s);
return this->at(key);
}
bool IsEmpty() {
std::unique_lock<std::mutex> lock(mutex_s);
return this->empty();
}
operator std::map<K,V>() {
return *this;
}
CCMap operator = (const std::map<K,V>& map) {
this->clear();
this->insert(map.begin(),map.end());
return *this;
}
CCMap operator = (const CCMap<K,V>& map) {
this->clear();
this->insert(map.begin(),map.end());
return *this;
}
CCMap operator = (const CCMap<K,V>* map) {
this->clear();
this->insert(map->begin(),map->end());
return *this;
}
2026-03-20 09:51:56 +08:00
bool exist(K key) {
std::unique_lock<std::mutex> lock(mutex_s);
return this->find(key) != this->end();
}
2025-11-11 17:46:19 +08:00
};
#endif