具有通用指针类型的自链接类的类设计

发布于 2024-10-31 04:35:21 字数 805 浏览 0 评论 0原文


我目前正在修改一个复杂的类,该类的节点指向自身,就像链接列表或图形一样。我希望它通过 boost::interprocess 函数在共享内存中使用。现在我正在寻找重新设计它的方法,使其保持通用并且涉及最少的更改。

template< class T >  
class node {  
  public:  
    T data;  
    node* prev;  
    node* next;  
};

重新设计应使用 boost::interprocess::allocator 分配器,以便隐式使用 boost::interprocess::offset_ptr 类型的相对智能指针。我认为它应该涉及第二个模板参数,因为

template< class T, class alloc_type = std::allocator< node< T > > >  
class node {
  public:  
    T data;  
    typename alloc_type::pointer prev;  
    typename alloc_type::pointer next;  
};

就像引用一样,由于循环依赖关系,该参数当然不起作用。
我希望能够从 C++ 类模板专家那里获得一些关于实现它的最佳方法的帮助。我查看了启用共享内存的增强容器,但它们正在以相当复杂的方式解决它,涉及多个外部类。

约翰

I am currently modifying a complex class that has nodes pointing to themselves just like linked lists or graphs. I want it to be used in shared memory using boost::interprocess functions. Now I am looking for the way to redesign it such that it stays generic and involves least changes.

template< class T >  
class node {  
  public:  
    T data;  
    node* prev;  
    node* next;  
};

The redesign should make use of the boost::interprocess::allocator allocator in order to implicitly use the relative smart pointers of type boost::interprocess::offset_ptr. I thought it should involve a second template parameter like

template< class T, class alloc_type = std::allocator< node< T > > >  
class node {
  public:  
    T data;  
    typename alloc_type::pointer prev;  
    typename alloc_type::pointer next;  
};

which of course doesn't work due to cyclic dependencies just as with references.
I hope a can get some help from a C++ class template pro on the best way to implement it. I had a look at the boost shared memory enabled containers but they are solving it in a rather complicated manner that involves several external classes.

Joh

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

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

发布评论

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

评论(2

寄居者 2024-11-07 04:35:21

由于循环依赖,这当然不起作用,就像引用一样。

是的,这行不通,因为类型 node 正在定义,但尚未完成,但您将其作为类型参数传递给 allocator。因此问题来了!

另外,node 是一个类模板,但是当您将其传递给allocator 时,您没有为node 提供类型参数。但不用担心,即使你通过了,它也不会起作用,因为类型 node 尚未完成(正如我之前所说)。


除此之外,您还有另一个问题,

alloc_type::pointer prev;  
alloc_type::pointer next;  

这里您需要 typename 作为

typename alloc_type::pointer prev;  
typename alloc_type::pointer next;  

which of course doesn't work due to cyclic dependencies just as with references.

Yes, that will not work, because the type node is being defined and it's not yet complete, but you're passing it to allocator as type parameter. Hence the problem!

Also, node is a class template, but when you're passing it to allocator you're not providing type argument for node. But dont worry, even if you pass, it will not work since type node is not yet complete (as I said before).


Besides that you've another problem,

alloc_type::pointer prev;  
alloc_type::pointer next;  

Here you need typename as

typename alloc_type::pointer prev;  
typename alloc_type::pointer next;  
ˇ宁静的妩媚 2024-11-07 04:35:21

在我看来,分配器类属于容器,而不是元素。我意识到,对于像您这样的侵入式容器,它们可以是相同的。但是,我们可以把它排除掉吗?

怎么样:

#include <memory>

template <class T, class alloc_type >
struct nodePtr {
    typedef typename alloc_type::pointer pointer;
    typedef alloc_type allocator;
};


template< class T >
class node {
  public:
    typedef nodePtr<node<T>, std::allocator<node<T> > > pointer;
    // OR: typedef nodePtr<node<T>, my_allocator<node<T> > > pointer;
    T data;
    pointer prev;
    pointer next;
};

node<int> n;

当然,我不知道如何将 std::allocator<> 传递给 node<>,但也许你不需要它。您介意在类节点中间指定分配器的名称吗?

或者,我们可以让您的分配器成为 nodePtr 的默认分配器?

// untested
template <class T> class node;
template <class T, class alloc_type = my_allocator<node<T> > >
class nodePtr { /* ... */ };
template <class T> class node {
    public: typename nodePtr<T> prev;
}

It seems to me that the allocator class belongs to the container, not the element. I realize that, for intrusive containers like yours, they can be the same. But, can we factor it back out?

How about:

#include <memory>

template <class T, class alloc_type >
struct nodePtr {
    typedef typename alloc_type::pointer pointer;
    typedef alloc_type allocator;
};


template< class T >
class node {
  public:
    typedef nodePtr<node<T>, std::allocator<node<T> > > pointer;
    // OR: typedef nodePtr<node<T>, my_allocator<node<T> > > pointer;
    T data;
    pointer prev;
    pointer next;
};

node<int> n;

Of course, I can't figure out how to pass std::allocator<> to node<>, but maybe you don't need that. Do you mind specifying your allocator's name in the middle of class node?

Or maybe, we could make your allocator the default allocator for nodePtr?

// untested
template <class T> class node;
template <class T, class alloc_type = my_allocator<node<T> > >
class nodePtr { /* ... */ };
template <class T> class node {
    public: typename nodePtr<T> prev;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文