| 92 | // Helper to complete function invocation. |
| 93 | template <typename Function, typename Alloc> |
| 94 | static void complete(impl_base* base, bool call) |
| 95 | { |
| 96 | // Take ownership of the function object. |
| 97 | impl<Function, Alloc>* i(static_cast<impl<Function, Alloc>*>(base)); |
| 98 | Alloc allocator(i->allocator_); |
| 99 | typename impl<Function, Alloc>::ptr p = { |
| 100 | detail::addressof(allocator), i, i }; |
| 101 | |
| 102 | // Make a copy of the function so that the memory can be deallocated before |
| 103 | // the upcall is made. Even if we're not about to make an upcall, a |
| 104 | // sub-object of the function may be the true owner of the memory |
| 105 | // associated with the function. Consequently, a local copy of the function |
| 106 | // is required to ensure that any owning sub-object remains valid until |
| 107 | // after we have deallocated the memory here. |
| 108 | Function function(static_cast<Function&&>(i->function_)); |
| 109 | p.reset(); |
| 110 | |
| 111 | // Make the upcall if required. |
| 112 | if (call) |
| 113 | { |
| 114 | static_cast<Function&&>(function)(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl_base* impl_; |
| 119 | }; |