File: enable_shared_from_this_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (94 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//////////////////////////////////////////////////////////////////////////////
//  Copyright (c) 2002, 2003 Peter Dimov
//
// This file is the adaptation of shared_from_this_test.cpp from smart_ptr library
//
// (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp>
#include <boost/interprocess/smart_ptr/shared_ptr.hpp>

#include <boost/core/lightweight_test.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include "get_process_id_name.hpp"

//

using namespace boost::interprocess;

typedef allocator<void, managed_shared_memory::segment_manager>
   v_allocator_t;

struct X;

typedef deleter<X, managed_shared_memory::segment_manager>  x_deleter_t;

struct X :
   public enable_shared_from_this<X, v_allocator_t, x_deleter_t>
{
};

typedef shared_ptr<X, v_allocator_t, x_deleter_t>           v_shared_ptr;


template<class ManagedMemory>
void test_enable_shared_this(ManagedMemory &managed_mem)
{
   v_shared_ptr p(make_managed_shared_ptr
      (managed_mem.template construct<X>(anonymous_instance)(), managed_mem));

   v_shared_ptr q = p->shared_from_this();
   BOOST_TEST(p == q);
   BOOST_TEST(!(p < q) && !(q < p));

   X v2(*p);

   BOOST_TRY
   {
      //This should throw bad_weak_ptr
      v_shared_ptr r = v2.shared_from_this();
      BOOST_ERROR("v2.shared_from_this() failed to throw");
   }
   BOOST_CATCH(boost::interprocess::bad_weak_ptr const &)
   {
      //This is the expected path
   }
   BOOST_CATCH(...){
      BOOST_ERROR("v2.shared_from_this() threw an unexpected exception");
   } BOOST_CATCH_END

   BOOST_TRY
   {
      //This should not throw bad_weak_ptr
      *p = X();
      v_shared_ptr r = p->shared_from_this();
      BOOST_TEST(p == r);
      BOOST_TEST(!(p < r) && !(r < p));
   }
   BOOST_CATCH(boost::interprocess::bad_weak_ptr const &)
   {
      BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = X()");
   }
   BOOST_CATCH(...)
   {
      BOOST_ERROR("p->shared_from_this() threw an unexpected exception after *p = X()");
   } BOOST_CATCH_END
}


int main()
{
   std::string process_name;
   test::get_process_id_name(process_name);
   shared_memory_object::remove(process_name.c_str());
   managed_shared_memory shmem(create_only, process_name.c_str(), 65536);
   test_enable_shared_this(shmem);
   shared_memory_object::remove(process_name.c_str());
   return boost::report_errors();
}