| 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 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2006 REA Elektronik GmbH |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef _WX_DFB_DFBPTR_H_ |
| 12 | #define _WX_DFB_DFBPTR_H_ |
| 13 | |
| 14 | //----------------------------------------------------------------------------- |
| 15 | // wxDFB_DECLARE_INTERFACE |
| 16 | //----------------------------------------------------------------------------- |
| 17 | |
| 18 | /** |
| 19 | Forward declares wx wrapper around DirectFB interface @a name. |
| 20 | |
| 21 | Also declares wx##name##Ptr typedef for wxDfbPtr<wx##name> pointer. |
| 22 | |
| 23 | @param name name of the DirectFB interface |
| 24 | */ |
| 25 | #define wxDFB_DECLARE_INTERFACE(name) \ |
| 26 | class wx##name; \ |
| 27 | typedef wxDfbPtr<wx##name> wx##name##Ptr; |
| 28 | |
| 29 | |
| 30 | //----------------------------------------------------------------------------- |
| 31 | // wxDfbPtr<T> |
| 32 | //----------------------------------------------------------------------------- |
| 33 | |
| 34 | class wxDfbWrapperBase; |
| 35 | |
| 36 | class WXDLLIMPEXP_CORE wxDfbPtrBase |
| 37 | { |
| 38 | protected: |
| 39 | static void DoAddRef(wxDfbWrapperBase *ptr); |
| 40 | static void DoRelease(wxDfbWrapperBase *ptr); |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | This template implements smart pointer for keeping pointers to DirectFB |
| 45 | wrappers (i.e. wxIFoo classes derived from wxDfbWrapper<T>). Interface's |
| 46 | reference count is increased on copying and the interface is released when |
| 47 | the pointer is deleted. |
| 48 | */ |
| 49 | template<typename T> |
| 50 | class wxDfbPtr : private wxDfbPtrBase |
| 51 | { |
| 52 | public: |
| 53 | /** |
| 54 | Creates the pointer from raw pointer to the wrapper. |
| 55 | |
| 56 | Takes ownership of @a ptr, i.e. AddRef() is @em not called on it. |
| 57 | */ |
| 58 | wxDfbPtr(T *ptr = NULL) : m_ptr(ptr) {} |
| 59 | |
| 60 | /// Copy ctor |
| 61 | wxDfbPtr(const wxDfbPtr& ptr) { InitFrom(ptr); } |
| 62 | |
| 63 | /// Dtor. Releases the interface |
| 64 | ~wxDfbPtr() { Reset(); } |
| 65 | |
| 66 | /// Resets the pointer to NULL, decreasing reference count of the interface. |
| 67 | void Reset() |
| 68 | { |
| 69 | if ( m_ptr ) |
| 70 | { |
| 71 | this->DoRelease((wxDfbWrapperBase*)m_ptr); |
| 72 | m_ptr = NULL; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Cast to the wrapper pointer |
| 77 | operator T*() const { return m_ptr; } |
| 78 | |
| 79 | // standard operators: |
| 80 | |
| 81 | wxDfbPtr& operator=(T *ptr) |
| 82 | { |
| 83 | Reset(); |
| 84 | m_ptr = ptr; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | wxDfbPtr& operator=(const wxDfbPtr& ptr) |
| 89 | { |
| 90 | Reset(); |
| 91 | InitFrom(ptr); |
| 92 | return *this; |
| 93 | } |
| 94 | |
| 95 | T& operator*() const { return *m_ptr; } |
| 96 | T* operator->() const { return m_ptr; } |
| 97 | |
| 98 | private: |
| 99 | void InitFrom(const wxDfbPtr& ptr) |
| 100 | { |
| 101 | m_ptr = ptr.m_ptr; |
| 102 | if ( m_ptr ) |
| 103 | this->DoAddRef((wxDfbWrapperBase*)m_ptr); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | T *m_ptr; |
| 108 | }; |
| 109 | |
| 110 | #endif // _WX_DFB_DFBPTR_H_ |