Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would be nice if support check valid #14

Open
pvthuyet opened this issue Sep 11, 2020 · 1 comment
Open

Would be nice if support check valid #14

pvthuyet opened this issue Sep 11, 2020 · 1 comment

Comments

@pvthuyet
Copy link

Additional context
Can support the valid checking for unique_resource?
Example:

auto file = make_unique_resource_checked(::fopen("nonexistentfile.txt", "r"), nullptr, fclose);
ASSERT_EQUAL((FILE*)NULL, file.get());
// reading the content 
// ...

I would expected:

auto file = make_unique_resource_checked(::fopen("nonexistentfile.txt", "r"), nullptr, fclose);
ASSERT_EQUAL((FILE*)NULL, file.get());
if (file.valid()) {
     // reading the content 
     // ...
}
@PeterSommerlad
Copy link
Owner

The current implementation does not invalidate the resource member, when it is in a moved-from or invalid state. To allow such a test, would mean to have the "empty" state as an additional template parameter. Microsoft has an implementation of a similar class that allows this (defining the empty state via a policy). With such, an empty/invalid state can be used.

I am thinking about resurrecting the unique_resource proposal with an implementation that keeps the resources in a std::optional. and have a moved-from state being an empty optional. This could then give a simple check for all such cases.

In the above case the query is the condition in the ASSERT_EQUAL

if( file.get() != nullptr )...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants