Welcome to mirror list, hosted at ThFree Co, Russian Federation.

holder.h « Runtime « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a72cf44d95b3d76ca137ecec44df0f898d94452 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// -----------------------------------------------------------------------------------------------------------
// Cut down versions of the Holder and Wrapper template classes used in the CLR. If this coding pattern is
// also common in the Redhawk code then it might be worth investigating pulling the whole holder.h header file
// over (a quick look indicates it might not drag in too many extra dependencies).
//

// -----------------------------------------------------------------------------------------------------------
// This version of holder does not have a default constructor.

#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define EQUALS_DEFAULT 
#else
#define EQUALS_DEFAULT = default
#endif

template <typename TYPE, void (*ACQUIRE_FUNC)(TYPE), void (*RELEASE_FUNC)(TYPE)>
class HolderNoDefaultValue
{
public:
    HolderNoDefaultValue(TYPE value, bool fTake = true) : m_value(value), m_held(false) 
        { if (fTake) { ACQUIRE_FUNC(value); m_held = true; } }

    ~HolderNoDefaultValue() { if (m_held) RELEASE_FUNC(m_value); }

    TYPE GetValue() { return m_value; }

    void Acquire() { ACQUIRE_FUNC(m_value); m_held = true; }
    void Release() { if (m_held) { RELEASE_FUNC(m_value); m_held = false; } }
    void SuppressRelease() { m_held = false; }
    TYPE Extract() { m_held = false; return GetValue(); }

    HolderNoDefaultValue(HolderNoDefaultValue && other) EQUALS_DEFAULT;

protected:
    TYPE    m_value;
    bool    m_held;

private:
    // No one should be copying around holder types.
    HolderNoDefaultValue & operator=(const HolderNoDefaultValue & other);
    HolderNoDefaultValue(const HolderNoDefaultValue & other);
};

// -----------------------------------------------------------------------------------------------------------
template <typename TYPE, void (*ACQUIRE_FUNC)(TYPE), void (*RELEASE_FUNC)(TYPE), TYPE DEFAULTVALUE = nullptr>
class Holder : public HolderNoDefaultValue<TYPE, ACQUIRE_FUNC, RELEASE_FUNC>
{
    typedef HolderNoDefaultValue<TYPE, ACQUIRE_FUNC, RELEASE_FUNC> MY_PARENT;
public:
    Holder() : MY_PARENT(DEFAULTVALUE, false) {}
    Holder(TYPE value, bool fTake = true) : MY_PARENT(value, fTake) {}

    Holder(Holder && other) EQUALS_DEFAULT;

private:
    // No one should be copying around holder types.
    Holder & operator=(const Holder & other);
    Holder(const Holder & other);
};

// -----------------------------------------------------------------------------------------------------------
template <typename TYPE, void (*ACQUIRE_FUNC)(TYPE), void (*RELEASE_FUNC)(TYPE), TYPE DEFAULTVALUE = nullptr>
class Wrapper : public Holder<TYPE, ACQUIRE_FUNC, RELEASE_FUNC, DEFAULTVALUE>
{
    typedef Holder<TYPE, ACQUIRE_FUNC, RELEASE_FUNC, DEFAULTVALUE> MY_PARENT;

public:
    Wrapper() : MY_PARENT() {}
    Wrapper(TYPE value, bool fTake = true) : MY_PARENT(value, fTake) {}
    Wrapper(Wrapper && other) EQUALS_DEFAULT;

    FORCEINLINE TYPE& operator=(TYPE const & value)
    {
        MY_PARENT::Release();
        MY_PARENT::m_value = value;
        MY_PARENT::Acquire();
        return MY_PARENT::m_value;
    }

    FORCEINLINE const TYPE &operator->() { return MY_PARENT::m_value; }
    FORCEINLINE const TYPE &operator*() { return MY_PARENT::m_value; }
    FORCEINLINE operator TYPE() { return MY_PARENT::m_value; }

private:
    // No one should be copying around wrapper types.
    Wrapper & operator=(const Wrapper & other);
    Wrapper(const Wrapper & other);
};

// -----------------------------------------------------------------------------------------------------------
template <typename TYPE>
FORCEINLINE void DoNothing(TYPE /*value*/)
{
}

// -----------------------------------------------------------------------------------------------------------
template <typename TYPE> 
FORCEINLINE void Delete(TYPE *value)
{
    delete value;
}

// -----------------------------------------------------------------------------------------------------------
template <typename TYPE,
          typename PTR_TYPE = TYPE *,
          void (*ACQUIRE_FUNC)(PTR_TYPE) = DoNothing<PTR_TYPE>,
          void (*RELEASE_FUNC)(PTR_TYPE) = Delete<TYPE>,
          PTR_TYPE NULL_VAL = nullptr,
          typename BASE = Wrapper<PTR_TYPE, ACQUIRE_FUNC, RELEASE_FUNC, NULL_VAL> >
class NewHolder : public BASE
{
public:
    NewHolder(PTR_TYPE p = NULL_VAL) : BASE(p)
        { }

    PTR_TYPE& operator=(PTR_TYPE p)
        { return BASE::operator=(p); }

    bool IsNull()
        { return BASE::GetValue() == NULL_VAL; }
};

//-----------------------------------------------------------------------------
// NewArrayHolder : New []'ed pointer holder
//  {
//      NewArrayHolder<Foo> foo = new (nothrow) Foo [30];
//  } // delete [] foo on out of scope
//-----------------------------------------------------------------------------

template <typename TYPE> 
FORCEINLINE void DeleteArray(TYPE *value)
{
    delete [] value;
    value = NULL;
}

template <typename TYPE,
          typename PTR_TYPE = TYPE *,
          void (*ACQUIRE_FUNC)(PTR_TYPE) = DoNothing<PTR_TYPE>,
          void (*RELEASE_FUNC)(PTR_TYPE) = DeleteArray<TYPE>,
          PTR_TYPE NULL_VAL = nullptr,
          typename BASE = Wrapper<PTR_TYPE, ACQUIRE_FUNC, RELEASE_FUNC, NULL_VAL> >
class NewArrayHolder : public BASE
{
public:
    NewArrayHolder(PTR_TYPE p = NULL_VAL) : BASE(p)
        { }

    PTR_TYPE& operator=(PTR_TYPE p)
        { return BASE::operator=(p); }

    bool IsNull()
        { return BASE::GetValue() == NULL_VAL; }
};

// -----------------------------------------------------------------------------------------------------------
template<typename TYPE>
FORCEINLINE void Destroy(TYPE * value)
{
    value->Destroy();
}

// -----------------------------------------------------------------------------------------------------------
template <typename TYPE,
          typename PTR_TYPE = TYPE *,
          void (*ACQUIRE_FUNC)(PTR_TYPE) = DoNothing<PTR_TYPE>,
          void (*RELEASE_FUNC)(PTR_TYPE) = Destroy<TYPE>,
          PTR_TYPE NULL_VAL = nullptr,
          typename BASE = Wrapper<PTR_TYPE, ACQUIRE_FUNC, RELEASE_FUNC, NULL_VAL> >
class CreateHolder : public BASE
{
public:
    CreateHolder(PTR_TYPE p = NULL_VAL) : BASE(p)
        { }

    PTR_TYPE& operator=(PTR_TYPE p)
        { return BASE::operator=(p); }
};