File: dfbptr.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (109 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/////////////////////////////////////////////////////////////////////////////
// Name:        wx/dfb/dfbptr.h
// Purpose:     wxDfbPtr<T> for holding objects declared in wrapdfb.h
// Author:      Vaclav Slavik
// Created:     2006-08-09
// Copyright:   (c) 2006 REA Elektronik GmbH
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_DFB_DFBPTR_H_
#define _WX_DFB_DFBPTR_H_

//-----------------------------------------------------------------------------
// wxDFB_DECLARE_INTERFACE
//-----------------------------------------------------------------------------

/**
    Forward declares wx wrapper around DirectFB interface @a name.

    Also declares wx##name##Ptr typedef for wxDfbPtr<wx##name> pointer.

    @param name  name of the DirectFB interface
 */
#define wxDFB_DECLARE_INTERFACE(name)            \
    class wx##name;                              \
    typedef wxDfbPtr<wx##name> wx##name##Ptr;


//-----------------------------------------------------------------------------
// wxDfbPtr<T>
//-----------------------------------------------------------------------------

class wxDfbWrapperBase;

class WXDLLIMPEXP_CORE wxDfbPtrBase
{
protected:
    static void DoAddRef(wxDfbWrapperBase *ptr);
    static void DoRelease(wxDfbWrapperBase *ptr);
};

/**
    This template implements smart pointer for keeping pointers to DirectFB
    wrappers (i.e. wxIFoo classes derived from wxDfbWrapper<T>). Interface's
    reference count is increased on copying and the interface is released when
    the pointer is deleted.
 */
template<typename T>
class wxDfbPtr : private wxDfbPtrBase
{
public:
    /**
        Creates the pointer from raw pointer to the wrapper.

        Takes ownership of @a ptr, i.e. AddRef() is @em not called on it.
     */
    wxDfbPtr(T *ptr = NULL) : m_ptr(ptr) {}

    /// Copy ctor
    wxDfbPtr(const wxDfbPtr& ptr) { InitFrom(ptr); }

    /// Dtor. Releases the interface
    ~wxDfbPtr() { Reset(); }

    /// Resets the pointer to NULL, decreasing reference count of the interface.
    void Reset()
    {
        if ( m_ptr )
        {
            this->DoRelease((wxDfbWrapperBase*)m_ptr);
            m_ptr = NULL;
        }
    }

    /// Cast to the wrapper pointer
    operator T*() const { return m_ptr; }

    // standard operators:

    wxDfbPtr& operator=(T *ptr)
    {
        Reset();
        m_ptr = ptr;
        return *this;
    }

    wxDfbPtr& operator=(const wxDfbPtr& ptr)
    {
        Reset();
        InitFrom(ptr);
        return *this;
    }

    T& operator*() const { return *m_ptr; }
    T* operator->() const { return m_ptr; }

private:
    void InitFrom(const wxDfbPtr& ptr)
    {
        m_ptr = ptr.m_ptr;
        if ( m_ptr )
            this->DoAddRef((wxDfbWrapperBase*)m_ptr);
    }

private:
    T *m_ptr;
};

#endif // _WX_DFB_DFBPTR_H_