Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
boost_interprocess/example/doc_scoped_ptr.cpp at develop · zanbilify/boost_interprocess · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
zanbilify
/
boost_interprocess
Public
forked from
boostorg/interprocess
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
boost_interprocess
/
example
/
doc_scoped_ptr.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
112 lines (99 loc) · 3.42 KB
develop
Breadcrumbs
boost_interprocess
/
example
/
doc_scoped_ptr.cpp
Copy path
Top
File metadata and controls
Code
Blame
112 lines (99 loc) · 3.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
////////////////////////////////////////////////////////////////////////////
//
//
(C) Copyright Ion Gaztanaga 2006-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
>
//
[doc_scoped_ptr
#
include
<
boost/interprocess/managed_shared_memory.hpp
>
#
include
<
boost/interprocess/smart_ptr/scoped_ptr.hpp
>
//
<-
#
include
"
../test/get_process_id_name.hpp
"
//
->
using
namespace
boost
::interprocess
;
class
my_class
{};
class
my_exception
{};
//
A functor that destroys the shared memory object
template
<
class
T
>
class
my_deleter
{
private:
//
A typedef to save typing
typedef
managed_shared_memory::segment_manager segment_manager;
//
This my_deleter is created in the stack, not in shared memory,
//
so we can use raw pointers
segment_manager *mp_segment_manager;
public:
//
This typedef will specify the pointer type that
//
scoped_ptr will store
typedef
T *pointer;
//
Constructor
my_deleter
(segment_manager *s_mngr)
: mp_segment_manager(s_mngr){}
void
operator
()(pointer object_to_delete)
{ mp_segment_manager->
destroy_ptr
(object_to_delete); }
};
int
main
()
{
//
Create shared memory
//
Remove shared memory on construction and destruction
struct
shm_remove
{
shm_remove
() {
shared_memory_object::remove
(
test::get_process_id_name
()); }
~shm_remove
(){
shared_memory_object::remove
(
test::get_process_id_name
()); }
} remover;
//
<-
(
void
)remover;
//
->
managed_shared_memory
shmem
(create_only,
test::get_process_id_name
(),
10000
);
//
In the first try, there will be no exceptions
//
in the second try we will throw an exception
for
(
int
i =
0
; i <
2
; ++i){
//
Create an object in shared memory
my_class * my_object = shmem.
construct
<my_class>(
"
my_object
"
)();
my_class * my_object2 = shmem.
construct
<my_class>(anonymous_instance)();
shmem.
destroy_ptr
(my_object2);
//
Since the next shared memory allocation can throw
//
assign it to a scoped_ptr so that if an exception occurs
//
we destroy the object automatically
my_deleter<my_class>
d
(shmem.
get_segment_manager
());
BOOST_INTERPROCESS_TRY
{
scoped_ptr<my_class, my_deleter<my_class> >
s_ptr
(my_object, d);
//
Let's emulate a exception capable operation
//
In the second try, throw an exception
if
(i ==
1
){
throw
(
my_exception
());
}
//
If we have passed the dangerous zone
//
we can release the scoped pointer
//
to avoid destruction
s_ptr.
release
();
}
BOOST_INTERPROCESS_CATCH
(
const
my_exception &){}
BOOST_INTERPROCESS_CATCH_END
//
Here, scoped_ptr is destroyed
//
so it we haven't thrown an exception
//
the object should be there, otherwise, destroyed
if
(i ==
0
){
//
Make sure the object is alive
if
(!shmem.
find
<my_class>(
"
my_object
"
).
first
){
return
1
;
}
//
Now we can use it and delete it manually
shmem.
destroy
<my_class>(
"
my_object
"
);
}
else
{
//
Make sure the object has been deleted
if
(shmem.
find
<my_class>(
"
my_object
"
).
first
){
return
1
;
}
}
}
return
0
;
}
//
]
You can’t perform that action at this time.