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
f34067a7
Commit
f34067a7
authored
Nov 09, 2015
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support constructing maybes from error enums
parent
460560ab
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
11 deletions
+41
-11
libcaf_core/caf/maybe.hpp
libcaf_core/caf/maybe.hpp
+29
-11
libcaf_core/test/maybe.cpp
libcaf_core/test/maybe.cpp
+12
-0
No files found.
libcaf_core/caf/maybe.hpp
View file @
f34067a7
...
...
@@ -76,6 +76,16 @@ public:
cr_moved_value
(
value
);
}
/// Creates an instance representing an error
/// from an error condition enum.
template
<
class
E
,
class
=
typename
std
::
enable_if
<
std
::
is_error_condition_enum
<
E
>
::
value
>::
type
>
maybe
(
E
error_code_enum
)
{
cr_error
(
std
::
make_error_condition
(
error_code_enum
));
}
/// Creates an empty instance.
maybe
()
{
cr_error
(
error_type
{});
...
...
@@ -152,6 +162,14 @@ public:
return
*
this
;
}
template
<
class
E
,
class
=
typename
std
::
enable_if
<
std
::
is_error_condition_enum
<
E
>
::
value
>::
type
>
maybe
&
operator
=
(
E
error_code_enum
)
{
return
*
this
=
std
::
make_error_condition
(
error_code_enum
);
}
maybe
&
operator
=
(
maybe
&&
other
)
{
return
other
?
*
this
=
std
::
move
(
*
other
)
:
*
this
=
std
::
move
(
other
.
error
());
}
...
...
@@ -381,6 +399,8 @@ bool operator!=(const none_t&, const maybe<T>& val) {
return
!
val
.
empty
();
}
// Represents a computation performing side effects only and
// optionally return a `std::error_condition`.
template
<
>
class
maybe
<
void
>
{
public:
...
...
@@ -391,21 +411,22 @@ public:
using
const_pointer
=
const
type
*
;
using
error_type
=
std
::
error_condition
;
maybe
(
const
none_t
&
=
none
)
{
}
maybe
(
)
=
default
;
maybe
(
error_type
err
)
:
error_
{
std
::
move
(
err
)}
{
}
maybe
(
const
none_t
&
)
{
// nop
}
maybe
&
operator
=
(
const
none_t
&
)
{
error_
=
{};
return
*
this
;
maybe
(
error_type
err
)
:
error_
(
std
::
move
(
err
))
{
// nop
}
maybe
&
operator
=
(
const
error_type
&
err
)
{
error_
=
err
;
maybe
&
operator
=
(
const
none_t
&
)
{
error_
.
clear
()
;
return
*
this
;
}
maybe
&
operator
=
(
error_type
&&
err
)
{
maybe
&
operator
=
(
error_type
err
)
{
error_
=
std
::
move
(
err
);
return
*
this
;
}
...
...
@@ -448,14 +469,11 @@ public:
return
&
get
();
}
/// Returns whether this objects holds neither a value nor an actual error.
bool
empty
()
const
{
return
!
error
();
}
/// Returns the error.
const
error_type
&
error
()
const
{
CAF_ASSERT
(
!
available
());
return
error_
;
}
...
...
libcaf_core/test/maybe.cpp
View file @
f34067a7
...
...
@@ -81,6 +81,18 @@ CAF_TEST(custom_type_engaged) {
CAF_CHECK
(
*
j
==
obj
);
}
CAF_TEST
(
error_construct_and_assign
)
{
auto
f
=
[]()
->
maybe
<
int
>
{
return
std
::
errc
::
invalid_argument
;
};
auto
val
=
f
();
CAF_CHECK
(
!
val
&&
val
.
error
()
==
std
::
errc
::
invalid_argument
);
val
=
42
;
CAF_CHECK
(
val
&&
*
val
==
42
);
val
=
std
::
errc
::
state_not_recoverable
;
CAF_CHECK
(
!
val
&&
val
.
error
()
==
std
::
errc
::
state_not_recoverable
);
}
CAF_TEST
(
maybe_void
)
{
maybe
<
void
>
m
;
CAF_CHECK
(
!
m
);
...
...
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