使用c++封装com的IEnum接口
最近在做DirectShow的一些开发,其中总要用到连接filter之类的操作,而filter是通过com中的枚举器得到的。在c++中使用枚举器十分不方便,可以看下一段代码:
HRESULT
hr = pGraph->EnumFilters(&pEnum);
if (FAILED(hr)) return
result;
while(pEnum->Next(1, &pFilter, &cFetched) == S_OK)
{
FILTER_INFO FilterInfo;
hr =
pFilter->QueryFilterInfo(&FilterInfo);
if
(FAILED(hr))
{
MessageBox(NULL, TEXT("Could not
get the filter info"),
TEXT("Error"), MB_OK |
MB_ICONERROR);
continue; // Maybe the next
one will work.
}
}
pEnum->Release();
如果每天都要写这样的代码,真的要晕倒,想想c++中不时有iterator吗,而且我也经常用iterator,我用iterator可比Ienum要熟得多。在c++中,想做什么可都是你自己的事,那就做一个iterator
adapter吧,然后我就不用写循环了,用for_each就可以搞定了。
先分析一下,IEnum中主要方法是Next、Reset,这样看来应该是个forward_iterator,不过大多数stl算法已经足够了。好吧,先写一个,不过还是要看看书,找了TCPL看看,forward
iterator要实现 =*p , -> , *p= , ++ , == != 这些操作符。如下代码:
struct graph_filter_iterator :
public
std::iterator<std::forward_iterator_tag,
IBaseFilter*,
ptrdiff_t,
IBaseFilter*,
IBaseFilter*>
{
public:
explicit
graph_filter_iterator()
{}
explicit
graph_filter_iterator(IEnumFilters* penum)
{
pEnum = penum;
reset();
}
IBaseFilter* operator*() const
{
return
pFilter.p;
}
IBaseFilter* operator->()const
{
return
pFilter.p;
}
graph_filter_iterator& operator++()
{
next();
return
*this;
}
graph_filter_iterator operator++(int)
{
graph_filter_iterator tmp = *this;
next();
return
tmp;
}
bool
operator == (graph_filter_iterator& rhs) const
{
return
pFilter == rhs.pFilter;
}
bool
operator != (graph_filter_iterator& rhs) const
{
return
pFilter != rhs.pFilter;
}
void
reset()
{
if
(pEnum == NULL)
return;
HRESULT hr =
pEnum->Reset();
if
(FAILED(hr))
return
;
//
get first filter:
next();
}
protected:
void
next()
{
if
(pFilter != NULL)
pFilter = NULL;
if
(pEnum == NULL)
return;
ULONG cFetched;
pEnum->Next(1,
&pFilter, &cFetched);
}
protected:
CComPtr<IEnumFilters>
pEnum;
CComPtr<IBaseFilter> pFilter;
};
应该说这段代码并不复杂,就重载了需要的操作符,主要都封装在next函数中。然后就可以使用了:
void getname(IBaseFilter* filter)
{
FILTER_INFO info;
HRESULT hr =
filter->QueryFilterInfo(&info);
ATLTRACE(info.achName);
ATLTRACE(_T("\n"));
}
void test(IGraphBuilder* builder)
{
CComPtr<IEnumFilters>
pEnum;
HRESULT hr =
builder->EnumFilters(&pEnum);
if
(FAILED(hr) )
return
;
graph_filter_iterator beg(pEnum);
graph_filter_iterator end;
std::for_each(beg, end,
getname1);
}
不过还是很不方便,因为现在我用filter,一会我还要用pin啊,而且这应该也可以做成通用的吧。没问题,c++就是c++,继续模板化,实际上从next函数中就可以看到需要模板化的参数了,就这一句:pEnum->Next(1, &pFilter, &cFetched) 。
template<class
EnumT, class valueT>
struct com_enum_iterator :
public
std::iterator<std::forward_iterator_tag,
valueT*,
ptrdiff_t,
valueT*,
valueT*>
{
typedef
EnumT enum_type; // enum of the container
typedef
valueT value_type; // element in container
typedef
valueT* ptr_type; // element in
container
typedef
CComPtr<EnumT> enum_wrapper; // enum of the
container
typedef
CComPtr<valueT> value_wrapper; // element in
container
//
refer to myself
typedef
com_enum_iterator<enum_type, value_type> this_type;
explicit
com_enum_iterator(enum_type* enums=NULL)
: _pEnum(enums)
{
reset();
}
ptr_type operator*()
const
{
return
_pValue.p;
}
ptr_type operator->()const
{
return
_pValue.p;
}
this_type& operator++()
{
next();
return
*this;
}
this_type operator++(int)
{
this_type tmp(_pCont.p,
_pFEnum, _pFNext);
next();
return
tmp;
}
bool
operator == (this_type& rhs) const
{
return
_pValue == rhs._pValue;
}
bool
operator != (this_type& rhs) const
{
return
_pValue != rhs._pValue;
}
void
reset()
{
if
(_pEnum == NULL)
return;
HRESULT hr =
_pEnum->Reset();
if
(FAILED(hr))
return
;
//
get first filter:
next();
}
protected:
void
next()
{
if
(_pValue != NULL)
_pValue = NULL;
if
(_pEnum == NULL)
return;
//
get first filter:
ULONG cFetched;
_pEnum->Next(1,
&_pValue, &cFetched);
}
protected:
enum_wrapper _pEnum;
// enum of the container
value_wrapper _pValue; // value
in the container
};
// 辅助函数
template <class
EnumT, class valueT>
com_enum_iterator<EnumT, valueT>
inline make_com_iterator(EnumT *enums,
valueT *valueFake)
{
typedef
com_enum_iterator<EnumT, valueT> iterator_type;
return
iterator_type(enums);
}
这么使用:
void getname(IBaseFilter* filter)
{
FILTER_INFO info;
HRESULT hr =
filter->QueryFilterInfo(&info);
ATLTRACE(info.achName);
ATLTRACE(_T("\n"));
}
void test(IGraphBuilder* builder)
{
CComPtr<IEnumFilters> pEnum;
HRESULT hr =
builder->EnumFilters(&pEnum);
if
(FAILED(hr))
{
return
;
}
com_enum_iterator<IEnumFilters,
IBaseFilter> beg =
make_com_iterator1(pEnum.p,
((IBaseFilter*)(NULL) ) );
com_enum_iterator<IEnumFilters,
IBaseFilter> end;
std::for_each(beg, end, getname);
}
好了,就这么简单,以后com就变成stl风格的c++了。
实际上,网上还有一个enum_iterator,google一搜就搜到了(http://www.sellsbrothers.com/tools/enum_iterator.h),不过要复杂得多,我看了一下,放弃了,没我的简单,呵呵!