Page 183 - DCAP507_SYSTEM_SOFTWARE
P. 183

Unit 11: Programming Languages Concept (II)




          This shows that the handler must not be called directly from within the initiating function,  Notes
          although the asynchronous operation completes instantly.
          A bound completion handler is a handler object that includes a copy of a user-supplied handler,
          where the user-supplied handler accepts one or more arguments. The bound completion handler
          does not accept any arguments, and includes values to be passed as arguments to the user-
          supplied  handler.  The  bound  completion  handler  forwards  the    handler_allocate(),
          handler_deallocate(), and  handler_invoke() calls to the analogous functions for the user-supplied
          handler. A bound completion handler fulfills the requirements for a completion handler.


                 Example: A bound completion handler for a ReadHandler may be executed as follows:
          template<class ReadHandler>
          struct bound_read_handler
          {

            bound_read_handler(ReadHandler handler, const error_code& ec, size_t s)
              : handler_(handler), ec_(ec), s_(s)
            {
            }



            void operator()()
            {
              handler_(ec_, s_);

            }


            ReadHandler handler_;
            const error_code ec_;

            const size_t s_;
          };


          template<class ReadHandler>

          void* asio_handler_allocate(size_t size,
                                      bound_read_handler<ReadHandler>* this_handler)
          {
            using namespace boost::asio;

            return asio_handler_allocate(size, &this_handler->handler_);
          }


          template<class ReadHandler>

          void asio_handler_deallocate(void* pointer, std::size_t size,


                                           LOVELY PROFESSIONAL UNIVERSITY                                   177
   178   179   180   181   182   183   184   185   186   187   188