employee.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef BOOST_MULTI_INDEX_TEST_EMPLOYEE_HPP
  11. #define BOOST_MULTI_INDEX_TEST_EMPLOYEE_HPP
  12. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  13. #include <boost/move/core.hpp>
  14. #include <boost/move/utility_core.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #include <boost/multi_index_container.hpp>
  17. #include <boost/multi_index/hashed_index.hpp>
  18. #include <boost/multi_index/identity.hpp>
  19. #include <boost/multi_index/member.hpp>
  20. #include <boost/multi_index/ordered_index.hpp>
  21. #include <boost/multi_index/random_access_index.hpp>
  22. #include <boost/multi_index/ranked_index.hpp>
  23. #include <boost/multi_index/sequenced_index.hpp>
  24. #include <ostream>
  25. #include <string>
  26. #include "non_std_allocator.hpp"
  27. struct employee
  28. {
  29. int id;
  30. std::string name;
  31. int age;
  32. int ssn;
  33. employee(int id_,std::string name_,int age_,int ssn_):
  34. id(id_),name(name_),age(age_),ssn(ssn_)
  35. {}
  36. employee(const employee& x):
  37. id(x.id),name(x.name),age(x.age),ssn(x.ssn)
  38. {}
  39. employee(BOOST_RV_REF(employee) x):
  40. id(x.id),name(boost::move(x.name)),age(x.age),ssn(x.ssn)
  41. {}
  42. employee& operator=(BOOST_COPY_ASSIGN_REF(employee) x)
  43. {
  44. id=x.id;
  45. name=x.name;
  46. age=x.age;
  47. ssn=x.ssn;
  48. return *this;
  49. };
  50. employee& operator=(BOOST_RV_REF(employee) x)
  51. {
  52. id=x.id;
  53. name=boost::move(x.name);
  54. age=x.age;
  55. ssn=x.ssn;
  56. return *this;
  57. }
  58. bool operator==(const employee& x)const
  59. {
  60. return id==x.id&&name==x.name&&age==x.age;
  61. }
  62. bool operator<(const employee& x)const
  63. {
  64. return id<x.id;
  65. }
  66. bool operator!=(const employee& x)const{return !(*this==x);}
  67. bool operator> (const employee& x)const{return x<*this;}
  68. bool operator>=(const employee& x)const{return !(*this<x);}
  69. bool operator<=(const employee& x)const{return !(x<*this);}
  70. struct comp_id
  71. {
  72. bool operator()(int x,const employee& e2)const{return x<e2.id;}
  73. bool operator()(const employee& e1,int x)const{return e1.id<x;}
  74. };
  75. friend std::ostream& operator<<(std::ostream& os,const employee& e)
  76. {
  77. os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
  78. return os;
  79. }
  80. private:
  81. BOOST_COPYABLE_AND_MOVABLE(employee)
  82. };
  83. struct name{};
  84. struct by_name{};
  85. struct age{};
  86. struct as_inserted{};
  87. struct ssn{};
  88. struct randomly{};
  89. struct employee_set_indices:
  90. boost::mpl::vector<
  91. boost::multi_index::ordered_unique<
  92. boost::multi_index::identity<employee> >,
  93. boost::multi_index::hashed_non_unique<
  94. boost::multi_index::tag<name,by_name>,
  95. BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
  96. boost::multi_index::ranked_non_unique<
  97. boost::multi_index::tag<age>,
  98. BOOST_MULTI_INDEX_MEMBER(employee,int,age)>,
  99. boost::multi_index::sequenced<
  100. boost::multi_index::tag<as_inserted> >,
  101. boost::multi_index::hashed_unique<
  102. boost::multi_index::tag<ssn>,
  103. BOOST_MULTI_INDEX_MEMBER(employee,int,ssn)>,
  104. boost::multi_index::random_access<
  105. boost::multi_index::tag<randomly> > >
  106. {};
  107. typedef
  108. boost::multi_index::multi_index_container<
  109. employee,
  110. employee_set_indices,
  111. non_std_allocator<employee> > employee_set;
  112. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  113. typedef boost::multi_index::nth_index<
  114. employee_set,1>::type employee_set_by_name;
  115. #else
  116. typedef employee_set::nth_index<1>::type employee_set_by_name;
  117. #endif
  118. typedef boost::multi_index::index<
  119. employee_set,age>::type employee_set_by_age;
  120. typedef boost::multi_index::index<
  121. employee_set,as_inserted>::type employee_set_as_inserted;
  122. typedef boost::multi_index::index<
  123. employee_set,ssn>::type employee_set_by_ssn;
  124. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  125. typedef boost::multi_index::index<
  126. employee_set,randomly>::type employee_set_randomly;
  127. #else
  128. typedef employee_set::index<
  129. randomly>::type employee_set_randomly;
  130. #endif
  131. #endif