Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
238331fe
Commit
238331fe
authored
Jul 07, 2016
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use scope guard in custom type example 3
parent
a2ea6a22
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
15 deletions
+34
-15
examples/custom_type/custom_types_3.cpp
examples/custom_type/custom_types_3.cpp
+34
-15
No files found.
examples/custom_type/custom_types_3.cpp
View file @
238331fe
// Showcases custom message types that cannot provide
// Showcases custom message types that cannot provide
// friend access to the inspect() function.
// friend access to the inspect() function.
// Manual refs: 20-49,
51-76
(TypeInspection)
// Manual refs: 20-49,
76-103
(TypeInspection)
#include <utility>
#include <utility>
#include <iostream>
#include <iostream>
...
@@ -48,6 +48,31 @@ private:
...
@@ -48,6 +48,31 @@ private:
int
b_
;
int
b_
;
};
};
// A lightweight scope guard implementation.
template
<
class
Fun
>
class
scope_guard
{
public:
scope_guard
(
Fun
f
)
:
fun_
(
std
::
move
(
f
)),
enabled_
(
true
)
{
}
scope_guard
(
scope_guard
&&
x
)
:
fun_
(
std
::
move
(
x
.
fun_
)),
enabled_
(
x
.
enabled_
)
{
x
.
enabled_
=
false
;
}
~
scope_guard
()
{
if
(
enabled_
)
fun_
();
}
private:
Fun
fun_
;
bool
enabled_
;
};
// Creates a guard that executes `f` as soon as it goes out of scope.
template
<
class
Fun
>
scope_guard
<
Fun
>
make_scope_guard
(
Fun
f
)
{
return
{
std
::
move
(
f
)};
}
template
<
class
Inspector
>
template
<
class
Inspector
>
typename
std
::
enable_if
<
Inspector
::
is_saving
::
value
,
typename
std
::
enable_if
<
Inspector
::
is_saving
::
value
,
typename
Inspector
::
result_type
>::
type
typename
Inspector
::
result_type
>::
type
...
@@ -59,20 +84,14 @@ template <class Inspector>
...
@@ -59,20 +84,14 @@ template <class Inspector>
typename
std
::
enable_if
<
Inspector
::
is_loading
::
value
,
typename
std
::
enable_if
<
Inspector
::
is_loading
::
value
,
typename
Inspector
::
result_type
>::
type
typename
Inspector
::
result_type
>::
type
inspect
(
Inspector
&
f
,
foo
&
x
)
{
inspect
(
Inspector
&
f
,
foo
&
x
)
{
struct
tmp_t
{
int
a
;
tmp_t
(
foo
&
ref
)
:
x_
(
ref
)
{
int
b
;
// nop
// write back to x at scope exit
}
auto
g
=
make_scope_guard
([
&
]
{
~
tmp_t
()
{
x
.
set_a
(
a
);
// write back to x at scope exit
x
.
set_b
(
b
);
x_
.
set_a
(
a
);
});
x_
.
set_b
(
b
);
return
f
(
meta
::
type_name
(
"foo"
),
a
,
b
);
}
foo
&
x_
;
int
a
;
int
b
;
}
tmp
{
x
};
return
f
(
meta
::
type_name
(
"foo"
),
tmp
.
a
,
tmp
.
b
);
}
}
behavior
testee
(
event_based_actor
*
self
)
{
behavior
testee
(
event_based_actor
*
self
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment