February 28, 2021
Hot Topics:

A C++ Macro to Stub Interface Methods

  • By Kate Gregory
  • Send Email »
  • More Articles »

Finding the Interfaces for Each Class

The VCCodeClass has a handy property called Bases that represents all the bases of the class. Because C++ doesn't use different syntax for inheriting from a base class and implementing an interface, Bases gives you interfaces as well as traditional base classes. You can get the type of these and ask whether they are interfaces or not with the following code:

for (int l=1; l<classes->Count+1; l++)
   VCCodeClass* vcClass = dynamic_cast<VCCodeClass*>
   VCCodeElements* vcCE = static_cast<VCCodeElements*>
   int elems = vcCE->Count;
   for (int i=1; i<elems+1; i++)
      VCCodeElement* element = dynamic_cast<VCCodeElement*>
      VCCodeBase* baseclass = dynamic_cast<VCCodeBase*>(element);
      EnvDTE::CodeType* ct = baseclass->Class;
      if (ct->Kind == EnvDTE::vsCMElement::vsCMElementInterface)
         VCCodeInterface* in = dynamic_cast<VCCodeInterface*>(ct);
//use in somehow
      }    // if this is an interface
      }    // for i: looping through the bases of this class
}          // for l: looping through the classes in this file

This code uses the Item() function to get each class in the classes collection. Then it grabs the Bases collection and loops through the CodeElements returned. The first step is to cast from a CodeElement to a VCCodeElement; from there, I cast to a VCCodeBase. The baseclass has a Class property that is a CodeType. The Kind property of the CodeType represents the kind of class that it is; this code confirms it's an interface. Knowing that, I can cast it to a VCCodeInterface pointer with confidence.

Are the Functions Already Implemented?

Once the interface has been found, the next step is to get all the functions in the interface and find out whether each is already implemented:

VCCodeElements* functions = static_cast<VCCodeElements*>
VCCodeElements* targetFunctions = static_cast<VCCodeElements*>
for (int j=1;j<functions->Count+1;j++)
   EnvDTE::CodeFunction* ifunction =
   if (!targetFunctions->Find(ifunction->Name))
      //Add the function
}    // for j: looping through functions in the interface

This code gets all the functions in the interface, as well as all the functions that have already been implemented in the class being modified, vcClass. It then loops through the functions in the interface, verifying whether each one is in the targetfunctions collection. If it is, the code's work is done—the function has already been added. If it's not, it's time to add this function to the class being modified.

Page 2 of 3

This article was originally published on May 17, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date