eis/mix_cc/shm/stl_builder.h

71 lines
2.0 KiB
C++

/**
* @file mix_cc/shm/stl_builder.h
* @brief 共享内存stl容器构造器
* @author Cat (null.null.null@qq.com)
* @version 0.1
* @date 2021-05-07
*
* Copyright: Baosight Co. Ltd.
* DO NOT COPY/USE WITHOUT PERMISSION
*
*/
#pragma once
#include <mix_cc/shm/utility.h>
#include <string>
namespace mix_cc {
namespace shm {
/**
* @brief 共享内存stl容器构造器基类
* @tparam ContainerType
* @tparam T
* @tparam Segment
*/
template <class ContainerType, class T,
class Segment = bip::managed_mapped_file>
class StlBuilder {
public:
typedef ScopedShmAlloc<T, Segment> Allocator;
typedef ContainerType Type;
public:
/**
* @brief 创建共享stl容器
* @param shm 共享内存块
* @param container_name 容器名
* @return bip::offset_ptr<ContainerType>&
*/
static bip::offset_ptr<ContainerType>& construct(
Segment* const shm, const std::string& container_name) {
return shm->template construct<ContainerType>(container_name.c_str())(
Allocator(shm->get_segment_manager()));
}
/**
* @brief 在指定内存块上查找共享内存容器
* @param shm 共享内存块
* @param container_name 容器名
* @return bip::offset_ptr<ContainerType>&
*/
static bip::offset_ptr<ContainerType>& find(
Segment* const shm, const std::string& container_name) {
return shm->template find<ContainerType>(container_name.c_str())(
Allocator(shm->get_segment_manager()));
}
/**
* @brief 在指定内存块上查找共享内存容器,如果找不到,就创建新的容器
* @param shm 共享内存块
* @param container_name 容器名
* @return bip::offset_ptr<ContainerType>&
*/
static bip::offset_ptr<ContainerType> find_or_construct(
Segment* const shm, const std::string& container_name) {
return shm->template find_or_construct<ContainerType>(
container_name.c_str())(Allocator(shm->get_segment_manager()));
}
};
} // namespace shm
} // namespace mix_cc