Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
0091c4e1
Commit
0091c4e1
authored
Mar 01, 2021
by
Max Kellermann
Committed by
Max Kellermann
Mar 02, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/Exception: add FindNested()
parent
80172e17
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
0 deletions
+90
-0
Exception.hxx
src/util/Exception.hxx
+21
-0
TestException.cxx
test/util/TestException.cxx
+69
-0
No files found.
src/util/Exception.hxx
View file @
0091c4e1
...
...
@@ -84,6 +84,27 @@ NestException(std::exception_ptr ep, T &&t) noexcept
}
/**
* Find an instance of #T in the nested exception chain, and return a
* pointer. Returns nullptr if no such instance was found.
*/
template
<
typename
T
>
[[
gnu
::
pure
]]
inline
const
T
*
FindNested
(
std
::
exception_ptr
ep
)
noexcept
{
try
{
std
::
rethrow_exception
(
ep
);
}
catch
(
const
T
&
t
)
{
return
&
t
;
}
catch
(
const
std
::
nested_exception
&
ne
)
{
return
FindNested
<
T
>
(
ne
.
nested_ptr
());
}
catch
(...)
{
}
return
nullptr
;
}
/**
* Find an instance of #T in the nested exception chain, and rethrow
* it. Does nothing if no such instance was found.
*/
...
...
test/util/TestException.cxx
View file @
0091c4e1
...
...
@@ -50,6 +50,75 @@ TEST(ExceptionTest, DerivedError)
ASSERT_EQ
(
GetFullMessage
(
std
::
make_exception_ptr
(
DerivedError
(
"Foo"
))),
"Foo"
);
}
TEST
(
ExceptionTest
,
FindNestedDirect
)
{
struct
Foo
{};
struct
Bar
{};
struct
Derived
:
Foo
{};
try
{
throw
Foo
{};
}
catch
(...)
{
EXPECT_NE
(
FindNested
<
Foo
>
(
std
::
current_exception
()),
nullptr
);
}
try
{
throw
Bar
{};
}
catch
(...)
{
EXPECT_EQ
(
FindNested
<
Foo
>
(
std
::
current_exception
()),
nullptr
);
}
try
{
throw
Derived
{};
}
catch
(...)
{
EXPECT_NE
(
FindNested
<
Foo
>
(
std
::
current_exception
()),
nullptr
);
}
}
TEST
(
ExceptionTest
,
FindNestedIndirect
)
{
struct
Foo
{};
struct
Bar
{};
struct
Derived
:
Foo
{};
struct
Outer
{};
try
{
throw
Foo
{};
}
catch
(...)
{
try
{
std
::
throw_with_nested
(
Outer
{});
}
catch
(...)
{
EXPECT_NE
(
FindNested
<
Foo
>
(
std
::
current_exception
()),
nullptr
);
}
}
try
{
throw
Bar
{};
}
catch
(...)
{
try
{
std
::
throw_with_nested
(
Outer
{});
}
catch
(...)
{
EXPECT_EQ
(
FindNested
<
Foo
>
(
std
::
current_exception
()),
nullptr
);
}
}
try
{
throw
Derived
{};
}
catch
(...)
{
try
{
std
::
throw_with_nested
(
Outer
{});
}
catch
(...)
{
EXPECT_NE
(
FindNested
<
Foo
>
(
std
::
current_exception
()),
nullptr
);
}
}
}
template
<
typename
T
>
static
bool
CheckFindRetrowNested
(
std
::
exception_ptr
e
)
noexcept
...
...
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