boost::container::map::operator[] default-constructs the mapped_type
when the key doesn't exist. TaskRecord lacked a default constructor
after the e21b2af refactor (only had an allocator-arg constructor).
Added a static vec_allocator_f (matching RuleStatShm.h pattern) and a
default constructor that initializes data_record with it.
49 lines
1.5 KiB
C++
49 lines
1.5 KiB
C++
#pragma once
|
||
/**
|
||
* @file TaskData.h
|
||
* @brief task进程,缓存数据(动态vector,按需分配)
|
||
* @author your name (you@domain.com)
|
||
* @version 0.2
|
||
* @date 2024-02-27
|
||
*
|
||
* Copyright: Baosight Co. Ltd.
|
||
* DO NOT COPY/USE WITHOUT PERMISSION
|
||
*/
|
||
#include <mix_cc/shm.h>
|
||
namespace TaskShm {
|
||
namespace SHM = mix_cc::shm;
|
||
namespace bipc = mix_cc::shm::bip;
|
||
|
||
const static std::string dir_path = "/users/dsc/shm";
|
||
const static std::string shm_file = "TaskData"; ///<映射文件名
|
||
const double data_size = 10; ///< 数据大小 MB(按需动态)
|
||
|
||
static bipc::managed_mapped_file
|
||
obj_mapped_file(bipc::open_or_create,
|
||
(dir_path + "/" + shm_file + "_boost.mmap").c_str(),
|
||
mix_cc::data_size::MB(data_size));
|
||
|
||
typedef bipc::managed_mapped_file::segment_manager segment_manager_t;
|
||
typedef bipc::node_allocator<float, segment_manager_t> vec_allocator_f;
|
||
typedef boost::container::vector<float, vec_allocator_f> shm_vector_f;
|
||
|
||
static vec_allocator_f
|
||
default_allocator(obj_mapped_file.get_segment_manager());
|
||
|
||
struct TaskRecord {
|
||
shm_vector_f data_record;
|
||
|
||
TaskRecord()
|
||
: data_record(default_allocator) {}
|
||
|
||
TaskRecord(const bipc::node_allocator<void, segment_manager_t> &alloc)
|
||
: data_record(alloc) {}
|
||
};
|
||
|
||
/*单例 map 记录task数据项*/
|
||
const static bipc::offset_ptr<SHM::Map<int, TaskRecord>> TaskRecordPtr =
|
||
SHM::MapBuilder<int, TaskRecord>::find_or_construct(&obj_mapped_file,
|
||
shm_file);
|
||
|
||
} // namespace TaskShm
|