GUID Maps
Implementing
QueryGuid() is a tedious and repetitive task, so using macros to write most of the code is convenient. A GUID map is a set of macros that collectively implement the
QueryGuid() function. They are nearly identical to ATL’s
BEGIN_COM_MAP and
END_COM_MAP macros, which implement
QueryInterface().
Example 4 shows how you can use GUID maps to implement
QueryGuid().
Example 4 – Using a GUID map
class CCow : public IAnimal
{
public:
BEGIN_GUID_MAP(CCow)
GUID_ENTRY(IAnimal)
GUID_ENTRY(IQueryGuid)
END_GUID_MAP
…
};
The macros expand out to the same implementation of CCow::QueryGuid() shown in the previous sample.
In certain cases, a class may inherit an interface from more than one base class. That will produce an ambiguous reference in the GUID map that you must resolve with the
GUID_ENTRY2 macro.
Example 5 shows the
GUID_ENTRY2 macro used to resolve an ambiguous reference to
IQueryGuid.
Example 5 – Resolving an ambiguous reference in a GUID map
class IFood : public IQueryGuid
{
public:
virtual void BeConsumed() = 0;
};
class CCow : public IAnimal, public IFood
{
public:
BEGIN_GUID_MAP(CCow)
GUID_ENTRY(IAnimal)
GUID_ENTRY2(IQueryGuid, IAnimal)
END_GUID_MAP
…
};