1 //////////////////////////////////////////////////////////////////////////////
2 //  Copyright (c) 2002, 2003 Peter Dimov
3 //
4 // This file is the adaptation of shared_from_this_test.cpp from smart_ptr library
5 //
6 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // See http://www.boost.org/libs/interprocess for documentation.
11 //
12 //////////////////////////////////////////////////////////////////////////////
13 #include <boost/interprocess/detail/config_begin.hpp>
14 #include <boost/interprocess/detail/workaround.hpp>
15 #include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp>
16 #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
17 
18 #include <boost/core/lightweight_test.hpp>
19 #include <boost/interprocess/managed_shared_memory.hpp>
20 #include "get_process_id_name.hpp"
21 
22 //
23 
24 using namespace boost::interprocess;
25 
26 typedef allocator<void, managed_shared_memory::segment_manager>
27    v_allocator_t;
28 
29 struct X;
30 
31 typedef deleter<X, managed_shared_memory::segment_manager>  x_deleter_t;
32 
33 struct X :
34    public enable_shared_from_this<X, v_allocator_t, x_deleter_t>
35 {
36 };
37 
38 typedef shared_ptr<X, v_allocator_t, x_deleter_t>           v_shared_ptr;
39 
40 
41 template<class ManagedMemory>
test_enable_shared_this(ManagedMemory & managed_mem)42 void test_enable_shared_this(ManagedMemory &managed_mem)
43 {
44    v_shared_ptr p(make_managed_shared_ptr
45       (managed_mem.template construct<X>(anonymous_instance)(), managed_mem));
46 
47    v_shared_ptr q = p->shared_from_this();
48    BOOST_TEST(p == q);
49    BOOST_TEST(!(p < q) && !(q < p));
50 
51    X v2(*p);
52 
53    try
54    {
55       //This should throw bad_weak_ptr
56       v_shared_ptr r = v2.shared_from_this();
57       BOOST_ERROR("v2.shared_from_this() failed to throw");
58    }
59    catch(boost::interprocess::bad_weak_ptr const &)
60    {
61       //This is the expected path
62    }
63    catch(...){
64       BOOST_ERROR("v2.shared_from_this() threw an unexpected exception");
65    }
66 
67    try
68    {
69       //This should not throw bad_weak_ptr
70       *p = X();
71       v_shared_ptr r = p->shared_from_this();
72       BOOST_TEST(p == r);
73       BOOST_TEST(!(p < r) && !(r < p));
74    }
75    catch(boost::interprocess::bad_weak_ptr const &)
76    {
77       BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = X()");
78    }
79    catch(...)
80    {
81       BOOST_ERROR("p->shared_from_this() threw an unexpected exception after *p = X()");
82    }
83 }
84 
85 
main()86 int main()
87 {
88    std::string process_name;
89    test::get_process_id_name(process_name);
90    shared_memory_object::remove(process_name.c_str());
91    managed_shared_memory shmem(create_only, process_name.c_str(), 65536);
92    test_enable_shared_this(shmem);
93    shared_memory_object::remove(process_name.c_str());
94    return boost::report_errors();
95 }
96 
97 #include <boost/interprocess/detail/config_end.hpp>
98