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/workaround.hpp> |
14 | #include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp> |
15 | #include <boost/interprocess/smart_ptr/shared_ptr.hpp> |
16 | |
17 | #include <boost/core/lightweight_test.hpp> |
18 | #include <boost/interprocess/managed_shared_memory.hpp> |
19 | #include "get_process_id_name.hpp" |
20 | |
21 | // |
22 | |
23 | using namespace boost::interprocess; |
24 | |
25 | typedef allocator<void, managed_shared_memory::segment_manager> |
26 | v_allocator_t; |
27 | |
28 | struct X; |
29 | |
30 | typedef deleter<X, managed_shared_memory::segment_manager> x_deleter_t; |
31 | |
32 | struct X : |
33 | public enable_shared_from_this<X, v_allocator_t, x_deleter_t> |
34 | { |
35 | }; |
36 | |
37 | typedef shared_ptr<X, v_allocator_t, x_deleter_t> v_shared_ptr; |
38 | |
39 | |
40 | template<class ManagedMemory> |
41 | void test_enable_shared_this(ManagedMemory &managed_mem) |
42 | { |
43 | v_shared_ptr p(make_managed_shared_ptr |
44 | (managed_mem.template construct<X>(anonymous_instance)(), managed_mem)); |
45 | |
46 | v_shared_ptr q = p->shared_from_this(); |
47 | BOOST_TEST(p == q); |
48 | BOOST_TEST(!(p < q) && !(q < p)); |
49 | |
50 | X v2(*p); |
51 | |
52 | BOOST_TRY |
53 | { |
54 | //This should throw bad_weak_ptr |
55 | v_shared_ptr r = v2.shared_from_this(); |
56 | BOOST_ERROR("v2.shared_from_this() failed to throw" ); |
57 | } |
58 | BOOST_CATCH(boost::interprocess::bad_weak_ptr const &) |
59 | { |
60 | //This is the expected path |
61 | } |
62 | BOOST_CATCH(...){ |
63 | BOOST_ERROR("v2.shared_from_this() threw an unexpected exception" ); |
64 | } BOOST_CATCH_END |
65 | |
66 | BOOST_TRY |
67 | { |
68 | //This should not throw bad_weak_ptr |
69 | *p = X(); |
70 | v_shared_ptr r = p->shared_from_this(); |
71 | BOOST_TEST(p == r); |
72 | BOOST_TEST(!(p < r) && !(r < p)); |
73 | } |
74 | BOOST_CATCH(boost::interprocess::bad_weak_ptr const &) |
75 | { |
76 | BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = X()" ); |
77 | } |
78 | BOOST_CATCH(...) |
79 | { |
80 | BOOST_ERROR("p->shared_from_this() threw an unexpected exception after *p = X()" ); |
81 | } BOOST_CATCH_END |
82 | } |
83 | |
84 | |
85 | int main() |
86 | { |
87 | std::string process_name; |
88 | test::get_process_id_name(str&: process_name); |
89 | shared_memory_object::remove(filename: process_name.c_str()); |
90 | managed_shared_memory shmem(create_only, process_name.c_str(), 65536); |
91 | test_enable_shared_this(managed_mem&: shmem); |
92 | shared_memory_object::remove(filename: process_name.c_str()); |
93 | return boost::report_errors(); |
94 | } |
95 | |