1 | /* Used in Boost.MultiIndex tests. |
2 | * |
3 | * Copyright 2003-2018 Joaquin M Lopez Munoz. |
4 | * Distributed under the Boost Software License, Version 1.0. |
5 | * (See accompanying file LICENSE_1_0.txt or copy at |
6 | * http://www.boost.org/LICENSE_1_0.txt) |
7 | * |
8 | * See http://www.boost.org/libs/multi_index for library home page. |
9 | */ |
10 | |
11 | #ifndef BOOST_MULTI_INDEX_TEST_EMPLOYEE_HPP |
12 | #define BOOST_MULTI_INDEX_TEST_EMPLOYEE_HPP |
13 | |
14 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ |
15 | #include <boost/move/core.hpp> |
16 | #include <boost/move/utility_core.hpp> |
17 | #include <boost/mpl/vector.hpp> |
18 | #include <boost/multi_index_container.hpp> |
19 | #include <boost/multi_index/hashed_index.hpp> |
20 | #include <boost/multi_index/identity.hpp> |
21 | #include <boost/multi_index/member.hpp> |
22 | #include <boost/multi_index/ordered_index.hpp> |
23 | #include <boost/multi_index/random_access_index.hpp> |
24 | #include <boost/multi_index/ranked_index.hpp> |
25 | #include <boost/multi_index/sequenced_index.hpp> |
26 | #include <ostream> |
27 | #include <string> |
28 | #include "non_std_allocator.hpp" |
29 | |
30 | struct employee |
31 | { |
32 | int id; |
33 | std::string name; |
34 | int age; |
35 | int ssn; |
36 | |
37 | employee(int id_,std::string name_,int age_,int ssn_): |
38 | id(id_),name(name_),age(age_),ssn(ssn_) |
39 | {} |
40 | |
41 | employee(const employee& x): |
42 | id(x.id),name(x.name),age(x.age),ssn(x.ssn) |
43 | {} |
44 | |
45 | employee(BOOST_RV_REF(employee) x): |
46 | id(x.id),name(boost::move(t&: x.name)),age(x.age),ssn(x.ssn) |
47 | {} |
48 | |
49 | employee& operator=(BOOST_COPY_ASSIGN_REF(employee) x) |
50 | { |
51 | id=x.id; |
52 | name=x.name; |
53 | age=x.age; |
54 | ssn=x.ssn; |
55 | return *this; |
56 | }; |
57 | |
58 | employee& operator=(BOOST_RV_REF(employee) x) |
59 | { |
60 | id=x.id; |
61 | name=boost::move(t&: x.name); |
62 | age=x.age; |
63 | ssn=x.ssn; |
64 | return *this; |
65 | } |
66 | |
67 | bool operator==(const employee& x)const |
68 | { |
69 | return id==x.id&&name==x.name&&age==x.age; |
70 | } |
71 | |
72 | bool operator<(const employee& x)const |
73 | { |
74 | return id<x.id; |
75 | } |
76 | |
77 | bool operator!=(const employee& x)const{return !(*this==x);} |
78 | bool operator> (const employee& x)const{return x<*this;} |
79 | bool operator>=(const employee& x)const{return !(*this<x);} |
80 | bool operator<=(const employee& x)const{return !(x<*this);} |
81 | |
82 | struct comp_id |
83 | { |
84 | bool operator()(int x,const employee& e2)const{return x<e2.id;} |
85 | bool operator()(const employee& e1,int x)const{return e1.id<x;} |
86 | }; |
87 | |
88 | friend std::ostream& operator<<(std::ostream& os,const employee& e) |
89 | { |
90 | os<<e.id<<" " <<e.name<<" " <<e.age<<std::endl; |
91 | return os; |
92 | } |
93 | |
94 | private: |
95 | BOOST_COPYABLE_AND_MOVABLE(employee) |
96 | }; |
97 | |
98 | struct name{}; |
99 | struct by_name{}; |
100 | struct age{}; |
101 | struct as_inserted{}; |
102 | struct ssn{}; |
103 | struct randomly{}; |
104 | |
105 | struct employee_set_indices: |
106 | boost::mpl::vector< |
107 | boost::multi_index::ordered_unique< |
108 | boost::multi_index::identity<employee> >, |
109 | boost::multi_index::hashed_non_unique< |
110 | boost::multi_index::tag<name,by_name>, |
111 | BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>, |
112 | boost::multi_index::ranked_non_unique< |
113 | boost::multi_index::tag<age>, |
114 | BOOST_MULTI_INDEX_MEMBER(employee,int,age)>, |
115 | boost::multi_index::sequenced< |
116 | boost::multi_index::tag<as_inserted> >, |
117 | boost::multi_index::hashed_unique< |
118 | boost::multi_index::tag<ssn>, |
119 | BOOST_MULTI_INDEX_MEMBER(employee,int,ssn)>, |
120 | boost::multi_index::random_access< |
121 | boost::multi_index::tag<randomly> > > |
122 | {}; |
123 | |
124 | typedef |
125 | boost::multi_index::multi_index_container< |
126 | employee, |
127 | employee_set_indices, |
128 | non_std_allocator<employee> > employee_set; |
129 | |
130 | #if defined(BOOST_NO_MEMBER_TEMPLATES) |
131 | typedef boost::multi_index::nth_index< |
132 | employee_set,1>::type employee_set_by_name; |
133 | #else |
134 | typedef employee_set::nth_index<1>::type employee_set_by_name; |
135 | #endif |
136 | |
137 | typedef boost::multi_index::index< |
138 | employee_set,age>::type employee_set_by_age; |
139 | typedef boost::multi_index::index< |
140 | employee_set,as_inserted>::type employee_set_as_inserted; |
141 | typedef boost::multi_index::index< |
142 | employee_set,ssn>::type employee_set_by_ssn; |
143 | |
144 | #if defined(BOOST_NO_MEMBER_TEMPLATES) |
145 | typedef boost::multi_index::index< |
146 | employee_set,randomly>::type employee_set_randomly; |
147 | #else |
148 | typedef employee_set::index< |
149 | randomly>::type employee_set_randomly; |
150 | #endif |
151 | |
152 | #endif |
153 | |