Coverage Report

Created: 2023-02-22 06:06

/src/wxwidgets/include/wx/scopedarray.h
Line
Count
Source (jump to first uncovered line)
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/scopedarray.h
3
// Purpose:     scoped smart pointer class
4
// Author:      Vadim Zeitlin
5
// Created:     2009-02-03
6
// Copyright:   (c) Jesse Lovelace and original Boost authors (see below)
7
//              (c) 2009 Vadim Zeitlin
8
// Licence:     wxWindows licence
9
/////////////////////////////////////////////////////////////////////////////
10
11
#ifndef _WX_SCOPED_ARRAY_H_
12
#define _WX_SCOPED_ARRAY_H_
13
14
#include "wx/defs.h"
15
#include "wx/checkeddelete.h"
16
17
// ----------------------------------------------------------------------------
18
// wxScopedArray: A scoped array
19
// ----------------------------------------------------------------------------
20
21
template <class T>
22
class wxScopedArray
23
{
24
public:
25
    typedef T element_type;
26
27
0
    explicit wxScopedArray(T * array = nullptr) : m_array(array) { }
28
    explicit wxScopedArray(size_t count) : m_array(new T[count]) { }
29
30
0
    ~wxScopedArray() { delete [] m_array; }
31
32
    // test for pointer validity: defining conversion to unspecified_bool_type
33
    // and not more obvious bool to avoid implicit conversions to integer types
34
    typedef T *(wxScopedArray<T>::*unspecified_bool_type)() const;
35
    operator unspecified_bool_type() const
36
    {
37
        return m_array ? &wxScopedArray<T>::get : nullptr;
38
    }
39
40
    void reset(T *array = nullptr)
41
    {
42
        if ( array != m_array )
43
        {
44
            delete [] m_array;
45
            m_array = array;
46
        }
47
    }
48
49
    T& operator[](size_t n) const { return m_array[n]; }
50
51
    T *get() const { return m_array; }
52
53
    void swap(wxScopedArray &other)
54
    {
55
        T * const tmp = other.m_array;
56
        other.m_array = m_array;
57
        m_array = tmp;
58
    }
59
60
private:
61
    T *m_array;
62
63
    wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray, T);
64
};
65
66
// ----------------------------------------------------------------------------
67
// old macro based implementation
68
// ----------------------------------------------------------------------------
69
70
// the same but for arrays instead of simple pointers
71
#define wxDECLARE_SCOPED_ARRAY(T, name)\
72
class name                          \
73
{                                   \
74
private:                            \
75
    T * m_ptr;                      \
76
    name(name const &);             \
77
    name & operator=(name const &); \
78
                                    \
79
public:                             \
80
    explicit name(T * p = nullptr) : m_ptr(p) \
81
    {}                              \
82
                                    \
83
    ~name();                        \
84
    void reset(T * p = nullptr);       \
85
                                    \
86
    T & operator[](long int i) const\
87
    {                               \
88
        wxASSERT(m_ptr != nullptr);    \
89
        wxASSERT(i >= 0);           \
90
        return m_ptr[i];            \
91
    }                               \
92
                                    \
93
    T * get() const                 \
94
    {                               \
95
        return m_ptr;               \
96
    }                               \
97
                                    \
98
    void swap(name & ot)            \
99
    {                               \
100
        T * tmp = ot.m_ptr;         \
101
        ot.m_ptr = m_ptr;           \
102
        m_ptr = tmp;                \
103
    }                               \
104
};
105
106
#define wxDEFINE_SCOPED_ARRAY(T, name)  \
107
name::~name()                           \
108
{                                       \
109
    wxCHECKED_DELETE_ARRAY(m_ptr);      \
110
}                                       \
111
void name::reset(T * p){                \
112
    if (m_ptr != p)                     \
113
    {                                   \
114
       wxCHECKED_DELETE_ARRAY(m_ptr);   \
115
       m_ptr = p;                       \
116
    }                                   \
117
}
118
119
#endif // _WX_SCOPED_ARRAY_H_
120