"com essence" com is a better C + + experience sharing

Source: Internet
Author: User

Yesterday read the first chapter of the "COM Essence", "com is a better C + +", think it is necessary to do some notes, so organized into this article. I believe you deserve it.

The main content of this article is: a high-speed lookup function of the class faststring, after a small demand, slowly evolved into a COM component of the process.

Class Faststring implements a high-speed lookup string function. Fast to the time complexity is O (1), we first regardless of how the author is implemented, is expected to be through space to change time.

Because this class finds the string very quickly, the author takes the class as a product and sells it to the required vendor in the form of source code . The manufacturers feel very good after use. But some manufacturers want to get the function of string length, they think strlen (str) speed is too slow, after all, the function gets string length is linear, time complexity is O (N), so the author decided to change his faststring. His heart has been telling himself: my faststring must be fast.

Let's take a look at what the author Faststring looks like:

Class Faststring{public:      faststring (const char* str);      Faststring (void);       int Find (const char* str);p rivate:      char* m_str;};

Do not underestimate this class, it is fast to find the string (I do not know why it is so fucking fast). After listening to the needs of the manufacturer, the clever writer quickly thought of a very good solution, through a variable len to save the length of the string, through a function length return variable len, time complexity but O (0) Oh, so the author very quickly realized the needs of manufacturers. Probably for example the following:

Class Faststring{public:      faststring (const char* str);      Faststring (void);      int Length ();//New      int Find (const char* str);p rivate:      char* m_str;      int len;//new};

After a flawless test. The author proudly distributed his work to vendors willing to pay again. The manufacturers used a very big fire, there have been a variety of inexplicable problems, after the roar of the various manufacturers, the author found his work defects, so decided to go to the Road of COM .

Let's see if the manufacturer used the author's faststring to hang up.

After the manufacturers have taken the author's source code. Compile the DLL file in the same way as the source code and your other code, and then let your product upgrade. Upgrade is simply to overwrite the DLL file, so the manufacturer's product upgrade after the hang.

Since faststring may instantiate multiple files in multiple DLLs, faststring consumes 4 bytes of memory in those DLLs, and the new version number faststring takes up 8 bytes of memory, and the vendor only covers the DLL where faststring is located. Instead of overwriting all DLLs that use faststring. Because the DLL where faststring is created is 8 bytes, while the other DLLs are 4 bytes. Assuming that faststring is passed across the library, a 4-byte object is used as a 8-byte object, which is not yet hanging.

The clever author realizes his COM component very quickly, the source code is probably the following, do not wonder why the author of the COM road so smooth. Out of the works so soon.

#pragma onceclass iextensibleobject{public:    virtual void* dynamic_cast (const char* str) =0;    virtual void AddRef () =0;    virtual void Release () = 0;}; Class Ifaststring:public Iextensibleobject{public:    virtual int Length (void) =0;    virtual int Find (const char* str) = 0;}; Class Faststring:public Ifaststring{public:    faststring (const char* str=null);    Virtual void* dynamic_cast (const char* str);    virtual void AddRef ();    virtual void Release ();    virtual int Length ();    virtual int Find (const char* str);    ~faststring ();p rivate:    char* m_str;    int Len;    int m_cptrs;//reference count};//export function extern "C" __declspec (dllimport) ifaststring*  createfaststring (const char* psz);

Press CTRL + C to copy the code

The author's COM component did a few things and finally implemented an incremental update. The article references the Game Programming network www.cgzhw.com data. This is hereby noted.

1: The author does not sell to the vendor in source code mode. Instead, sell a vendor in the form of a header file and a library. Vendors can link authors ' libraries in a static/dynamic way.

2: The author does not allow the vendor to instantiate his faststring everywhere. My lovely faststring.

Instead, the faststring is instantiated by an export function, and the ifaststring is returned. This will not have the same problem as the faststring instance size in different DLLs. Now all instances are created in the author's DLL.

3: Questions about recycling faststring? The author just started to want to delete the createfaststring return pointer directly, but in order to implement the COM component, at this time the faststring is not his own, he inherited and implemented a number of interfaces, because the interface between the conversion to convert, do not know which pointer is removed. The author then decides to destroy the faststring by using a reference counting method.

4: Why should I implement dynamic_cast?

Rtti is an extremely relevant feature of the compiler, each compiler vendor is unique to the implementation of RTTI, which greatly destroys the "compiler independence derived from an abstract base class as an interface", since each compiler may have different implementations, that is, destructors cannot be defined as virtual functions due to different compilers. Virtual functions are not in the same position in the virtual method table, and some of the compilers are placed at the top of the list, which results in a different compiler's location in the virtual method table when the virtual method is compiled.

Therefore, destructors cannot be defined as virtual, and other public interfaces must be defined as virtual. Other virtual methods are consistent in the location of the virtual method table and the declaration of the virtual method, which is stored in the virtual method in the order in which they are declared.

Because type conversions and reference counts are required for each interface, they are presented to the topmost level. Let the entire interface inherit it.

5: The new interface can only be added to the last surface, the discarded interface cannot be deleted.

Suppose the new interface is inserted in the middle. Then the address of the partial interface in the virtual method table will change. The new version number of the DLL can not be compatible with the published program, you can not implement an incremental upgrade, that is, only to overwrite a DLL, without all the need to update, discarded interface removal will cause the same problem.



"com essence" com is a better C + + experience sharing

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.