The following smart pointer is designed to wrap any IReferenceCounted instance, so it can be used with Irrlicht as is. It's not as fancy as boost::shared_ptr, but it's good enough for the job.
Code: Select all
#ifndef IRR_PTR_H
#define IRR_PTR_H
namespace irr
{
template <class T>
class irr_ptr
{
private:
T* ptr;
public:
irr_ptr() : ptr(0) {}
irr_ptr(T* ptr) : ptr(ptr) { }
irr_ptr(const irr_ptr& r) : ptr(r.ptr) { if (ptr) ptr->grab(); }
template <class Y> irr_ptr(const irr_ptr<Y>& r) : ptr(r.ptr) { if (ptr) ptr->grab(); }
~irr_ptr() { if (ptr) ptr->drop(); }
T* get() { return ptr; }
T* operator->() { return ptr; }
T& operator*() { return *ptr; }
const T* get() const { return ptr; }
const T* operator->() const { return ptr; }
const T& operator*() const { return *ptr; }
bool operator==(const irr_ptr<T>& r) const { return ptr == r.ptr; }
bool operator!=(const irr_ptr<T>& r) const { return ptr != r.ptr; }
bool operator<(const irr_ptr<T>& r) const { return ptr < r.ptr; }
irr_ptr& operator=(const irr_ptr& r)
{
ptr = r.ptr;
ptr->grab();
return *this;
}
};
}
#endif
Code: Select all
irr_ptr<IrrlichtDevice> device = createDevice(video::EDT_DIRECT3D9);