Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the engineering methods of C++ interface class

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the engineering methods of C++ interface". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The interface is divided into the calling interface and the callback interface. The calling interface mainly realizes the function of module decoupling. As long as the interface compatibility is maintained, the upgrade within the module can be imperceptible to users. Good interface layering helps business teams to develop efficiently.

Callback API is mainly used to notify users of asynchronous events in the system. The interface form of the system is predefined and registered by the user, and the specific call time is determined by the system.

Call interface

Suppose there is a network sending module class Network, which is defined as follows:

Class Network {public:bool send ();}

Virtual function

The most commonly used are virtual functions, which can be used to define Network interface classes:

Class Network {public: virtual bool send () = 0 static Network* New (); static void Delete (Network* network_);}

Send is defined as a virtual function, which is implemented by an inherited class (such as inherited by a PLC module or an Ethernet module). A subclass object is created statically and returned to the business with the pointer of the base class Network. Resources follow the principle of who creates and destroys, and the base class also provides Delete methods to destroy objects. Because the object destruction is encapsulated inside the interface, the Network interface class does not need a virtual destructor.

The code is easy to read by using virtual functions, but the virtual functions are expensive (indirect calls using virtual function table pointers) and cannot be optimized inline during compilation. In fact, the calling interface can determine which function to use at compile time, and the dynamic characteristics of virtual functions are not needed. In addition, because the virtual function is called indirectly by the pointer of the virtual function table, adding the virtual function will cause the index of the function address table to change, and the new interface is not compatible with the old interface at the binary level. And because users may inherit the Network interface class, it is also risky to add virtual functions at the end, so once the virtual function interface is released and launched, it is basically impossible to modify.

A pointer to the implementation

You can use pointers to the implementation to define Network interface classes:

Class NetworkImpl;class Network {public: bool send (); static Network* New (); Network () ~ Network (); private: NetworkImpl* impl;}

The specific implementation of Network is completed by NetworkImpl. By using the pointer to the implementation to define the interface, the creation and destruction of interface class objects can be taken care of by the user to better manage the object life cycle.

In addition, this method has strong versatility, and the new interface will not affect the binary compatibility, which is beneficial to the rapid iteration of the project.

However, this method still adds a layer of calls, which still has a slight impact on performance, and does not comply with the zero-overhead principle of C++.

Hidden subclass

The idea of hidden subclass is very simple, the goal of the interface is to decouple, mainly to hide the implementation, that is, to hide the member variables of the interface class. If the member variables of the interface class can be transferred to another hidden class, then the interface class does not need any member variables, then the purpose of hiding the implementation is achieved. The specific implementation methods are as follows:

Class Network {public: bool send (); static Network* New (); static void Delete (Network* network_); protected: Network (); ~ Network ();}

The Network interface class has only member functions and no member variables. Provide a static method New to create the object, and the Delete method to destroy the object. The implementation of the New method creates an object of the hidden subclass NetworkImol and returns it as a Network pointer of the parent class. The member variable of the Network class is defined in the NetworkImol class, and the Network class is declared as friend:

Class NetworkImol:public Network {friend class Network; private: / / define member variables of the Network class}

The NetworkImol subclass object is created in the implementation of the Network class and returned as a parent class pointer to access member variables by casting this to a pointer of the subclass NetworkImol type:

Bool Network::send () {NetworkImpl* impl = (NetworkImpl*) this; / / access member variables through impl to realize the function of Network} static Network* New () {return new NetworkImpl ();} static Delete (Network* network) {delete (NetworkImpl*) network;}

This method accords with the zero-overhead principle of C++ and also conforms to the binary compatibility.

There is a Trait feature in the Rust language, and you can implement a Trait outside the class (without modifying the class code). Then C++ can also refer to implementing the Trait function. Suppose you need to implement the serialization data in the Network class, and redesign the Network API. The Serializable class is defined as follows:

Class Serializable {public: virtual void serialize () const = 0;}

The Network class is defined as follows:

Class Network {public: bool send (const char* host, uint16_t port,constSerializable& buf);}

The Serializable interface is similar to Trait in Rust. Now any object that implements the Serializable interface class can complete the data sending function by calling the Network class interface. So the problem is that joining the project iteration requires adding int data to be sent through the Network class, so how do you implement the Serializable interface without modifying the class definition? It's simple:

Class IntSerializable: public Serializable {public:IntSerializable (const int i): intthis (I) {} virtual void serialize () const override {buffer + = std::to_string (* intthis);} private: const int* const intthis;}

You can then send int data over Network:

Network* network = Network::New (); int iTunes 1 * network-> send (ip,port, IntSerializable (I))

The non-intrusive interface distinguishes the class from the interface, and the data in the class contains only member variables and no virtual function table pointers, so the class will not introduce n virtual function table pointers because it implements n interfaces. However, the interface contains only virtual function table pointers, not data members. Type conversion is carried out between classes and interfaces by implementing classes. Classes introduce virtual function table pointers only when they act as interfaces, and not when they do not act as interfaces. It is in line with the C++ zero-cost principle.

The Rust compiler records which Trait is implemented by each class through the impl keyword, so the compiler can automatically convert the object to the corresponding Trait type when assigning values. However, C++ compilers such as Gateway + do not record these conversion information, so you need to convert the type manually. In essence, it is still through the code to help the compiler record which Trait is implemented by each interface class, and use the inheritance of template classes to achieve similar "static polymorphism" functions at compile time.

This is the end of the introduction of "what are the engineering methods of C++ interface class". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report