70 lines
2.0 KiB
C++
70 lines
2.0 KiB
C++
#pragma once
|
|
/**
|
|
* @file SerializeMap.h
|
|
* @brief
|
|
* @author your name (you@domain.com)
|
|
* @version 0.1
|
|
* @date 2023-12-22
|
|
*
|
|
* Copyright: Baosight Co. Ltd.
|
|
* DO NOT COPY/USE WITHOUT PERMISSION
|
|
*
|
|
*/
|
|
#include <mix_cc/serialize/deserialize.h>
|
|
#include <mix_cc/serialize/serialize.h>
|
|
#include <mutex>
|
|
#include "shm_header.h"
|
|
namespace RuleStatShm {
|
|
|
|
using namespace ShmHeader;
|
|
|
|
const static std::string dir_path = "/users/dsc/shm";
|
|
const static std::string shm_file = "SerializeMap"; ///<映射文件名
|
|
const double data_size = 500; ///< 数据大小 MB
|
|
|
|
///< key 是string的情况
|
|
typedef std::pair<const char_string, bipc::string> pair_s; ///< key-value
|
|
typedef bipc::node_allocator<pair_s, mapped_segment_manager_t>
|
|
allocator_s; ///<映射文件
|
|
typedef std::less<char_string> less_s;
|
|
typedef bipc::map<char_string, bipc::string, less_s, allocator_s> SerializeMap;
|
|
typedef SerializeMap::iterator SerializeMap_iter_s;
|
|
|
|
namespace {
|
|
bipc::offset_ptr<SerializeMap> SerializeMapPtr =
|
|
obj_mapped_file.find_or_construct<SerializeMap>(shm_file.c_str())(
|
|
less_s(), obj_mapped_file.get_segment_manager());
|
|
std::mutex local_mutext{}; ///<共享锁
|
|
} // namespace
|
|
|
|
///<定义数据操作
|
|
template <typename T>
|
|
struct SerializeMapOperator {
|
|
public:
|
|
template <typename T>
|
|
static bool insert(std::string key, T data) {
|
|
std::lock_guard<std::mutex> guard(local_mutext);
|
|
std::string res = mix_cc::serialize(data);
|
|
if (!res.empty()) {
|
|
try {
|
|
SerializeMapPtr.get()->operator[](key) = res;
|
|
}
|
|
}
|
|
}
|
|
template <typename T>
|
|
static T get_data(std::string key) {
|
|
std::lock_guard<std::mutex> guard(local_mutext);
|
|
auto res = SerializeMapPtr.get()->operator[](key);
|
|
return mix_cc::deserialize(res);
|
|
}
|
|
static bool delete_data(std::string key) {
|
|
if (SerializeMapPtr.get()->find(key) == SerializeMapPtr.get()->end()) {
|
|
return false;
|
|
} else {
|
|
SerializeMapPtr.get()->erase(key);
|
|
return true;
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace RuleStatShm
|