wx auto_ptr

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

wx auto_ptr

Post 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
Last edited by prophet on Wed Nov 09, 2005 9:19 am, edited 1 time in total.
leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio »

consider using boost::shared_ptr :lol:
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

Post 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 ;-)
leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio »

word.
Some template in there should support specifying such stuff, but I'm not too familiar with boost.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

Post 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.
Jamie
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Wed Mar 30, 2005 10:56 pm

Post 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......
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

Post 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.
priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post 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
Post Reply