1 | #include <iostream>
|
---|
2 | #include <string>
|
---|
3 | #include <boost/noncopyable.hpp>
|
---|
4 | #include <boost/intrusive/set.hpp>
|
---|
5 |
|
---|
6 | namespace intrusive = boost::intrusive;
|
---|
7 |
|
---|
8 | class object : public boost::noncopyable
|
---|
9 | {
|
---|
10 | public:
|
---|
11 | object()
|
---|
12 | {
|
---|
13 | }
|
---|
14 | virtual ~object()
|
---|
15 | {
|
---|
16 | }
|
---|
17 | };
|
---|
18 |
|
---|
19 |
|
---|
20 | class signal : virtual public object
|
---|
21 | {
|
---|
22 | public:
|
---|
23 | signal()
|
---|
24 | {
|
---|
25 | }
|
---|
26 | virtual ~signal()
|
---|
27 | {
|
---|
28 | }
|
---|
29 | };
|
---|
30 |
|
---|
31 |
|
---|
32 | class record : public signal
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | record()
|
---|
36 | {
|
---|
37 | }
|
---|
38 | virtual ~record()
|
---|
39 | {
|
---|
40 | }
|
---|
41 |
|
---|
42 | public:
|
---|
43 | virtual const std::string& get_buffer() const
|
---|
44 | {
|
---|
45 | return m_buffer;
|
---|
46 | }
|
---|
47 |
|
---|
48 | typedef intrusive::set_member_hook<
|
---|
49 | intrusive::link_mode<intrusive::auto_unlink>
|
---|
50 | > hook;
|
---|
51 | hook m_hook;
|
---|
52 |
|
---|
53 | std::string m_buffer;
|
---|
54 | };
|
---|
55 |
|
---|
56 |
|
---|
57 | template <class T, class M, M (T::*V)>
|
---|
58 | struct member_comparator
|
---|
59 | {
|
---|
60 | bool operator()(const T& t1, const T& t2) const
|
---|
61 | {
|
---|
62 | return (t1.*V) < (t2.*V);
|
---|
63 | }
|
---|
64 | bool operator()(const M& m, const T& t) const
|
---|
65 | {
|
---|
66 | return m < (t.*V);
|
---|
67 | }
|
---|
68 | bool operator()(const T& t, const M& m) const
|
---|
69 | {
|
---|
70 | return (t.*V) < m;
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | typedef member_comparator<
|
---|
75 | record,
|
---|
76 | std::string,
|
---|
77 | &record::m_buffer
|
---|
78 | > record_comparator;
|
---|
79 |
|
---|
80 | typedef intrusive::set<
|
---|
81 | record,
|
---|
82 | intrusive::compare<record_comparator>,
|
---|
83 | intrusive::member_hook<
|
---|
84 | record,
|
---|
85 | record::hook,
|
---|
86 | &record::m_hook
|
---|
87 | >,
|
---|
88 | intrusive::constant_time_size<false>
|
---|
89 | > records
|
---|
90 | ;
|
---|
91 |
|
---|
92 |
|
---|
93 | int main(int argc, char** argv)
|
---|
94 | {
|
---|
95 | union
|
---|
96 | {
|
---|
97 | int32_t as_int;
|
---|
98 | const record::hook record::* ptr_to_member;
|
---|
99 | }
|
---|
100 | sss;
|
---|
101 | sss.ptr_to_member = &record::m_hook;
|
---|
102 |
|
---|
103 | std::cout << "offsets: " << sss.as_int << " and " << offsetof(record,m_hook) << std::endl;
|
---|
104 |
|
---|
105 | records rr;
|
---|
106 |
|
---|
107 | std::string key = "123";
|
---|
108 | records::insert_commit_data icd;
|
---|
109 | std::pair<records::iterator,bool> ir = rr.insert_check(
|
---|
110 | key,
|
---|
111 | record_comparator(),
|
---|
112 | icd
|
---|
113 | );
|
---|
114 |
|
---|
115 | if ( !ir.second )
|
---|
116 | {
|
---|
117 | throw std::exception();
|
---|
118 | }
|
---|
119 |
|
---|
120 | record rec;
|
---|
121 | rec.m_buffer = key;
|
---|
122 | records::iterator i = rr.insert_commit( rec, icd );
|
---|
123 |
|
---|
124 | record* rrr = &(*i);
|
---|
125 |
|
---|
126 | std::cout << "pointers: " << ((void*)rrr) << " and " << ((void*)&rec) << std::endl;
|
---|
127 |
|
---|
128 | return 0;
|
---|
129 | }
|
---|