eis/mix_cc/shm/stl_builder.h

71 lines
2.0 KiB
C
Raw Normal View History

/**
* @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