MCPcopy Create free account
hub / github.com/boostorg/asio / callback

Method callback

example/cpp20/operations/c_callback_wrapper.cpp:86–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

84 }
85
86 static void callback(void* arg, const char* result)
87 {
88 read_input_state* self = static_cast<read_input_state*>(arg);
89
90 // A unique_ptr deleter that is used to destroy initialised objects.
91 struct deleter
92 {
93 // Get the handler's associated allocator type. If the handler does not
94 // specify an associated allocator, we will use a recycling allocator as
95 // the default. As the associated allocator is a proto-allocator, we must
96 // rebind it to the correct type before we can use it to allocate objects.
97 typename std::allocator_traits<
98 boost::asio::associated_allocator_t<Handler,
99 boost::asio::recycling_allocator<void>>>::template
100 rebind_alloc<read_input_state> alloc;
101
102 void operator()(read_input_state* ptr)
103 {
104 std::allocator_traits<decltype(alloc)>::destroy(alloc, ptr);
105 std::allocator_traits<decltype(alloc)>::deallocate(alloc, ptr, 1);
106 }
107 } d{boost::asio::get_associated_allocator(self->handler_,
108 boost::asio::recycling_allocator<void>())};
109
110 // To conform to the rules regarding asynchronous operations and memory
111 // allocation, we must make a copy of the state and deallocate the memory
112 // before dispatching the completion handler.
113 std::unique_ptr<read_input_state, deleter> state_ptr(self, d);
114 read_input_state state(std::move(*self));
115 state_ptr.reset();
116
117 // Dispatch the completion handler through the handler's associated
118 // executor, using the handler's associated allocator.
119 boost::asio::dispatch(state.work_.get_executor(),
120 boost::asio::bind_allocator(d.alloc,
121 [
122 handler = std::move(state.handler_),
123 result = std::string(result)
124 ]() mutable
125 {
126 std::move(handler)(result);
127 }));
128 }
129
130private:
131 Handler handler_;

Callers

nothing calls this directly

Calls 6

get_associated_allocatorFunction · 0.85
stringFunction · 0.85
dispatchFunction · 0.50
resetMethod · 0.45
get_executorMethod · 0.45

Tested by

no test coverage detected