| 1 |
|
|---|
| 2 | #include <iostream>
|
|---|
| 3 | #include <string>
|
|---|
| 4 | #include <boost/noncopyable.hpp>
|
|---|
| 5 | #include <boost/intrusive/list.hpp>
|
|---|
| 6 | #include <boost/intrusive/set.hpp>
|
|---|
| 7 |
|
|---|
| 8 | namespace intrusive = boost::intrusive;
|
|---|
| 9 |
|
|---|
| 10 | class object : public boost::noncopyable
|
|---|
| 11 | {
|
|---|
| 12 | public:
|
|---|
| 13 | object()
|
|---|
| 14 | {
|
|---|
| 15 | }
|
|---|
| 16 | virtual ~object()
|
|---|
| 17 | {
|
|---|
| 18 | }
|
|---|
| 19 | };
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | class signal : virtual public object
|
|---|
| 23 | {
|
|---|
| 24 | public:
|
|---|
| 25 | signal()
|
|---|
| 26 | {
|
|---|
| 27 | }
|
|---|
| 28 | virtual ~signal()
|
|---|
| 29 | {
|
|---|
| 30 | }
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | class set_item : public signal
|
|---|
| 35 | {
|
|---|
| 36 | public:
|
|---|
| 37 | set_item()
|
|---|
| 38 | {
|
|---|
| 39 | }
|
|---|
| 40 | virtual ~set_item()
|
|---|
| 41 | {
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public:
|
|---|
| 45 | virtual const std::string& get_buffer() const
|
|---|
| 46 | {
|
|---|
| 47 | return m_buffer;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | typedef intrusive::set_member_hook<
|
|---|
| 51 | intrusive::link_mode<intrusive::auto_unlink>
|
|---|
| 52 | > hook;
|
|---|
| 53 | hook m_hook;
|
|---|
| 54 |
|
|---|
| 55 | std::string m_buffer;
|
|---|
| 56 | };
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | template <class T, class M, M (T::*V)>
|
|---|
| 60 | struct member_comparator
|
|---|
| 61 | {
|
|---|
| 62 | bool operator()(const T& t1, const T& t2) const
|
|---|
| 63 | {
|
|---|
| 64 | return (t1.*V) < (t2.*V);
|
|---|
| 65 | }
|
|---|
| 66 | bool operator()(const M& m, const T& t) const
|
|---|
| 67 | {
|
|---|
| 68 | return m < (t.*V);
|
|---|
| 69 | }
|
|---|
| 70 | bool operator()(const T& t, const M& m) const
|
|---|
| 71 | {
|
|---|
| 72 | return (t.*V) < m;
|
|---|
| 73 | }
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | class list_item : virtual public object
|
|---|
| 77 | {
|
|---|
| 78 | public:
|
|---|
| 79 | list_item()
|
|---|
| 80 | {
|
|---|
| 81 | }
|
|---|
| 82 | virtual ~list_item()
|
|---|
| 83 | {
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | virtual void f()
|
|---|
| 87 | {
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | typedef intrusive::list_member_hook<
|
|---|
| 91 | intrusive::link_mode<intrusive::auto_unlink>
|
|---|
| 92 | > hook;
|
|---|
| 93 | hook m_hook;
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | int main(int argc, char** argv)
|
|---|
| 98 | {
|
|---|
| 99 | {
|
|---|
| 100 | typedef member_comparator<
|
|---|
| 101 | set_item,
|
|---|
| 102 | std::string,
|
|---|
| 103 | &set_item::m_buffer
|
|---|
| 104 | > set_item_comparator;
|
|---|
| 105 |
|
|---|
| 106 | typedef intrusive::set<
|
|---|
| 107 | set_item,
|
|---|
| 108 | intrusive::compare<set_item_comparator>,
|
|---|
| 109 | intrusive::member_hook<
|
|---|
| 110 | set_item,
|
|---|
| 111 | set_item::hook,
|
|---|
| 112 | &set_item::m_hook
|
|---|
| 113 | >,
|
|---|
| 114 | intrusive::constant_time_size<false>
|
|---|
| 115 | > set_items
|
|---|
| 116 | ;
|
|---|
| 117 |
|
|---|
| 118 | union
|
|---|
| 119 | {
|
|---|
| 120 | int32_t as_int;
|
|---|
| 121 | const set_item::hook set_item::* ptr_to_member;
|
|---|
| 122 | }
|
|---|
| 123 | sss;
|
|---|
| 124 | sss.ptr_to_member = &set_item::m_hook;
|
|---|
| 125 |
|
|---|
| 126 | std::cout << "set offsets: " << sss.as_int << " and " << offsetof(set_item,m_hook) << std::endl;
|
|---|
| 127 |
|
|---|
| 128 | set_items rr;
|
|---|
| 129 |
|
|---|
| 130 | std::string key = "123";
|
|---|
| 131 | set_items::insert_commit_data icd;
|
|---|
| 132 | std::pair<set_items::iterator,bool> ir = rr.insert_check(
|
|---|
| 133 | key,
|
|---|
| 134 | set_item_comparator(),
|
|---|
| 135 | icd
|
|---|
| 136 | );
|
|---|
| 137 |
|
|---|
| 138 | if ( !ir.second )
|
|---|
| 139 | {
|
|---|
| 140 | throw std::exception();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | set_item rec;
|
|---|
| 144 | rec.m_buffer = key;
|
|---|
| 145 | set_items::iterator i = rr.insert_commit( rec, icd );
|
|---|
| 146 |
|
|---|
| 147 | set_item* rrr = &(*i);
|
|---|
| 148 |
|
|---|
| 149 | std::cout << "set pointers: " << ((void*)rrr) << " and " << ((void*)&rec) << std::endl;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | {
|
|---|
| 153 | typedef intrusive::list<
|
|---|
| 154 | list_item,
|
|---|
| 155 | intrusive::member_hook<
|
|---|
| 156 | list_item,
|
|---|
| 157 | list_item::hook,
|
|---|
| 158 | &list_item::m_hook
|
|---|
| 159 | >,
|
|---|
| 160 | intrusive::constant_time_size<false>
|
|---|
| 161 | > list_items
|
|---|
| 162 | ;
|
|---|
| 163 |
|
|---|
| 164 | union
|
|---|
| 165 | {
|
|---|
| 166 | int32_t as_int;
|
|---|
| 167 | const list_item::hook list_item::* ptr_to_member;
|
|---|
| 168 | }
|
|---|
| 169 | sss;
|
|---|
| 170 | sss.ptr_to_member = &list_item::m_hook;
|
|---|
| 171 |
|
|---|
| 172 | std::cout << "list offsets: " << sss.as_int << " and " << offsetof(list_item,m_hook) << std::endl;
|
|---|
| 173 |
|
|---|
| 174 | list_items rr;
|
|---|
| 175 |
|
|---|
| 176 | list_item rec;
|
|---|
| 177 | rr.push_back( rec );
|
|---|
| 178 |
|
|---|
| 179 | list_item* rrr = &rr.front();
|
|---|
| 180 |
|
|---|
| 181 | std::cout << "list pointers: " << ((void*)rrr) << " and " << ((void*)&rec) << std::endl;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | return 0;
|
|---|
| 185 | }
|
|---|