Page 1 of 1

wx auto_ptr

Posted: Tue Nov 08, 2005 12:00 pm
by prophet
Consider using the following template:

Code: Select all

template<class wxtype>
class wx_ptr
{
    wxtype *object;
    
public:    
    wx_ptr(wxtype *my_object = NULL)
        : object(my_object)
    { }
    
    ~wx_ptr() 
    {	
    	if(object != NULL)
            object->Destroy(); 
    }
    
    void operator=(wx_ptr &other_ptr)
    {
    	if(this == &other_ptr)
            return;
        
        if(object != NULL)
            object->Destroy();
                
        object = other_ptr.object;
        other_ptr.object = NULL;
    }
    
    void reset(wxtype *my_object = NULL)
    {
    	if(object != NULL)
            object->Destroy();
                
        object = my_object;
    }
    
    wxtype *operator->() { return object; }
    wxtype const *operator->() const { return object; }
    
    wxtype &operator*() { return *object; }
    wxtype const &operator*() const { return *object; }

    wxtype *get() { return object; }
    wxtype const *get() const { return object; }
};

It is the equivalant of the std::auto_ptr, but it calls Destroy when the class goes out-of-scope. Use like:

Code: Select all

{

    wx_ptr<wxMessageDialog> ptr(new wxMessageDialog(..))
    ptr->ShowModal(...)
} // Ptr goes out of scope, auto delete

Posted: Tue Nov 08, 2005 3:10 pm
by leio
consider using boost::shared_ptr :lol:

Posted: Wed Nov 09, 2005 9:19 am
by prophet
leio wrote:consider using boost::shared_ptr :lol:
I wouldn't recommend that, cos the shared_ptr calls delete in it's destructor ;-)

Posted: Wed Nov 09, 2005 4:30 pm
by leio
word.
Some template in there should support specifying such stuff, but I'm not too familiar with boost.

Posted: Fri Nov 11, 2005 12:40 pm
by prophet
leio wrote:word.
Some template in there should support specifying such stuff, but I'm not too familiar with boost.
You're probably right, one of the parameters for the template class is possibly a destruction functor. I should look in too that.

I tried using boost once with mingw and wx, but it didn't turn out to be such a huge success. I think it had some problems with headers files and my applications not compiling any more.

Posted: Fri Nov 11, 2005 1:49 pm
by Jamie

Code: Select all

#include <boost/shared_ptr.hpp>
boost::shared_ptr<wxMessageDialog> ptr(new wxMessageDialog(..), mem_fun(&wxMessageDialog::Destroy));
It's a shame wxWidgets doesn't use boost or even real STL......

Posted: Mon Nov 14, 2005 8:22 am
by prophet
Jamie wrote:

Code: Select all

#include <boost/shared_ptr.hpp>
boost::shared_ptr<wxMessageDialog> ptr(new wxMessageDialog(..), mem_fun(&wxMessageDialog::Destroy));
It's a shame wxWidgets doesn't use boost or even real STL......
I agree, thx for the code sample btw.

Posted: Mon Dec 26, 2005 10:39 am
by priyank_bolia
I modified the boost files, so that it will automatically call Destroy instead of delete for objects derived with wxWindow. So now, when you write code, it will automatically call Destroy:

Code: Select all

#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr wx_ptr;
wx_ptr<wxMessageDialog> ptr(new wxMessageDialog(...));
http://www.priyank.in/downloads/boostpointer.zip