dfbptr.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dfb/dfbptr.h
  3. // Purpose: wxDfbPtr<T> for holding objects declared in wrapdfb.h
  4. // Author: Vaclav Slavik
  5. // Created: 2006-08-09
  6. // Copyright: (c) 2006 REA Elektronik GmbH
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_DFB_DFBPTR_H_
  10. #define _WX_DFB_DFBPTR_H_
  11. //-----------------------------------------------------------------------------
  12. // wxDFB_DECLARE_INTERFACE
  13. //-----------------------------------------------------------------------------
  14. /**
  15. Forward declares wx wrapper around DirectFB interface @a name.
  16. Also declares wx##name##Ptr typedef for wxDfbPtr<wx##name> pointer.
  17. @param name name of the DirectFB interface
  18. */
  19. #define wxDFB_DECLARE_INTERFACE(name) \
  20. class wx##name; \
  21. typedef wxDfbPtr<wx##name> wx##name##Ptr;
  22. //-----------------------------------------------------------------------------
  23. // wxDfbPtr<T>
  24. //-----------------------------------------------------------------------------
  25. class wxDfbWrapperBase;
  26. class WXDLLIMPEXP_CORE wxDfbPtrBase
  27. {
  28. protected:
  29. static void DoAddRef(wxDfbWrapperBase *ptr);
  30. static void DoRelease(wxDfbWrapperBase *ptr);
  31. };
  32. /**
  33. This template implements smart pointer for keeping pointers to DirectFB
  34. wrappers (i.e. wxIFoo classes derived from wxDfbWrapper<T>). Interface's
  35. reference count is increased on copying and the interface is released when
  36. the pointer is deleted.
  37. */
  38. template<typename T>
  39. class wxDfbPtr : private wxDfbPtrBase
  40. {
  41. public:
  42. /**
  43. Creates the pointer from raw pointer to the wrapper.
  44. Takes ownership of @a ptr, i.e. AddRef() is @em not called on it.
  45. */
  46. wxDfbPtr(T *ptr = NULL) : m_ptr(ptr) {}
  47. /// Copy ctor
  48. wxDfbPtr(const wxDfbPtr& ptr) { InitFrom(ptr); }
  49. /// Dtor. Releases the interface
  50. ~wxDfbPtr() { Reset(); }
  51. /// Resets the pointer to NULL, decreasing reference count of the interface.
  52. void Reset()
  53. {
  54. if ( m_ptr )
  55. {
  56. this->DoRelease((wxDfbWrapperBase*)m_ptr);
  57. m_ptr = NULL;
  58. }
  59. }
  60. /// Cast to the wrapper pointer
  61. operator T*() const { return m_ptr; }
  62. // standard operators:
  63. wxDfbPtr& operator=(T *ptr)
  64. {
  65. Reset();
  66. m_ptr = ptr;
  67. return *this;
  68. }
  69. wxDfbPtr& operator=(const wxDfbPtr& ptr)
  70. {
  71. Reset();
  72. InitFrom(ptr);
  73. return *this;
  74. }
  75. T& operator*() const { return *m_ptr; }
  76. T* operator->() const { return m_ptr; }
  77. private:
  78. void InitFrom(const wxDfbPtr& ptr)
  79. {
  80. m_ptr = ptr.m_ptr;
  81. if ( m_ptr )
  82. this->DoAddRef((wxDfbWrapperBase*)m_ptr);
  83. }
  84. private:
  85. T *m_ptr;
  86. };
  87. #endif // _WX_DFB_DFBPTR_H_