Debian Codegraph @

common/main/d_construct.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 #pragma once
9 #include <functional> // std::invoke
10 #include <memory> // std::addressof
11 #include <new> // placement new
12 #include <type_traits> // std::same_as
13 #include <utility> // std::forward
14
15 /* Given a live variable `result` of type `result_type`, destroy it, and
16 * construct a new `result_type` into that location from the result of calling
17 * `std::invoke(invocable, args...)`. This is similar to `std::construct_at`,
18 * but:
19 * - `std::construct_at` requires the storage not to be live, since it does not
20 * invoke the destructor before constructing the new object.
21 * - `std::construct_at` requires its caller to provide the arguments passed to
22 * the `result_type` constructor. In the specific case that the caller wants
23 * to use an `invocable` that returns a `result_type` by value, the
24 * `std::construct_at` will be called with a `result_type` that is then
25 * used to construct the `result`. This extra layer defeats copy elision.
26 * By performing the invocation from inside `reconstruct_at`, the compiler is
27 * given the ability to apply copy elision.
28 */
29 template <typename result_type, typename Invocable, typename... Args>
30 requires(
31 requires(Invocable &&invocable, Args &&... args) {
32 /* Require that `args` can be used to invoke `invocable`. Failure to
33 * satisfy this would lead to a compile error later. Checking it here
34 * is not required for correctness, but produces better reporting of
35 * errors, and is a side effect of the main test.
36 *
37 * Require that the type of the result of that invocation is exactly
38 * `result_type`. This is required for optimal results, since an
39 * `invocable` that returns a type `T` that satisfies
40 * `(!std::same_as<T, result_type> && std::convertible_to<T, result_type>)`
41 * could satisfy the placement new in the body by converting the `T` to
42 * `result_type`, but would generate undesirable code. Consider:
43
44 ```
45 struct A { int a[100]; };
46 struct B : A { int b; };
47 B invocable();
48 void use_reconstruct(A &a)
49 {
50 reconstruct_at(a, invocable);
51 }
52 ```
53
54 * The call to `invocable` will return a `B`, which cannot be
55 * constructed directly into `a`, so the compiler will construct an
56 * anonymous temporary `B`, then construct `a` from that temporary `B`.
57 * In the shown example, that would require local storage for a `B`,
58 * and would copy the returned `int[100]` from local storage to the
59 * caller's `a`. Avoid that overhead by rejecting parameters that
60 * would require the temporary.
61 */
62 { std::invoke(std::move(invocable), std::forward<Args>(args)...) } -> std::same_as<result_type>;
63 /* Require that the object to be replaced has a trivial destructor or
64 * that the construction of the replacement object be `noexcept`. A
65 * non-trivial destructor might malfunction if the object is destroyed
66 * twice. Normally, there should be only one call, but if
67 * `result.~result_type()` finishes, then an exception is thrown during
68 * the call to `invocable`, the caller of this function could try to
69 * destroy `result` again, while it is still in the state that the
70 * destructor left it. Therefore, require that if `result_type` has a
71 * non-trivial destructor, then construction cannot throw, so that
72 * there cannot be an exception during the period when `result` is in
73 * an inconsistent state.
74 *
75 * This check is overly cautious, since a non-trivial destructor might
76 * be safe to call twice. There is no generic way to detect that and,
77 * for now, there are no types that fail to satisfy this check and for
78 * which `reconstruct_at` would be useful, so use the overly cautious
79 * check.
80 */
81 requires(std::is_trivially_destructible_v<result_type> || noexcept(result_type{std::invoke(std::move(invocable), std::forward<Args>(args)...)}));
82 }
83 )
84 static inline void reconstruct_at(result_type &result, Invocable &&invocable, Args &&... args)
85 {
86 /* Destroy the old object, since its storage will be reused. */
87 result.~result_type();
88 /* Construct a new `result_type` in the location `result`, using a
89 * constructor that takes the result of `std::invoke(...)`. Since the
90 * above `requires` clause requires that `std::invoke` return a
91 * `result_type`, this should result in copy elision constructing the new
92 * `result_type` directly into `result`, bypassing any copy constructor or
93 * move constructor.
94 */
95 new(static_cast<void *>(std::addressof(result))) result_type{std::invoke(std::move(invocable), std::forward<Args>(args)...)};
96 }