#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 #include #include #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 pair_s; ///< key-value typedef bipc::node_allocator allocator_s; ///<映射文件 typedef std::less less_s; typedef bipc::map SerializeMap; typedef SerializeMap::iterator SerializeMap_iter_s; namespace { bipc::offset_ptr SerializeMapPtr = obj_mapped_file.find_or_construct(shm_file.c_str())( less_s(), obj_mapped_file.get_segment_manager()); std::mutex local_mutext{}; ///<共享锁 } // namespace ///<定义数据操作 template struct SerializeMapOperator { public: template static bool insert(std::string key, T data) { std::lock_guard guard(local_mutext); std::string res = mix_cc::serialize(data); if (!res.empty()) { try { SerializeMapPtr.get()->operator[](key) = res; } } } template static T get_data(std::string key) { std::lock_guard 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