windowptr.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/windowptr.h
  3. // Purpose: smart pointer for holding wxWindow instances
  4. // Author: Vaclav Slavik
  5. // Created: 2013-09-01
  6. // Copyright: (c) 2013 Vaclav Slavik
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_WINDOWPTR_H_
  10. #define _WX_WINDOWPTR_H_
  11. #include "wx/sharedptr.h"
  12. // ----------------------------------------------------------------------------
  13. // wxWindowPtr: A smart pointer with correct wxWindow destruction.
  14. // ----------------------------------------------------------------------------
  15. namespace wxPrivate
  16. {
  17. struct wxWindowDeleter
  18. {
  19. void operator()(wxWindow *win)
  20. {
  21. win->Destroy();
  22. }
  23. };
  24. } // namespace wxPrivate
  25. template<typename T>
  26. class wxWindowPtr : public wxSharedPtr<T>
  27. {
  28. public:
  29. typedef T element_type;
  30. wxEXPLICIT wxWindowPtr(element_type* win)
  31. : wxSharedPtr<T>(win, wxPrivate::wxWindowDeleter())
  32. {
  33. }
  34. wxWindowPtr() {}
  35. wxWindowPtr(const wxWindowPtr& tocopy) : wxSharedPtr<T>(tocopy) {}
  36. wxWindowPtr& operator=(const wxWindowPtr& tocopy)
  37. {
  38. wxSharedPtr<T>::operator=(tocopy);
  39. return *this;
  40. }
  41. wxWindowPtr& operator=(element_type* win)
  42. {
  43. return operator=(wxWindowPtr(win));
  44. }
  45. void reset(T* ptr = NULL)
  46. {
  47. wxSharedPtr<T>::reset(ptr, wxPrivate::wxWindowDeleter());
  48. }
  49. };
  50. #endif // _WX_WINDOWPTR_H_