Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Using function hooks

A programmer might find that base or member hooks are not flexible enough in some situations. In some applications it would be optimal to put a hook deep inside a member of a class or just outside the class. Boost.Intrusive has an easy option to allow such cases: function_hook.

This option is similar to member_hook or base_hook, but the programmer can specify a function object that tells the container how to obtain a hook from a value and vice versa. The programmer just needs to define the following function object:

//This functor converts between value_type and a hook_type
struct Functor
{
   //Required types
   typedef /*impl-defined*/      hook_type;
   typedef /*impl-defined*/      hook_ptr;
   typedef /*impl-defined*/      const_hook_ptr;
   typedef /*impl-defined*/      value_type;
   typedef /*impl-defined*/      pointer;
   typedef /*impl-defined*/      const_pointer;
   //Required static functions
   static hook_ptr to_hook_ptr (value_type &value);
   static const_hook_ptr to_hook_ptr(const value_type &value);
   static pointer to_value_ptr(hook_ptr n);
   static const_pointer to_value_ptr(const_hook_ptr n);
};

Converting from values to hooks is generally easy, since most hooks are in practice members or base classes of class data members. The inverse operation is a bit more complicated, but Boost.Intrusive offers a bit of help with the function get_parent_from_member, which allows easy conversions from the address of a data member to the address of the parent holding that member. Let's see a little example of function_hook:

#include <boost/intrusive/