• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2010-2013
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 //    (See accompanying file LICENSE_1_0.txt or copy at
7 //          http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12 //[doc_function_hooks
13 #include <boost/intrusive/list.hpp>
14 #include <boost/intrusive/parent_from_member.hpp>
15 
16 using namespace boost::intrusive;
17 
18 struct MyClass
19 {
20    int dummy;
21    //This internal type has a member hook
22    struct InnerNode
23    {
24       int dummy;
25       list_member_hook<> hook;
26    } inner;
27 };
28 
29 //This functor converts between MyClass and InnerNode's member hook
30 struct Functor
31 {
32    //Required types
33    typedef list_member_hook<>    hook_type;
34    typedef hook_type*            hook_ptr;
35    typedef const hook_type*      const_hook_ptr;
36    typedef MyClass               value_type;
37    typedef value_type*           pointer;
38    typedef const value_type*     const_pointer;
39 
40    //Required static functions
to_hook_ptrFunctor41    static hook_ptr to_hook_ptr (value_type &value)
42       {  return &value.inner.hook; }
to_hook_ptrFunctor43    static const_hook_ptr to_hook_ptr(const value_type &value)
44       {  return &value.inner.hook; }
to_value_ptrFunctor45    static pointer to_value_ptr(hook_ptr n)
46    {
47       return get_parent_from_member<MyClass>
48          (get_parent_from_member<MyClass::InnerNode>(n, &MyClass::InnerNode::hook)
49          ,&MyClass::inner
50       );
51    }
to_value_ptrFunctor52    static const_pointer to_value_ptr(const_hook_ptr n)
53    {
54       return get_parent_from_member<MyClass>
55          (get_parent_from_member<MyClass::InnerNode>(n, &MyClass::InnerNode::hook)
56          ,&MyClass::inner
57       );
58    }
59 };
60 
61 //Define a list that will use the hook accessed through the function object
62 typedef list< MyClass, function_hook< Functor> >  List;
63 
main()64 int main()
65 {
66    MyClass n;
67    List l;
68    //Insert the node in both lists
69    l.insert(l.begin(), n);
70    assert(l.size() == 1);
71    return 0;
72 }
73 //]
74