Use C ++ template for multi-vendor interface adaptation

Source: Internet
Author: User

Many may have read Asimov's sci-fi novels, especially the robot series. Whether it's the Three Laws of robotics, humanoid robots, or robots that look ugly but have a mind to read, I'm amazed at the amazing imagination of the author.

Creating a robot is no different to me, but it is still possible to design a virtual robot. Well, I really want a robot that can chat with me, So I designed a robot class robottalker, which may be inherited from a certain robot class, but no matter how much it is. I just hope this robot can chat with me normally to eliminate the loneliness of the otaku and give me a sense of presence.

I have a special requirement for a virtual robot, that is, to allow it to speak in multiple languages. Sometimes it can chat with me in Mandarin, and sometimes it can also help its parents to their parents in their hometown, sometimes you can exchange technology with programmers outside China on the Internet. The right is a translation. However, I only speak Chinese, and I can only develop its Chinese Language module. The default language interface I provided to robottalker is defacompany company. h:

# Ifndef default_interface_h # define default_interface_hvoid speakinchinese (); // the specific implementation of the function may be in another. cpp file or in a separate DLL # endif

After I posted my chatbot information, I received a letter from two netizens who wanted to provide me with English and Russian modules, their interfaces adopt the same form as my default interfaces.

 

Companyengand. H provides English interfaces:

#ifndef Company_England_h#define Company_England_hvoid SpeakInEnglishUK();#endif

Companyrussia. H provides Russian interfaces.

#ifndef Company_Russia_h#define Company_Russia_hvoid SpeakInRussian();#endif

Similarly, their specific implementations are provided in the form of DLL.

 

However, the two netizens gave me an exchange condition, that is, after my robots are ready, they also need a copy, and they only want their own language version. It seems that I have to find a way to adapt to the different needs of the three of us. First, assign a vendor code for the language modules of the three of us to determine the robot implementation version based on companycode.

 

At the beginning, I designed the following robottalker in the traditional C language.

Class robottalker {public: robottalker () {}; virtual ~ Robottalker () {}; void speaklanguage () ;}; // function implementation: Compile different interfaces according to macro definition. Void robottalker: speaklanguage () {# ifdef defacompcompanycode speakinchinese (); # endif # ifdef companyrussiacode speakinrussian (); # endif # ifdef comanyengandcode Merge (); # endif}

Depending on the macro to compile different versions of this implementation form, no matter how awkward it looks, but at least it has met the needs of the three of us. However, I do not dare to publish this code to my customers. In the case of a macro, the other two language interfaces will not be compiled, and every time I publish the code, I have to change the macro definition, compiled three times.

Is there a way to do this once and for all? The C ++ template is a good method to replace this macro compilation. I designed a template function for the robot to specify the type as the template parameter. My Chinese Module Interface serves as the general form of the template, while English and Russian serve as the special version of the template, in this way, when the customer code calls the robot code, you only need to pass companycode as the template parameter to robottalker.

 

First, let's look at how the robot's language interface is implemented:

# Ifndef robot_reader_h # define robot_reader_h # include "defaultcompany. H "# include" companyengand. H "# include" companyrussia. H "// manufacturer code const int defaultcompanycode = 1; const int companyengandcode = 100; const int companyrussiacode = 200; // chatbot class robottalker {public: robottalker (){}; virtual ~ Robottalker () {}; template <int companycode> void speaklanguage () ;}; // The general version of function implementation uses the Chinese template by default. <int companycode> void robottalker :: speaklanguage () {speakinchinese () ;}// Special English version template <> void robottalker: speaklanguage <companyengandcode> () {speakinengishuk ();} // Russian special version template <> void robottalker: speaklanguage <companyrussiacode> () {speakinrussian () ;}# endif

I only need one set of robot code, and all interfaces are always in the scope of compiler check. That is to say, all manufacturers' interfaces are always compiled. Moreover, customers who only use English modules will not compile the other two interfaces into executable code and will not occupy extra memory space, which has the same effect as macros.

It is very convenient for customers to use this robottalker to implement their own language-defined robots.

# Include "robottalker. H "int main () {robottalker robot; robot. speaklanguage <companyengandcode> (); // speaks English robot. speaklanguage <defacompcompanycode> (); // speaks Chinese robottalker trobotrussian; robot. speaklanguage <companyrussiacode> (); // speaks Russian}

This article only discusses the needs of multi-vendor interfaces (with the same interface format) to introduce the power of the C ++ template. As for topics like robots that want to implement different functions, that is the scope of the design model. In addition, there is also an implementation method to address the above requirements, that is, to use the interface functions of different vendors according to the vendor code, initialize a function pointer, and set your own vendor code when the customer code is called, the called function pointer is naturally your own interface function. But I always think the form of function templates is more elegant.

Related Article

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.