哪些STL端口支持自定义分配器中自定义/智能指针的使用?

时间:2016-08-26 08:12:19

标签: c++ c++11 visual-studio-2015 stl

我想在内存映射文件中继续使用数据(例如unordered_map)进行收集,所以我需要像offset类型的指针来处理(集合可以映射到任何随机地址,所以我需要从映射区域的开头))。我的目标是创建一些大字典,就像我没有必要每次初始化它一样有效地使用它(只是附加/映射到app启动时任何可用的mem位置)。 当我尝试在Visual Studio 2015更新3中实现它时,我收到编译错误,显示集合(例如vector)在内部使用原始指针而不是我的类来表示偏移指针。 我知道boost / interprocess有自己的专用/自定义集合来将数据保存在共享内存(或内存映射文件)中,但我需要可以与任何STL容器一起使用的数据持久化方式。

问题如标题:
有人知道在自定义分配器上支持自定义智能指针的任何STL实现吗?

也许有人做过这样的事情已经取得了成功。在这种情况下,我不确定偏移智能指针的定义是否足够。

更新1 : 下面是我目前的分配器和偏移指针的定义......

分配器:

    #include <memory>
    #include "esoft_offset_pointer.hpp"

    namespace esoft::mem_map_file_allocator
    {
        template<typename T> 
        class mmf_allocator
        {
        public:
            using value_type = T;
            using size_type = size_t;

            using pointer = mmf_offset_pointer<T>;
            using const_pointer = mmf_offset_pointer<const T>; //  T*;
            //using difference_type = ptrdiff_t;

            // rebind allocator to type U
            template <typename U>
            struct rebind {
                typedef mmf_allocator<U> other;
            };

            // constructors and destructor
            mmf_allocator() noexcept {};
            mmf_allocator(const mmf_allocator&) noexcept {};

            template <typename U>
            mmf_allocator(const mmf_allocator<U>&) noexcept {};

            ~mmf_allocator() {};

            pointer allocate(size_type num, std::allocator<void>::const_pointer hint = 0)
            {
                return reinterpret_cast<pointer>(new char[num * sizeof(T)]);
            }

            // deallocate storage p of deleted elements
            void deallocate(pointer p, size_type num)
            {
                delete[] reinterpret_cast<T*>(p);
            }

            //// initialize elements of allocated storage p with values args
            //template <typename U, typename... Args>
            //void construct(U* p, Args&&... args)
            //{

            //}

            //// delete elements of initialized storage p
            //template <typename U>
            //void destroy(U* p)
            //{

            //}
        };
    }

偏移指针:

namespace esoft::mem_map_file_allocator
{
    template<typename T>
    class mmf_offset_pointer
    {
    public:
        operator T*();
        T& operator ++();
        //friend size_t operator -(const mmf_offset_pointer<T> p1, const mmf_offset_pointer<T> p2);
    };
}

用法:

#include <vector>
#include <memory>

#include "esoft_allocator.hpp"

using namespace std;
namespace ea = esoft::mem_map_file_allocator;
using vec_offset_alloc = ea::mmf_allocator<int>;

int main()
{
    vector<int, vec_offset_alloc> vec;

    for (int pos = 10; pos < 20; ++pos)
    {
        vec.push_back(pos);
    }

    int k = vec[4];
    return 0;
}

我还没有编写方法体。我的目标首先是找到方法(实际上是一组方法和运算符)来声明分配器和偏移量指针,例如std :: vector可以接受它。

0 个答案:

没有答案
相关问题