stdcpp/tsrc/Boost_test/smart_ptr/src/shared_from_this_test.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 #include <boost/config.hpp>
       
     2 
       
     3 #if defined(BOOST_MSVC)
       
     4 #pragma warning(disable: 4786)  // identifier truncated in debug info
       
     5 #pragma warning(disable: 4710)  // function not inlined
       
     6 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
       
     7 #pragma warning(disable: 4514)  // unreferenced inline removed
       
     8 #endif
       
     9 
       
    10 //
       
    11 //  shared_from_this_test.cpp
       
    12 //
       
    13 //  Copyright (c) 2002, 2003 Peter Dimov
       
    14 //
       
    15 // Distributed under the Boost Software License, Version 1.0. (See
       
    16 // accompanying file LICENSE_1_0.txt or copy at
       
    17 // http://www.boost.org/LICENSE_1_0.txt)
       
    18 //
       
    19 /*
       
    20  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
       
    21 */
       
    22 
       
    23 
       
    24 #include <boost/enable_shared_from_this.hpp>
       
    25 #include <boost/shared_ptr.hpp>
       
    26 
       
    27 #include <boost/detail/lightweight_test.hpp>
       
    28 
       
    29 #ifdef __SYMBIAN32__
       
    30 #include "std_log_result.h"
       
    31 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    32 #endif
       
    33 
       
    34 //
       
    35 
       
    36 class X
       
    37 {
       
    38 public:
       
    39 
       
    40     virtual void f() = 0;
       
    41 
       
    42 protected:
       
    43 
       
    44     ~X() {}
       
    45 };
       
    46 
       
    47 class Y
       
    48 {
       
    49 public:
       
    50 
       
    51     virtual boost::shared_ptr<X> getX() = 0;
       
    52 
       
    53 protected:
       
    54 
       
    55     ~Y() {}
       
    56 };
       
    57 
       
    58 boost::shared_ptr<Y> createY();
       
    59 
       
    60 void test()
       
    61 {
       
    62     boost::shared_ptr<Y> py = createY();
       
    63     BOOST_TEST(py.get() != 0);
       
    64     BOOST_TEST(py.use_count() == 1);
       
    65 
       
    66     boost::shared_ptr<X> px = py->getX();
       
    67     BOOST_TEST(px.get() != 0);
       
    68     BOOST_TEST(py.use_count() == 2);
       
    69 
       
    70     px->f();
       
    71 
       
    72     boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
       
    73     BOOST_TEST(py.get() == py2.get());
       
    74     BOOST_TEST(!(py < py2 || py2 < py));
       
    75     BOOST_TEST(py.use_count() == 3);
       
    76 }
       
    77 
       
    78 void test2();
       
    79 void test3();
       
    80 
       
    81 int main()
       
    82 {
       
    83 	std_log(LOG_FILENAME_LINE,"[Test Case for shared_from_this_test]");
       
    84     test();
       
    85     test2();
       
    86     test3();
       
    87 #ifdef __SYMBIAN32__
       
    88 	int failures = boost::report_errors();
       
    89 	if(failures)
       
    90 	{
       
    91 		std_log(LOG_FILENAME_LINE,"Result : Failed");
       
    92 		assert_failed = true;
       
    93 	}
       
    94 	else
       
    95 	{
       
    96 		std_log(LOG_FILENAME_LINE,"Result : Passed");
       
    97 	}
       
    98 	std_log(LOG_FILENAME_LINE,"[End Test Case ]");
       
    99 #endif
       
   100 	testResultXml("shared_from_this_test");
       
   101 	close_log_file();
       
   102 	return failures;
       
   103 }
       
   104 
       
   105 // virtual inheritance to stress the implementation
       
   106 // (prevents Y* -> impl*, enable_shared_from_this<impl>* -> impl* casts)
       
   107 
       
   108 class impl: public X, public virtual Y, public virtual boost::enable_shared_from_this<impl>
       
   109 {
       
   110 public:
       
   111 
       
   112     virtual void f()
       
   113     {
       
   114     }
       
   115 
       
   116     virtual boost::shared_ptr<X> getX()
       
   117     {
       
   118         boost::shared_ptr<impl> pi = shared_from_this();
       
   119         BOOST_TEST(pi.get() == this);
       
   120         return pi;
       
   121     }
       
   122 };
       
   123 
       
   124 // intermediate impl2 to stress the implementation
       
   125 
       
   126 class impl2: public impl
       
   127 {
       
   128 };
       
   129 
       
   130 boost::shared_ptr<Y> createY()
       
   131 {
       
   132     boost::shared_ptr<Y> pi(new impl2);
       
   133     return pi;
       
   134 }
       
   135 
       
   136 void test2()
       
   137 {
       
   138     boost::shared_ptr<Y> pi(static_cast<impl2*>(0));
       
   139 }
       
   140 
       
   141 //
       
   142 
       
   143 struct V: public boost::enable_shared_from_this<V>
       
   144 {
       
   145 };
       
   146 
       
   147 void test3()
       
   148 {
       
   149     boost::shared_ptr<V> p(new V);
       
   150 
       
   151     boost::shared_ptr<V> q = p->shared_from_this();
       
   152     BOOST_TEST(p == q);
       
   153     BOOST_TEST(!(p < q) && !(q < p));
       
   154 
       
   155     V v2(*p);
       
   156 
       
   157     try
       
   158     {
       
   159         boost::shared_ptr<V> r = v2.shared_from_this();
       
   160         BOOST_ERROR("v2.shared_from_this() failed to throw");
       
   161     }
       
   162     catch(boost::bad_weak_ptr const &)
       
   163     {
       
   164     }
       
   165 
       
   166     try
       
   167     {
       
   168         *p = V();
       
   169         boost::shared_ptr<V> r = p->shared_from_this();
       
   170         BOOST_TEST(p == r);
       
   171         BOOST_TEST(!(p < r) && !(r < p));
       
   172     }
       
   173     catch(boost::bad_weak_ptr const &)
       
   174     {
       
   175         BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()");
       
   176     }
       
   177 }