weak_from_this_test.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <boost/config.hpp>
  2. //
  3. // weak_from_this_test.cpp
  4. //
  5. // Copyright (c) 2002, 2003, 2015 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. //
  9. // See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt
  11. //
  12. #include <boost/enable_shared_from_this.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/weak_ptr.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. class V: public boost::enable_shared_from_this<V>
  17. {
  18. };
  19. void test()
  20. {
  21. boost::shared_ptr<V> p( new V );
  22. boost::weak_ptr<V> q = p;
  23. BOOST_TEST( !q.expired() );
  24. boost::weak_ptr<V> q2 = p->weak_from_this();
  25. BOOST_TEST( !q2.expired() );
  26. BOOST_TEST( !(q < q2) && !(q2 < q) );
  27. V v2( *p );
  28. boost::weak_ptr<V> q3 = v2.weak_from_this();
  29. BOOST_TEST( q3.expired() );
  30. *p = V();
  31. boost::weak_ptr<V> q4 = p->weak_from_this();
  32. BOOST_TEST( !q4.expired() );
  33. BOOST_TEST( !(q < q4) && !(q4 < q) );
  34. BOOST_TEST( !(q2 < q4) && !(q4 < q2) );
  35. }
  36. int main()
  37. {
  38. test();
  39. return boost::report_errors();
  40. }