Posted: Fri Mar 20, 2009 3:07 am
@vitek, wow thanks, that's cool. I haven't seen that before. Do you maybe also know why boost uses that instead of overloading operator bool()?
Making stuff easy in c++ is hard.vitek wrote:See Safe Bool Idiom.
Code: Select all
IrrPtr<IImage> p = IrrPtr<IImage>(driver->createImage(...), false);
Code: Select all
template <class T>
IrrPtr<T> make_created (T* ptr)
{
return IrrPtr<T>(ptr, false);
}
template <class T>
IrrPtr<T> make_added (T* ptr)
{
return IrrPtr<T>(ptr, true);
}
// use it like this
IrrPtr<ISceneNode> p = make_added(smgr->addCubeSceneNode(...));
IrrPtr<IImage> q = make_created(driver->createImage...(...));
Code: Select all
IrrPtr<IMeshSceneNode> msn(smgr->addMeshSceneNode(), true);
// these won't compile
IrrPtr<ISceneNode> sn(msn);
sn = msn;
// of course this will, but it exposes the fact that the pointer is different from a regular raw pointer
IrrPtr<ISceneNode> sn(msn.get(), true);
sn = IrrPtr<ISceneNode>(msn.get(), true);
The original intent was to handle pointers that would do the whole grab()/drop() lifecycle for the refcounted Irrlicht pointers. That's why I didn't give created (already grab()'ed) pointers equal rights, but did see them as exception. I already got a bad feeling about the operator=(T*), so I will probably drop that. I'll have to think some more about giving grab()'ed and non-grab()'ed pointers equal rights.vitek wrote:If your design intent is to handle pointers that may or may not be need to be dropped, I think that the set_created() and operator=(T*) functions should be removed, and the default grab value should be removed from the constructor.
Yeah, I know. I just didn't find time yet to look up which traps c++ will put in my way when trying that. I think there will be some, because boost has those strange casts like:vitek wrote: Another improvement would be to handle implicit conversions...
Code: Select all
class IBase{};
class Derived : public IBase {}
typedef boost::shared_ptr<IBase> base_ptr;
typedef boost::shared_ptr<Derived> derived_ptr;
base_ptr b(new Derived());
derived_ptr foo(boost::shared_static_cast<Derived>(b)); // using static_cast
// derived_ptr foo(boost::shared_dynamic_cast<Derived>(b)); // using dynamic_cast
// derived_ptr foo(boost::shared_polymorphic_downcast<Derived>(b)); // using dynamic_cast unless NDEBUG is set - then static_cast
I'm appreciating it :-)vitek wrote: I'm not saying you have to do anything, I'm just bringing up the issues as I see them and offering workarounds. :)
Travis