Thrift default Transport class Ttransportdefaults and virtual transport class Tvirtualtransport

Source: Internet
Author: User
Tags abstract inheritance

The default transport class Ttransportdefaults provides a default implementation of the abstract class Ttransport, Implementing a Non-virtual method (*_virt) Read (), ReadAll (), write (), borrow (), and consume (). The base class Ttransport always invokes the corresponding virtual function, and the default transport class is implemented in the default manner instead of overwriting the corresponding virtual functions.

In fact, the primary function of this default transport class is to act as a tvirtualtransport of the virtual transport class, so why do we need this class as the parent of the virtual transport class instead of using the abstract base class directly? This is determined by how the virtual base class is implemented, because in order to avoid the use of virtual base classes, the virtual transport class takes the form of a template to implement multiple inheritance (at the same time inheriting from two classes), that is, you can inherit from one of the default classes and another class is passed by template parameters The function of the default transport class is to prevent recursive calls (dead loops) from subclasses of methods that do not implement the default transport class implementation.

The implementation of the virtual transport class has been introduced above, the benefit of this implementation is to avoid the adoption of virtual inheritance, and virtual inheritance will cause a certain performance loss. The following is the implementation code for the virtual base class (omitting part of the function):

Template <class Transport_, class 

super_=ttransportdefaults>
class Tvirtualtransport:public Super_ { Public
 :
  //Implement virtual functions to invoke template parameters pass in a specific transport class of non-virtual functions virtual
  uint32_t read_virt (uint8_t* buf, uint32_t len) { Return
    static_cast<transport_*> (this)->read (buf, Len);
  }
   ......
}

It took so much of a wrinkle to get rid of the virtual inheritance, it seems that it is not easy to improve the efficiency of code execution, but it also proves that ingenious programming ideas and programming skills are also important ways to improve code efficiency. Although the above does not adopt virtual inheritance, its extensibility is still not affected by a little bit. For example, if we want to implement one of our own transport classes for a particular purpose, we only need to inherit from the virtual transport class, such as: Class Mytransport:public tvirtualtransport<mytransport> {...} And then implement your own read (), ReadAll (), and so on (non-virtual) functions.

Both the default transport class and the virtual transport class are the help classes that implement the entire transport-layer architecture, there is no specific transfer capability implemented, but the specific transport classes described later are based on these two classes (inherited), so we begin to look at some important implementations of the transport classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.