]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dfb/ifacehelpers.h
56a8808540a48e0c7e76854fc44ed696a940de76
[wxWidgets.git] / include / wx / dfb / ifacehelpers.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dfb/ifacehelpers.h
3 // Purpose: helpers for dealing with DFB interfaces
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_IFACEHELPERS_H_
12 #define _WX_DFB_IFACEHELPERS_H_
13
14 //-----------------------------------------------------------------------------
15 // wxDFB_DECLARE_INTERFACE
16 //-----------------------------------------------------------------------------
17
18 /**
19 Forward declares DirectFB interface @a name.
20
21 Also declares name##Ptr typedef for wxDfbPtr<name> pointer.
22
23 @param name name of the DirectFB interface
24 */
25 #define wxDFB_DECLARE_INTERFACE(name) \
26 struct _##name; \
27 typedef _##name name; \
28 typedef wxDfbPtr<name> name##Ptr;
29
30
31 //-----------------------------------------------------------------------------
32 // wxDfbPtr
33 //-----------------------------------------------------------------------------
34
35 // base class for wxDfbPtr
36 class wxDfbPtrBase
37 {
38 protected:
39 // increment/decrement refcount; see ifacehelpers.cpp for why using
40 // void* is safe
41 static void DoAddRef(void *ptr);
42 static void DoRelease(void *ptr);
43 };
44
45 /**
46 This template implements smart pointer for keeping pointers to DirectFB
47 interfaces. Interface's reference count is increased on copying and the
48 interface is released when the pointer is deleted.
49 */
50 template<typename T>
51 class wxDfbPtr : private wxDfbPtrBase
52 {
53 public:
54 /**
55 Creates the pointer from raw interface pointer.
56
57 Takes ownership of @a ptr, i.e. AddRef() is @em not called on it.
58 */
59 wxDfbPtr(T *ptr = NULL) : m_ptr(ptr) {}
60
61 /// Copy ctor
62 wxDfbPtr(const wxDfbPtr& ptr) { InitFrom(ptr); }
63
64 /// Dtor. Releases the interface
65 ~wxDfbPtr() { Reset(); }
66
67 /// Resets the pointer to NULL, decreasing reference count of the interface.
68 void Reset()
69 {
70 if ( m_ptr )
71 {
72 DoRelease(m_ptr);
73 m_ptr = NULL;
74 }
75 }
76
77 /// Cast to raw pointer
78 operator T*() const { return m_ptr; }
79
80 /**
81 Cast to @em writeable raw pointer so that code like
82 "dfb->CreateFont(dfb, NULL, &desc, &fontPtr)" works.
83
84 Note that this operator calls Reset(), so using it looses the value.
85 */
86 T** operator&()
87 {
88 Reset();
89 return &m_ptr;
90 }
91
92 // standard operators:
93
94 wxDfbPtr& operator=(const wxDfbPtr& ptr)
95 {
96 Reset();
97 InitFrom(ptr);
98 return *this;
99 }
100
101 T& operator*() const { return *m_ptr; }
102 T* operator->() const { return m_ptr; }
103
104 private:
105 void InitFrom(const wxDfbPtr& ptr)
106 {
107 m_ptr = ptr.m_ptr;
108 if ( m_ptr )
109 DoAddRef(m_ptr);
110 }
111
112 private:
113 T *m_ptr;
114 };
115
116 #endif // _WX_DFB_IFACEHELPERS_H_