集合类2-curve,lines,line

 

集合类的第二个例子,
curve包括一个集合(lines),lines包括一条条line
完全仿照深入解析atl,正在消化中。。。。。
首先定义接口
interface Iline : IDispatch
       {
              [id(1), helpstring("method AddItem")] HRESULT AddItem([in]double date,[in] double rate);
              [propget, id(2), helpstring("property val")] HRESULT val([out, retval] long *pVal);
              [propput, id(2), helpstring("property val")] HRESULT val([in] long newVal);
              [id(3), helpstring("method GetRate")] HRESULT GetRate([in]double date,[out,retval]double *rate);
              [id(4) ,helpstring("method Discount")] HRESULT Discount([in]double date,[out,retval]double *dis);
       };
       interface Ilines : IDispatch
       {
              [propget] HRESULT Count([out, retval] long *pVal);
              [id(DISPID_NEWENUM),propget] HRESULT _NewEnum([out, retval] IUnknown* *pVal);
              [id(DISPID_VALUE),propget] HRESULT Item(long n, [out, retval] Iline* *pVal);
              [id(1), helpstring("method AddLine")] HRESULT AddLine([out,retval]Iline**ppline);
       };
interface Icurve : IDispatch
       {
              [propget, id(1), helpstring("property lines")] HRESULT lines([out, retval] Ilines **pVal);
       };
Coclass {/……………./}
在curve中包含lines CcomPtr<Iline> m_lines;并且在finalconstruct中构造return Clines::CreateInstance(&m_lines);
定义getlines属性。return m_lines.CopyTo(pVal);
主要问题就是集合类的定义等等。
template<typename T>
struct _CopyVariantFromAdapterItf
{//供枚举器next方法使用,自己的copy策略类
       static HRESULT copy(VARIANT *p1,CAdapt<CComPtr<T> > *p2)
       { ///////////把容器中元素copy到我们的缓冲区
              HRESULT hr=p2->m_T->QueryInterface(IID_IDispatch,(void**)&p1->pdispVal);
              if(SUCCEEDED(hr))
              {
                     p1->vt=VT_DISPATCH;
              }
              else
              {
                     hr=p2->m_T->QueryInterface(IID_IUnknown,(void**)&p1->punkVal);
                     if(SUCCEEDED(hr))
                     {
                            p1->vt=VT_UNKNOWN;
                     }
                     return hr;
              }
              return S_OK;
       }
       static void init(VARIANT *p){VariantInit(p)};
       static void destroy(VARIANT *p){VariantClear(p);}
};
typedef CComEnumOnSTL<IEnumVARIANT,&IID_IEnumVARIANT,VARIANT,_CopyVariantFromAdapterItf<Iline>,
                                          std::list<CAdapt<CComPtr<Iline> > > > CComEnumVariantOnListOfLine;
std::list就是collType,我们存储数据的容器,在模参中提供。
///////////枚举器的实现类
template <typename T>
struct _CopyItfFromAdaptItf
{//供collectionImpl使用,实现get_item属性,copy策略类
       static HRESULT copy(T** p1,CAdapt<CComPtr<T> > *p2)
       {
              if(*p1=p2->m_T) return (*p1)->AddRef(),S_OK;
              return E_POINTER;
       }
       static void init(T** p){}
       static void destroy(T** p){if(*p) (*p)->Release();}
};
typedef ICollectionOnSTLImpl<IDispatchImpl<Ilines,&IID_Ilines>,
                                                 std::list<CAdapt<CComPtr<Iline> > >,
                                                 Iline*,
                                                 _CopyItfFromAdaptItf<Iline>,
                                                 CComEnumVariantOnListOfLine> ILinesCollImpl;
容器类的实现类,注意从IdispatchImpl继承了dual接口的实现,所以支持dispatch
typedef ICollectionOnSTLImpl<IDispatchImpl<Ilines,&IID_Ilines>,
                                                 std::list<CAdapt<CComPtr<Iline> > >,
                                                 Iline*,
                                                 _CopyItfFromAdaptItf<Iline>,
                                                 CComEnumVariantOnListOfLine> ILinesCollImpl;
class ATL_NO_VTABLE Clines :
其他基本和我以前的例子不变。新手学atl~~~晕~
 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭