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

Method create

example/cpp14/operations/c_callback_wrapper.cpp:52–84  ·  view source on GitHub ↗

Create the state using the handler's associated allocator.

Source from the content-addressed store, hash-verified

50
51 // Create the state using the handler's associated allocator.
52 static read_input_state* create(Handler&& handler)
53 {
54 // A unique_ptr deleter that is used to destroy uninitialised objects.
55 struct deleter
56 {
57 // Get the handler's associated allocator type. If the handler does not
58 // specify an associated allocator, we will use a recycling allocator as
59 // the default. As the associated allocator is a proto-allocator, we must
60 // rebind it to the correct type before we can use it to allocate objects.
61 typename std::allocator_traits<
62 boost::asio::associated_allocator_t<Handler,
63 boost::asio::recycling_allocator<void>>>::template
64 rebind_alloc<read_input_state> alloc;
65
66 void operator()(read_input_state* ptr)
67 {
68 std::allocator_traits<decltype(alloc)>::deallocate(alloc, ptr, 1);
69 }
70 } d{boost::asio::get_associated_allocator(handler,
71 boost::asio::recycling_allocator<void>())};
72
73 // Allocate memory for the state.
74 std::unique_ptr<read_input_state, deleter> uninit_ptr(
75 std::allocator_traits<decltype(d.alloc)>::allocate(d.alloc, 1), d);
76
77 // Construct the state into the newly allocated memory. This might throw.
78 read_input_state* ptr =
79 new (uninit_ptr.get()) read_input_state(std::move(handler));
80
81 // Release ownership of the memory and return the newly allocated state.
82 uninit_ptr.release();
83 return ptr;
84 }
85
86 static void callback(void* arg, const char* result)
87 {

Callers

nothing calls this directly

Calls 5

get_associated_allocatorFunction · 0.85
allocateFunction · 0.50
getMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected