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

Source: Internet
Author: User

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

The main content of this article is: A class faststring that implements the fast lookup function, which slowly evolves into a COM component after a small requirement.

Class Faststring implements a quick Find string function, fast to the time complexity is O (1), we first regardless of how the author is implemented, it is estimated that the space to change time. Because this class looks up the string quickly, so the author of this class as a product, the source of the way to sell to the needs of vendors , manufacturers feel good after use, but some manufacturers want to get the function of string length, they think strlen (str) speed is too slow, After all, this function gets the length of the string is linear, the time complexity is O (N), so the author decided to modify 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 manufacturers, the clever writer quickly thought of a 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 quickly realized the needs of the manufacturer, presumably as follows:

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 seamless test, the author proudly distributed his work to the vendors willing to pay again, the manufacturers used a very big fire, a variety of inexplicable problems, in the wake of the various vendors, the author found his work defects, so decided to go to the com road .

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

Manufacturers take the author's source code, the way to the source code and their own other codes compiled into a DLL file, and then let their products upgrade, upgrade is simply to cover the DLL file, so the manufacturer's product upgrade after the hang. Because faststring may be instantiated in multiple DLLs, faststring consumes 4 bytes of memory in these DLLs, and the new version of Faststring occupies 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 faststring is 8 bytes, and the other DLLs are 4 bytes, if the faststring is passed across the library, Use a 4-byte object as a 8-byte object, which is not hanging.

The clever author soon realized his COM component, the source code is probably the following, do not wonder why the author of the COM road so downwind, so quickly out of the works.

#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 refer to the Game Programming network www.cgzhw.com Information, hereby noted.

1: The author does not sell to the vendor in the source code, but sells a vendor in the form of a header file and a library, and the vendor can link the author's library in a static/dynamic way.

2: The author does not allow the vendor to instantiate his faststring everywhere, my lovely faststring. Instead, an export function instantiates the faststring and returns ifaststring so that there is no problem with faststring instance size in different DLLs. All instances are now created in the author's DLL.

3: Questions about recycling faststring? The author just initially wanted 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 abstract base classes as interfaces", since each compiler may have different implementations, that is, destructors cannot be defined as virtual functions, because 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. The location of the other virtual methods in the virtual method table is consistent with the declaration of the virtual method, which is stored in the virtual method in the order declared.

Because type conversions and reference counts are required for each interface, they are proposed to be placed at the top level, allowing all interfaces to inherit from it.

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

If 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 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, and do not need to update all, obsolete interface removal will cause the same problem.



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.