From d0c5e83def125388dc318e87f23f5db180d2d025 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 19 Nov 2002 13:45:33 +0000 Subject: [PATCH] Test for enable_shared_from_this added. [SVN r16318] --- enable_shared_from_this_test.cpp | 95 ++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 enable_shared_from_this_test.cpp diff --git a/enable_shared_from_this_test.cpp b/enable_shared_from_this_test.cpp new file mode 100644 index 0000000..66f9c17 --- /dev/null +++ b/enable_shared_from_this_test.cpp @@ -0,0 +1,95 @@ +#if defined(_MSC_VER) && !defined(__ICL) && !defined(__COMO__) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// enable_shared_from_this_test.cpp +// +// Copyright (c) 2002 Peter Dimov +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + + +#include +#include + +#include + +// + +class X +{ +public: + + virtual void f() = 0; + +protected: + + ~X() {} +}; + +class Y +{ +public: + + virtual boost::shared_ptr getX() = 0; + +protected: + + ~Y() {} +}; + +boost::shared_ptr createY(); + +void test() +{ + boost::shared_ptr py = createY(); + BOOST_TEST(py.get() != 0); + BOOST_TEST(py.use_count() == 1); + + boost::shared_ptr px = py->getX(); + BOOST_TEST(px.get() != 0); + BOOST_TEST(py.use_count() == 2); + + px->f(); + + boost::shared_ptr py2 = boost::dynamic_pointer_cast(px); + BOOST_TEST(py.get() == py2.get()); + BOOST_TEST(!(py < py2 || py2 < py)); + BOOST_TEST(py.use_count() == 3); +} + +int main() +{ + test(); + return boost::report_errors(); +} + +class impl: public X, public Y, public boost::enable_shared_from_this +{ +public: + + virtual void f() + { + } + + virtual boost::shared_ptr getX() + { + boost::shared_ptr pi = shared_from_this(); + BOOST_TEST(pi.get() == this); + return pi; + } +}; + +boost::shared_ptr createY() +{ + boost::shared_ptr pi(new impl); + return pi; +}