Search code examples
c++boostintrusive-containers

how to convert slist 'node_ptr' to my own node type


I've declared the following node which inherits from boost::intrusive::slist_base_hook<> :

class InputBufferSglNode : public boost::intrusive::slist_base_hook<>

declaration of the list which contains these nodes :

class InputBufferSglList : public boost::intrusive::slist<InputBufferSglNode, boost::intrusive::cache_last<true>>

I want to get the root node from InputBufferSglList member function, so I tried to do :
InputBufferSglNode* node = this->get_root_node();
but I get an error :

error: cannot initialize a variable of type 'InputBufferSglNode *' with an rvalue of type 'node_ptr' (aka 'boost::intrusive::slist_node<void *> *')

Should I cast node_ptr to InputBufferSglNode* ? which casting should it be ?


Solution

  • get_root_node is not part of the documented API.

    Are you looking for

    InputBufferSglNode& node = *this->begin();
    

    In case you're interested in the reverse:


    UPDATE:

    I've read some more in the documentation, and there is a way in which you can do what you want, by using the derived value_traits for your list type:

    InputBufferSglNode* node = 
          InputBufferSglList::value_traits::to_value_ptr(list.get_root_node());
    

    This uses the ValueTraits interface that underlies all intrusive containers.

    For all usual node default hooks it will result in a bit of offset arithmetic (to get from the member address to the node address, e.g.). But in practice it might involved casts up and down the class hierarchical (especially when virtual base(s) are involved).

    For any trivial_value_traits both to_value_ptr and to_node_ptr would be the identity function.