test_tagging.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // (C) Copyright Andy Tompkins 2009. Permission to copy, use, modify, sell and
  2. // distribute this software is granted provided this copyright notice appears
  3. // in all copies. This software is provided "as is" without express or implied
  4. // warranty, and with no claim as to its suitability for any purpose.
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // https://www.boost.org/LICENSE_1_0.txt)
  8. // libs/uuid/test/test_tagging.cpp -------------------------------//
  9. #include <boost/uuid/uuid.hpp>
  10. #include <boost/uuid/uuid_generators.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. class object
  13. {
  14. public:
  15. object()
  16. : tag(boost::uuids::random_generator()())
  17. , state(0)
  18. {}
  19. explicit object(int state)
  20. : tag(boost::uuids::random_generator()())
  21. , state(state)
  22. {}
  23. object(object const& rhs)
  24. : tag(rhs.tag)
  25. , state(rhs.state)
  26. {}
  27. bool operator==(object const& rhs) const {
  28. return tag == rhs.tag;
  29. }
  30. bool operator!=(object const& rhs) const {
  31. return !(operator==(rhs));
  32. }
  33. object& operator=(object const& rhs) {
  34. tag = rhs.tag;
  35. state = rhs.state;
  36. return *this;
  37. }
  38. int get_state() const { return state; }
  39. void set_state(int new_state) { state = new_state; }
  40. private:
  41. boost::uuids::uuid tag;
  42. int state;
  43. };
  44. template <typename elem, typename traits>
  45. std::basic_ostream<elem, traits>& operator<<(std::basic_ostream<elem, traits>& os, object const& o)
  46. {
  47. os << o.get_state();
  48. return os;
  49. }
  50. int main(int, char*[])
  51. {
  52. object o1(1);
  53. object o2 = o1;
  54. BOOST_TEST_EQ(o1, o2);
  55. o2.set_state(2);
  56. BOOST_TEST_EQ(o1, o2);
  57. object o3;
  58. o3.set_state(3);
  59. BOOST_TEST_NE(o1, o3);
  60. BOOST_TEST_NE(o2, o3);
  61. return boost::report_errors();
  62. }