Back to index

lightning-sunbird  0.9+nobinonly
wrapped-clr.cpp
Go to the documentation of this file.
00001 #include <nsIInterfaceInfoManager.h>
00002 #include <xptcall.h>
00003 #include <xpt_xdr.h>
00004 #include <nsCRT.h>
00005 
00006 #include <stdio.h>
00007 
00008 class WrappedCLR : public nsXPTCStubBase
00009 {
00010 public:
00011     typedef int (*MethodInvoker)(int, nsXPTCMiniVariant *);
00012 
00013     NS_DECL_ISUPPORTS
00014     NS_IMETHOD GetInterfaceInfo(nsIInterfaceInfo **aInfo);
00015     NS_IMETHOD CallMethod(PRUint16 methodIndex,
00016                           const nsXPTMethodInfo *info,
00017                           nsXPTCMiniVariant *params);
00018     WrappedCLR(MethodInvoker del, const nsIID &id) :
00019         mIID(id), mDelegate(del) { }
00020     virtual ~WrappedCLR() { }
00021 private:
00022     nsIID mIID;
00023     MethodInvoker mDelegate;
00024 };
00025 
00026 NS_IMPL_ISUPPORTS1(WrappedCLR, nsISupports);
00027 
00028 NS_IMETHODIMP
00029 WrappedCLR::GetInterfaceInfo(nsIInterfaceInfo **info)
00030 {
00031     extern nsIInterfaceInfoManager *infomgr;
00032     return infomgr->GetInfoForIID(&mIID, info);
00033 }
00034 
00035 NS_IMETHODIMP
00036 WrappedCLR::CallMethod(PRUint16 methodIndex,
00037                        const nsXPTMethodInfo *info,
00038                        nsXPTCMiniVariant *params)
00039 {
00040     fprintf(stderr, "Calling %s via %p\n", info->GetName(), mDelegate);
00041     return mDelegate(methodIndex, params);
00042 }
00043 
00044 extern "C" WrappedCLR *
00045 WrapCLRObject(WrappedCLR::MethodInvoker del, const nsIID &id)
00046 {
00047     char *idstr = id.ToString();
00048     fprintf(stderr, "Wrapping %p as %s\n", del, idstr);
00049     nsCRT::free(idstr);
00050     return new WrappedCLR(del, id);
00051 }