
Hi, I have a class X : public intrusive::list_base_hook<> and would like to embed an additional member bool using the pointer_plus_bit. Can this be achieved using the value_traits somehow, and have the list working 'appropriately'? example code: #include <boost/function/function0.hpp> #include <boost/intrusive/list.hpp> #include <boost/intrusive/pointer_plus_bit.hpp> struct X : public boost::intrusive::list_base_hook<> { template<typename F> X(F const& f) : f_(f) {} boost::function0<void> f_; }; void foo() {} void test_intrusive() { typedef boost::intrusive::list<X> ilist; ilist list; list.push_back(*(new X(&foo))); X& x = list.back(); // How to set/get the bit on x? }; Thanks, Christian

Christian Holmquist wrote:
Hi,
I have a class X : public intrusive::list_base_hook<> and would like to embed an additional member bool using the pointer_plus_bit.
Can this be achieved using the value_traits somehow, and have the list working 'appropriately'?
It can be achieved using value_traits. Just repeat the following example: http://www.boost.org/doc/libs/1_35_0/doc/html/intrusive/value_traits.html#in... but instead of defining this value_traits: //Define the node traits. A single node_traits will be enough. struct simple_node_traits { typedef simple_node node; typedef node * node_ptr; typedef const node * const_node_ptr; static node *get_next(const node *n) { return n->next_; } static void set_next(node *n, node *next) { n->next_ = next; } // ... }; Use this (supposing you want to embed the bit in the next pointer): struct simple_node_traits { // ... static node *get_next(const node *n) { return pointer_plus_bit<node*>::get_pointer(n->next_); } static void set_next(node *n, node *next) { return pointer_plus_bit<node*>::set_pointer(n->next_, next); } //... }; I haven't compiled it, so it might contain errors. To access to the set bit you should define a function that accesses next pointer and extracts the bit, something like: pointer_plus_bit<node*>::set_bit(x.next_, true); bool ret = pointer_plus_bit<node*>::get_bit(x.next_); Take in care that you should make sure that has_pointer_plus_bit<void *, boost::alignment_of<node>::value>::value; is true, otherwise, you won't have space to store the bit, because the pointed type has not even alignment. FYI, I've just erased pointer_plus_bit.hpp and substituted it with a more generic pointer_plus_bits.hpp in Trunk. Now that I see that it's being used, I will need to provide backwards compatibility. I hope it works, Ion

2008/5/15 Ion Gaztañaga <igaztanaga@gmail.com>:
Christian Holmquist wrote:
Hi,
I have a class X : public intrusive::list_base_hook<> and would like to embed an additional member bool using the pointer_plus_bit.
Can this be achieved using the value_traits somehow, and have the list working 'appropriately'?
It can be achieved using value_traits. Just repeat the following example:
http://www.boost.org/doc/libs/1_35_0/doc/html/intrusive/value_traits.html#in...
but instead of defining this value_traits:
//Define the node traits. A single node_traits will be enough. struct simple_node_traits { typedef simple_node node; typedef node * node_ptr; typedef const node * const_node_ptr;
static node *get_next(const node *n) { return n->next_; } static void set_next(node *n, node *next) { n->next_ = next; } // ... };
Use this (supposing you want to embed the bit in the next pointer):
struct simple_node_traits { // ...
static node *get_next(const node *n) { return pointer_plus_bit<node*>::get_pointer(n->next_); }
fails to compile due to n->next_ points to void* and not node*. is it safe to use static_cast<node*>(n->next_) here?
FYI, I've just erased pointer_plus_bit.hpp and substituted it with a more generic pointer_plus_bits.hpp in Trunk. Now that I see that it's being used, I will need to provide backwards compatibility.
Ah well,it's not production code, so to me it's not important with backward compatibility. What I'm trying to do is to build a rollback mechanism that's more robust to exceptions due to failing memory allocations. since i'm doing some optimizations on the way, it just seemed as a good idea using the pointer_plus_bit feature. BI is a great lib by the way, the most inspiring addition to boost in 1.35 (although I've been using it from trunk for quite a while)!
I hope it works,
Ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Christian Holmquist wrote:
fails to compile due to n->next_ points to void* and not node*. is it safe to use static_cast<node*>(n->next_) here?
Sorry for the late reply. Why is n->next_ void*? It should be node*. Anyway, it would be safe to cast it. I attach here the legacy_value example but with an additional pointer_plus_bit trick.
BI is a great lib by the way, the most inspiring addition to boost in 1.35 (although I've been using it from trunk for quite a while)!
Thanks. If you have suggestions to improve the library, please let me know. Regards, Ion
participants (2)
-
Christian Holmquist
-
Ion Gaztañaga