Any comments on this?
Adi Shavit wrote:
Hi,
I wrote a little proof-of-concept shim for doing this.
It seems to work as expected, while preventing naive misuse.
template <class T>
class raw_assignment_shim
{
public:
operator T::resource_type*() // conversion op
{ return &res_; }
~raw_assignment_shim() // dtor
{
// do some checks here...
ref_.set(res_, false);
}
private:
friend raw_assignment_shim<T> make_raw_assignment_shim(T&
ref);
raw_assignment_shim(T& ref): // private
ctor
res_(ref.get()),
ref_(ref)
{}
T::resource_type operator*(); // not
implemented. Prevents de-ref.
private:
T::resource_type res_;
T& ref_;
};
template <class T>
raw_assignment_shim<T> make_raw_assignment_shim(T& ref)
{ return raw_assignment_shim<T>(ref); }
///////////////////////////////////////////////////////////////////////////////////
bool SequenceProcessor::createFilterGraph(string filename)
{
using winstl::a2w;
// Create a source filter specified by filename
ref_ptr<IBaseFilter> pSource;
if(FAILED(pGraph->AddSourceFilter(a2w(filename),0, make_raw_assignment_shim(pSource))))
{
::MessageBox( NULL, "Unable to create source filter",
"Error", MB_OK | MB_ICONINFORMATION );
return 0;
}
/* // original code...
// Create a source filter specified by filename
ref_ptr<IBaseFilter> pSource;
{
IBaseFilter* ppSource= NULL;
if(FAILED(pGraph->AddSourceFilter(a2w(filename),0,&ppSource)))
{
::MessageBox( NULL, "Unable to create source filter",
"Error", MB_OK | MB_ICONINFORMATION );
return 0;
}
pSource.set(ppSource, false);
}
*/
...
Of course, the names can be changed, and some
refinements should probably be added.
Such as limit it only for initializing an empty (NULL) ref_ptr<>,
exceptions etc.
What do you think?
Adi