www.digitalmars.com         C & C++   DMDScript  

c++ - template implements __uuidof

reply nyra sohu.com writes:
I implements MSC++ keyword __uuid partly with template.
It May be usefull to port ATL and lastest MFC library to DMC++.

The source code.

#include <ole2.h> //for type REFIID ...

template <typename T> struct UUID_Traits;

#define DEFINE_IID_TRAITS(interface) \
template<> struct UUID_Traits<interface>\
{\
static REFIID GetIID()\
{\

}\
};

DEFINE_IID_TRAITS(IUnknown)
DEFINE_IID_TRAITS(IDispatch)

template <typename T> inline
REFIID uuidof(T&)
{
return UUID_Traits<T>::GetIID();
}

template <typename T> inline
REFIID uuidof(T*)
{
return UUID_Traits<T>::GetIID();
}

//-0------testing------0-

#include <stdio.h>

void print_iid(REFIID riid)
{
LPOLESTR test;
StringFromCLSID(riid, &test);
printf("%ws\n", test);
CoTaskMemFree(test);
}

int main()
{
CoInitialize(0);
IDispatch* p1;
IUnknown& p = *p1;  

print_iid(uuidof(p));
print_iid(uuidof(p1));   

CoUninitialize();
}
Feb 01 2005
parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
COMSTL has had this for several years, in the form of its IID_traits<> 
class template, where it forms an integral part of the interface_cast<> 
mechanisms. It also resolves to using __uuidof on compilers that support 
it. See comstl_interface_cast.h, comstl_interface_traits.h & 
comstl_interface_traits_std.h

The full technique's explained in detail in Chapter 19 of my book, 
Imperfect C++ (http://www.awprofessional.com/titles/0321228774/).

Cheers

Matthew Wilson

Author: "Imperfect C++", Addison-Wesley, 2004
    (http://www.imperfectcplusplus.com)
Contributing editor, C/C++ Users Journal
    (http://www.synesis.com.au/articles.html#columns)
STLSoft moderator
    (http://www.stlsoft.org)

-------------------------------------------------------------------------------

"So far, C++ is the best language I've discovered to say what I want to 
say" -- Alex Stepanov

-------------------------------------------------------------------------------




<nyra sohu.com> wrote in message news:ctpu8p$2bqr$1 digitaldaemon.com...
I implements MSC++ keyword __uuid partly with template.
 It May be usefull to port ATL and lastest MFC library to DMC++.

 The source code.

 #include <ole2.h> //for type REFIID ...

 template <typename T> struct UUID_Traits;

 #define DEFINE_IID_TRAITS(interface) \
 template<> struct UUID_Traits<interface>\
 {\
 static REFIID GetIID()\
 {\

 }\
 };

 DEFINE_IID_TRAITS(IUnknown)
 DEFINE_IID_TRAITS(IDispatch)

 template <typename T> inline
 REFIID uuidof(T&)
 {
 return UUID_Traits<T>::GetIID();
 }

 template <typename T> inline
 REFIID uuidof(T*)
 {
 return UUID_Traits<T>::GetIID();
 }

 //-0------testing------0-

 #include <stdio.h>

 void print_iid(REFIID riid)
 {
 LPOLESTR test;
 StringFromCLSID(riid, &test);
 printf("%ws\n", test);
 CoTaskMemFree(test);
 }

 int main()
 {
 CoInitialize(0);
 IDispatch* p1;
 IUnknown& p = *p1;

 print_iid(uuidof(p));
 print_iid(uuidof(p1));

 CoUninitialize();
 }

 
Feb 02 2005