C++分配器类型参数的设计模式

发布于 2024-11-15 00:23:02 字数 3151 浏览 1 评论 0原文

C++03 标准库在将类型传递给旨在作为分配器的类时使用简单的模板类型参数。这是可能的,因为模板在 C++ 中的工作方式。但是,它并不是很简单,您可能不知道类型定义到底应该是什么样子 - 特别是在非标准类型的情况下。

我认为使用适配器类可能是个好主意。我创建了一个示例来向您展示我的意思:

#ifndef HPP_ALLOCATOR_ADAPTOR_INCLUDED
#define HPP_ALLOCATOR_ADAPTOR_INCLUDED


#include <memory>


template<typename T>
struct allocator_traits;

template<typename T, class allocator_type = std::allocator<T>>
class allocator_adaptor;


template<>
struct allocator_traits<void>
{
    typedef std::allocator<void>::const_pointer const_pointer;
    typedef std::allocator<void>::pointer       pointer;
    typedef std::allocator<void>::value_type    value_type;
};

template<typename T>
struct allocator_traits
{
    typedef typename std::allocator<T>::const_pointer   const_pointer;
    typedef typename std::allocator<T>::const_reference const_reference;
    typedef typename std::allocator<T>::difference_type difference_type;
    typedef typename std::allocator<T>::pointer         pointer;
    typedef typename std::allocator<T>::reference       reference;
    typedef typename std::allocator<T>::size_type       size_type;
    typedef typename std::allocator<T>::value_type      value_type;
};


template<class allocator_type>
class allocator_adaptor<void, allocator_type>
    : public allocator_traits<void>
{
public:
    template<typename U> struct rebind { typedef allocator_adaptor<U, allocator_type> other; };
};

template<typename T, class allocator_type>
class allocator_adaptor
    : public allocator_traits<T>
{
private:
    allocator_type m_impl;

public:
    template<typename U> struct rebind { typedef allocator_adaptor<U, allocator_type> other; };

    allocator_adaptor() throw() /*noexcept*/;
    allocator_adaptor(allocator_adaptor const&) throw() /*noexcept*/;
    allocator_adaptor(allocator_type const&) throw() /*noexcept*/;
    template<typename U> allocator_adaptor(allocator_adaptor<U, allocator_type> const&) throw() /*noexcept*/;
    ~allocator_adaptor() throw();

    pointer       address(reference x) const /*noexcept*/;
    const_pointer address(const_reference x) const /*noexcept*/;

    pointer   allocate  (size_type, allocator_traits<void>::const_pointer hint = 0);
    void      deallocate(pointer p, size_type n) /*noexcept*/;
    size_type max_size  () const throw() /*noexcept*/;

    template<class U, typename... argument_types> void construct(U* p, argument_types&&... args);
    template<class U> void destroy(U* p);
};


#endif /* HPP_ALLOCATOR_ADAPTOR_INCLUDED */

实现应该是显而易见的。 这是一些使用示例。

template<class allocator_type>
int max_size(allocator_type const& alloc)
{
    // we don't know what kind of max_szie function will be called.
    return alloc.max_size();
}

template<typename T>
int max_size(allocator_adaptor<T> const& alloc)
{
    // we know what kind of max_size function will be called.
    return alloc.max_size();
}

与通常的方式相比,这是否有所改进?

The C++03 standard library uses simple template type arguments when passing a type to a class which is meant to be an allocator. This is possible because of how templates work in C++. However, it isn't very straightforward and you might don't know what exactly the type definition should look like - especially in case of non standard types.

I thought it might be a good idea to use adaptor classes instread. I've created an example to show you what I mean:

#ifndef HPP_ALLOCATOR_ADAPTOR_INCLUDED
#define HPP_ALLOCATOR_ADAPTOR_INCLUDED


#include <memory>


template<typename T>
struct allocator_traits;

template<typename T, class allocator_type = std::allocator<T>>
class allocator_adaptor;


template<>
struct allocator_traits<void>
{
    typedef std::allocator<void>::const_pointer const_pointer;
    typedef std::allocator<void>::pointer       pointer;
    typedef std::allocator<void>::value_type    value_type;
};

template<typename T>
struct allocator_traits
{
    typedef typename std::allocator<T>::const_pointer   const_pointer;
    typedef typename std::allocator<T>::const_reference const_reference;
    typedef typename std::allocator<T>::difference_type difference_type;
    typedef typename std::allocator<T>::pointer         pointer;
    typedef typename std::allocator<T>::reference       reference;
    typedef typename std::allocator<T>::size_type       size_type;
    typedef typename std::allocator<T>::value_type      value_type;
};


template<class allocator_type>
class allocator_adaptor<void, allocator_type>
    : public allocator_traits<void>
{
public:
    template<typename U> struct rebind { typedef allocator_adaptor<U, allocator_type> other; };
};

template<typename T, class allocator_type>
class allocator_adaptor
    : public allocator_traits<T>
{
private:
    allocator_type m_impl;

public:
    template<typename U> struct rebind { typedef allocator_adaptor<U, allocator_type> other; };

    allocator_adaptor() throw() /*noexcept*/;
    allocator_adaptor(allocator_adaptor const&) throw() /*noexcept*/;
    allocator_adaptor(allocator_type const&) throw() /*noexcept*/;
    template<typename U> allocator_adaptor(allocator_adaptor<U, allocator_type> const&) throw() /*noexcept*/;
    ~allocator_adaptor() throw();

    pointer       address(reference x) const /*noexcept*/;
    const_pointer address(const_reference x) const /*noexcept*/;

    pointer   allocate  (size_type, allocator_traits<void>::const_pointer hint = 0);
    void      deallocate(pointer p, size_type n) /*noexcept*/;
    size_type max_size  () const throw() /*noexcept*/;

    template<class U, typename... argument_types> void construct(U* p, argument_types&&... args);
    template<class U> void destroy(U* p);
};


#endif /* HPP_ALLOCATOR_ADAPTOR_INCLUDED */

The implemention should be obvious.
Here's some usage example.

template<class allocator_type>
int max_size(allocator_type const& alloc)
{
    // we don't know what kind of max_szie function will be called.
    return alloc.max_size();
}

template<typename T>
int max_size(allocator_adaptor<T> const& alloc)
{
    // we know what kind of max_size function will be called.
    return alloc.max_size();
}

is this an improvement compared to the usual way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹微笑 2024-11-22 00:23:02

实际上,您在这里的目的是引入一个基于可变参数的构造成员,它允许您编写而不是:

typedef std::allocator<T> my_alloc;
my_alloc alloc;
my_alloc::pointer p = alloc.allocate(10);
alloc::construct(p, T(param1, param2, param3));
alloc::construct(p+1, T(param1, param2, param3));
//...

一些更简单的形式:

alloc::construct(p, param1, param2, param3);
alloc::construct(p+1, param1, param2, param3);

这似乎是一个不错的功能。另一方面,您移动了所有初始化参数,这将禁止 p+1 对象的正确初始化。如果我想对多个对象的相同参数重复初始化怎么办?我认为你当前的方法将会失败(不幸的是不是在编译时)。

Actually your point here is to introduce a construct member which is based on variadic arguments and allows you to write instead of:

typedef std::allocator<T> my_alloc;
my_alloc alloc;
my_alloc::pointer p = alloc.allocate(10);
alloc::construct(p, T(param1, param2, param3));
alloc::construct(p+1, T(param1, param2, param3));
//...

some easier form:

alloc::construct(p, param1, param2, param3);
alloc::construct(p+1, param1, param2, param3);

This seems to be a nice feature. On the other hand, you move all the initialization parameters, which will prohibit the proper initialization of the p+1 object. What if I want to repeat the initialization for the same parameters for multiple objects. I think your current approach will fail (and unfortunately not at compile-time).

挽容 2024-11-22 00:23:02

对我来说看起来不错...
如果能解释一下你的设计何时会比通常的方式更有优势,那就太好了。一个例子对我很有帮助

“但是,这并不是很
简单明了,但你可能不会
知道类型定义到底是什么
应该看起来像“

与 std::allocator 相比,您的实现如何改进类型定义部分,请解释

looks good to me...
it would be nice to have an explanation of when your design will have an edge on the usual way. an example will be great to help me

"However, it isn't very
straightforward and you might don't
know what exactly the type definition
should look like"

how does your implementation improves upon the -type definition- part as compared to the std::allocator, please explain

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文