Welcome to weblogs.com.pk Sign in | Join | Help

Introduction to Components: Theory behind Vendor Independent Component

C/C++ Compiler Ambiguities

 

C/C++ was extensively used in last decade for software engineering; it impacted the component based software architecture. As discussed earlier, there was a need that C/C++ compiler use the standardize approach, so that the component being created using one compiler can work with the component created with another. The compiler vendors and the C/C++ community came with certain directives that can be used in the code. These directives instruct the compiler to use certain approaches to reduce the ambiguities. One such directive is _stdcall that results:

 

  • Arguments will be passed from right to left, they will be passed by value unless a pointer or reference type is passed
  • The function that’s being called will POP the arguments
  • The this pointer will also be pushed to the stack.

 

Memory Layout Issues

 

Another problem was the uniform memory layout for the objects of the classes and structures. The class/structure attributes (variables) and virtual methods take part in the memory layout. The methods, including constructor and destructor don’t participate in the memory layout. If the class contains the virtual members, it contains the pointer called vptr that’s pointing to the entry point of the virtual function table (vtbl) of the virtual member functions. Almost all the compilers use this approach. vtbl is an array of pointers that points to the function entry address of each virtual function in the given class.

 

Using virtual methods solves the name mangling issue that was discussed earlier but at the cost of few extra CPU cycle. Using virtual methods has its own problems, for instance, vptr is placed first to the class members or is placed in the last? What about multiple inheritances? What’s the order of vtbl entries? What’s the order of class’s attributes (variables)?

 

To workout these problems COM community adopted certain rules, like the class will have no data member, it will only contain vptr, the class can only inherit from a single base class and the vtbl entries will be mapped to the order of the virtual members in the code. So to have a good class that can be used in a component we need a class definition that full fills the following rules

 

  • Contains only pure virtual methods
  • Doesn’t contain any overloaded virtual methods
  • Doesn’t contain any member variables
  • Derives from at most one base class

 

Such a class is called abstract base class. The actual implementation will be in a corresponding C++ class deriving from such an abstract base class.

 

Instantiation of Abstract Base Class

 

The next problem is that such an abstract base class cannot be instantiated. (C/C++ language limitation) To achieve the compiler independence, a possible workout is that we export two additional functions that create and delete the instance of the implementation class and return to the client. The client thus will only know the abstract base class (its memory layout) and the two functions.

 

This is not the end of problems J, there are few more left, that I will try to cover in my next post. Stay tuned

Published Thursday, July 29, 2004 10:19 PM by khurram
Filed under:

Comments

No Comments

New Comments to this post are disabled