2
0
mirror of https://github.com/boostorg/scope.git synced 2026-01-19 04:42:10 +00:00

179 Commits

Author SHA1 Message Date
Andrey Semashev
127c3aaa4e Enabled warnings-as-errors for tests. 2024-02-05 00:03:54 +03:00
Andrey Semashev
8861d0297d Avoid calling unique_resource_data member function in a catch block of unique_resource_data ctor. 2024-02-04 23:25:43 +03:00
Andrey Semashev
75ac159ab6 Fixed operator-> example for unique_resource. 2024-02-04 23:23:24 +03:00
Andrey Semashev
9ea2a7264a Added a missing comma. 2024-02-04 21:17:14 +03:00
Andrey Semashev
16bbac4980 Improved unique_resource docs. 2024-02-04 21:15:23 +03:00
Andrey Semashev
45317772d1 Removed support for "reduced" resource traits in unique_resource.
The support required to be permissive wrt. is_allocated being callable
with a resource argument. This could lead to unexpected behavior, where
the user provided a seemingly conforming resource traits but
unique_resource would still reject them for some reason (e.g. if
is_allocated is not callable with the const resource value, as tested
by unique_resource) and work in "reduced" resource traits mode. This
would change the behavior of the APIs that accept resource values as
an argument (e.g. constructing unique_resource with a resource value
would always produce an object in the allocated state, while the user
would expect it to depend on the resource value).

Removing the support for "reduced" resource traits makes the behavior
more predictable. If the user specified resource traits, it either
compiles and works according to the resource traits, or fails to compile
if the resource traits are somehow not conforming.

The support for customizing default-constructed resource value is not
important enough. It can be worked around by either implementing proper
resource traits, or by specifying the default value in
make_unique_resource_checked.

Additionally, modified unallocated_resource to relax the argument type
of is_allocated and the types of the unallocated values. This can be
useful when the types of the unallocated resource values don't match
the actual resource type (e.g. std::nullptr_t vs. typed pointers).
2024-02-04 19:32:24 +03:00
Andrey Semashev
7167bce99a Don't call reset() in unique_resource destructor, call the deleter directly.
This avoids potentially assigning the default to the resource value, which
may be expensive and is pointless, as the resource is about to be destroyed
anyway.
2024-02-04 01:46:07 +03:00
Andrey Semashev
adf827b190 Docs improvements.
Removed Boost.ScopeExit from the table on the front page. Comparison with
Boost.ScopeExit is in a separate section, so this table duplicated it
to some degree and detracted focus from introducing Boost.Scope.

Added an example with unique_resource to the front page.

Other wording and example improvements.
2024-02-03 21:08:05 +03:00
Andrey Semashev
3914e2fabd Revert "Switched to actions/checkout@v4 to avoid CI deprecation warnings."
This reverts commit a8750d563f.

checkout@v4 doesn't work on ubuntu:16.04 and ubuntu:18.04 images:

https://github.com/actions/checkout/issues/1590
2024-02-02 13:38:45 +03:00
Andrey Semashev
a8750d563f Switched to actions/checkout@v4 to avoid CI deprecation warnings. 2024-02-02 13:27:37 +03:00
Andrey Semashev
18ac09b864 Add a link to an article re. different values for unallocated Windows handles. 2024-02-02 06:08:09 +03:00
Andrey Semashev
05bf32daa5 Added an example of using set_active with a runtime value. 2024-02-02 02:42:17 +03:00
Andrey Semashev
f781fd71c7 Improved scope guard examples in docs. 2024-02-01 02:52:09 +03:00
Andrey Semashev
d53d4f43af Enforced correctness checks for resource traits, improved docs.
Instead of silently switching to the unique_resource implementation
based on the "allocated" flag if the resource traits don't conform
to the requirements, issue hard compilation errors. This should better
protect against mistakes, where the user assumes unique_resource
behaves as if it is using the resource traits while it actually doesn't.

Improved documentation of requirements for resource traits and documented
the "reduced" form of resource traits, where the traits only provide
the default resource value but otherwise don't change unique_resource
behavior.
2024-01-31 04:52:12 +03:00
Andrey Semashev
bb295bcd77 Consistently use "unallocated" term to refer to a unique_resource in an unallocated state. 2024-01-31 01:39:32 +03:00
Andrey Semashev
e595286ad3 Converted typedefs to using type aliases.
One person during review complained that the library implementation
used typedefs instead of C++11 using type aliases. Not an essential
difference, but if it helps code readability, let it be.
2024-01-31 01:11:01 +03:00
Andrey Semashev
080e8100f1 Made formatting more consistent in examples. 2024-01-30 11:43:59 +03:00
Andrey Semashev
989676edc1 Write strings in CSV format in the examples in the docs.
This makes the final flush of the file stream actually necessary.
2024-01-30 11:40:30 +03:00
Andrey Semashev
7616f0b944 Added a new set of examples on the front page.
The new row of examples illustrate explicit deactivation of the scope
guards, as opposed to automatic failure detection via scope_fail.

Also improved the other examples to look more realistic.

Closes https://github.com/Lastique/scope/issues/13.
2024-01-30 03:32:54 +03:00
Andrey Semashev
012104819f Improved comment. 2024-01-28 23:43:22 +03:00
Andrey Semashev
8c985b565c Changed behavior of unique_resource constructor to be non-destructive.
If the resource type is nothrow move-constructible and the deleter type
is not, and the deleter's copy constructor throws during unique_resource
move constructor, then move the move-constructed resource back to the
original object instead of invoking the deleter on it. This leaves the
move source in its original state rather than in unallocated state,
which means unique_resource move constructor now provides strong
exception guarantee.
2024-01-28 23:27:53 +03:00
Andrey Semashev
6f2adf3988 Made unique_resource::reset() implementation slightly more efficient. 2024-01-28 18:50:54 +03:00
Andrey Semashev
7be5037875 Simplified unique_resource swap implementation. 2024-01-28 18:50:54 +03:00
Andrey Semashev
eca047bf40 Updated discussion re. swap implementation on unique_resource. 2024-01-28 15:50:32 +03:00
Andrey Semashev
10d22551b2 Moved caveats of capturing by reference to a separate section. 2024-01-28 04:36:52 +03:00
Andrey Semashev
56afb11dbd Added an example showing scope_exit/scope_success/scope_fail usage. 2024-01-28 04:11:45 +03:00
Andrey Semashev
a9bb776763 Added a note about resource traits being preferred over make_unique_resource_checked. 2024-01-28 01:47:26 +03:00
Andrey Semashev
84be26e1a2 Use boost::core::functor for simplified CloseHandle deleter in the docs. 2024-01-27 20:36:13 +03:00
Andrey Semashev
ecb7d7c9cd Added comparison with Library Fundamentals TS components in the docs.
Closes https://github.com/Lastique/scope/issues/5.
2024-01-27 20:35:54 +03:00
Andrey Semashev
3d228f7294 Switched to BOOST_NO_CXX17_AUTO_NONTYPE_TEMPLATE_PARAMS from Boost.Config. 2024-01-26 22:57:25 +03:00
Andrey Semashev
518557da18 Switch to Boost.Config macro for C++17 fold expressions.
Turns out, Boost.Config's macro is not excessively restrictive.
2024-01-24 03:12:32 +03:00
Andrey Semashev
d45a6255aa Added unallocated_resource for simplified declaration of resource traits.
A new unallocated_resource class template allows to automatically generate
resource traits from one or more unallocated resource values.

The idea for a more compact unique_resource declaration was given
by Janko Dedic in his review.
2024-01-24 02:59:54 +03:00
Andrey Semashev
fa650d558d Added explicit operator bool to unique_resource.
The operator tests if the resource wrapper is in an allocate state.
2024-01-21 19:52:29 +03:00
Andrey Semashev
a53bdaad49 Removed release() member of scope guards.
Although it was there to make the scope guards intergace-compatible with
the TS, the review conclusion indicated that strict conformance with
the TS is not a desired goal. So remove the release() method and keep
set_active() as the way to (de)activate the scope guard.
2024-01-21 19:47:46 +03:00
Andrey Semashev
b0117a4b2e Renamed scope_final to defer_guard and BOOST_SCOPE_FINAL to BOOST_SCOPE_DEFER.
This follows the suggestion by Peter Dimov during the review.
2024-01-19 01:13:26 +03:00
Andrey Semashev
80d26d6909 Prevent default-construction of function pointers.
Default- and value-constructed pointers to functions are not callable,
and therefore are not accepted as default-constructible condition
function objects and deleters.

Closes https://github.com/Lastique/scope/issues/14.
2023-12-17 19:16:08 +03:00
Andrey Semashev
ad4005f87e Added an assert to make it obvious that ptr is not null. 2023-12-03 01:16:37 +03:00
Andrey Semashev
7ee1b44711 Added a note about potential issues of capturing by reference in scope guards. 2023-12-03 01:11:01 +03:00
Andrey Semashev
5719cd4966 Changed the wording to discourage using std::function with scope guards. 2023-12-02 23:39:39 +03:00
Andrey Semashev
bc8665d398 Changed the example of unique_resource usage.
Use an `equal_files` function as an example to demonstrate `unique_resource`
usage. This is less controversial than the previously used `write_string`
function, as it should not raise concerns about general correctness of the
function in case of failure (i.e. whether it is correct to leave the file
partially written in case of failure).

Additionally, after code refactoring, the actual interesting part of the
example became smaller, which allowed to remove most of the `equal_files`
implementation from the second and third code samples.

Closes https://github.com/Lastique/scope/issues/9.
2023-12-02 23:21:22 +03:00
Andrey Semashev
958af1cd59 Use a using type alieas instead of a typedef in the docs. 2023-12-02 20:46:25 +03:00
Andrey Semashev
f081d0f850 Improved wording leading to function object type requirements.
Closes https://github.com/Lastique/scope/issues/12.
2023-12-02 19:50:33 +03:00
Andrey Semashev
2cd37e97c9 Remove forwarding res to is_allocated on unique_resource_data construction.
It is possible that the resource source will become moved-from after the
call to is_allocated, and that moved-from state will then be used to
initialize the resource in the resource_holder. To avoid this, don't
forward the resource source to is_allocated.

Fixes https://github.com/Lastique/scope/issues/10.
2023-12-01 19:33:21 +03:00
Andrey Semashev
a43830544a Removed forwarding resource and deleter when constructor may throw.
When resource_holder and deleter_holder are constructed, and resource/deleter
constructor may throw, remove forwarding the source resource/deleter to
the constructor. Forwarding there has no effect anyway, as the unique_resource
constructor already ensures the corresponding argument is passed as
an lvalue reference by using move_or_copy_construct_ref.
2023-12-01 19:32:27 +03:00
Andrey Semashev
969584e578 Use invoke_swap to swap m_allocated flags. 2023-12-01 18:49:07 +03:00
Andrey Semashev
ecb01f0f1c Added a comment describing is_not_like. 2023-12-01 17:46:05 +03:00
Andrey Semashev
28969ef48a Improved wording of scope_success/fail description. 2023-11-30 19:58:01 +03:00
Andrey Semashev
75e353281a Minor update to a scope_fail example. 2023-11-30 18:56:39 +03:00
Andrey Semashev
d9700c5d96 Added a link to P0052R10 in the docs. 2023-11-28 20:43:01 +03:00
Andrey Semashev
fb6607c097 Removed CMake self-test. Added running tests through CMake in GHA CI.
Running tests via CMake should be enough of a verification that the
library CMakeLists.txt is correct. It also verifies that test/CMakeLists.txt
is correct.
2023-11-28 20:20:05 +03:00