File: sharedptr.h

package info (click to toggle)
fife 0.4.2-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,204 kB
  • sloc: cpp: 42,642; xml: 18,881; python: 13,521; makefile: 23
file content (280 lines) | stat: -rw-r--r-- 8,874 bytes parent folder | download | duplicates (5)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/***************************************************************************
 *   Copyright (C) 2005-2019 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/
#ifndef FIFE_SHARED_PTR_H_
#define FIFE_SHARED_PTR_H_

// Standard C++ library includes
#include <cassert>
#include <functional>

// 3rd party library includes

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "fife_stdint.h"

namespace FIFE {
    /** shared pointer implementation to provide automatic
    * reference counting and deletion when last reference
    * falls out of scope.
    */
    template <typename T>
    class SharedPtr {
    public:

        /** Constructor
        * default constructor
        * creates a null shared pointer
        */
        SharedPtr()
        : m_ptr(0), m_refCount(0) {

		}

        /** Constructor
        * takes over ownership of the provided pointer
        * and will delete it automatically when last
        * reference falls out of scope.
        */
        template <typename U>
        explicit SharedPtr(U *ptr)
        : m_ptr(ptr), m_refCount(ptr ? new uint32_t(1) : 0) {

		}

        /** Copy Constructor
        * provides ability to properly copy a shared resource
        */
        SharedPtr(const SharedPtr& rhs)
        : m_ptr(rhs.m_ptr), m_refCount(rhs.m_refCount) {
            // increase reference count
            incRefCount();
        }

        /** Constructor
        * shares ownership with the value passed into rhs
        * the pointer type passed in must be convertible
        * to this shared pointer type
        */
        template <typename U>
        SharedPtr(const SharedPtr<U>& rhs) {
            m_ptr = rhs.get();
            m_refCount = rhs.useCountPtr();
            incRefCount();
        }

        /** Destructor
        * handles deletion of underlying pointer
        * if the reference count reaches 0
        */
        ~SharedPtr() {
            // decrement reference count
            decRefCount();

            // check to see if we need to delete
            if (m_refCount && *m_refCount == 0) {
                // delete and set pointers to null
                delete m_ptr;
                delete m_refCount;
                m_ptr = 0;
                m_refCount = 0;
            }
        }

        /** provides functionality for the equality operator
        */
        SharedPtr& operator=(const SharedPtr& rhs) {
            // handle self assignment
            if (rhs.get() == m_ptr) {
                return *this;
            }

            // store in temporary (which causes a ref count increase)
            // and swap with this object
            SharedPtr<T> temp(rhs);
            swap(temp);
            return *this;
        }

        /** provides functionality for the equality operator
        * the passed in pointer type must be convertible to
        * this pointer type
        */
        template <typename U>
        SharedPtr& operator=(const SharedPtr<U>& rhs) {
            // handle self assignment
            if (rhs.get() == m_ptr) {
                return *this;
            }

            // store in temporary (which causes a ref count increase)
            // and swap with this object
            SharedPtr<T> temp(rhs);
            swap(temp);
            return *this;
        }

        /** allows dereferencing of shared pointer to act
        * identical to dereferencing the underlying pointer
        */
        inline T& operator*() const {
            assert(m_ptr);
            return *m_ptr;
        }

        /** allows dereferencing of shared pointer to act
        * identical to dereferencing the underlying pointer
        */
        inline T* operator->() const {
            assert(m_ptr);
            return m_ptr;
        }

        /** allows direct access to underlying pointer
        */
        inline T* get() const {
            return m_ptr;
        }

        /** reset this pointer to a null shared pointer
        * this can be used to lower the reference count
        * of the shared resource or set the underlying pointer
        * to different pointer.
        */
        inline void reset(T* ptr = 0) {
			assert(ptr == 0 || ptr != m_ptr);
            SharedPtr<T>(ptr).swap(*this);
        }

        /** returns the current reference count
        * this should only be called on a non-null
        * shared pointer
        */
        inline uint32_t useCount() const {
            assert(m_refCount);

            if (!m_refCount) {
                return 0;
            }

            return *m_refCount;
        }

        /** returns the current reference count
        * provides direct access to the user count pointer
        * this should really only be used internally
        */
        inline uint32_t* useCountPtr() const {
            return m_refCount;
        }

        /** provides the ability to see if
        * a shared resource is currently only
        * held by a single shared pointer
        * this should only be called on a non-null
        * shared pointer
        */
        inline bool unique() const {
            assert(m_refCount);
            return (*m_refCount == 1);
        }

        /** provides the ability to convert a
        * shared pointer to a bool, this is
        * a convenience for checking validity
        * of a shared pointer in a conditional
        */
        operator bool() const {
            return (m_ptr != 0);
        }

        /** negation operator overload
        */
        bool operator!() const {
            return (m_ptr == 0);
        }

    private:

        /** provides swapping function between
        * two shared pointers, used internally
        */
        inline void swap(SharedPtr<T>& rhs) {
            std::swap(m_ptr, rhs.m_ptr);
            std::swap(m_refCount, rhs.m_refCount);
        }

        /** increases the reference count for
        * this shared resource, used internally
        */
        inline void incRefCount() {
            if (m_refCount) {
                ++(*m_refCount);
            }
        }

        /** decreases the reference count for
        * this shared resource, used internally
        */
        inline void decRefCount() {
            if (m_refCount) {
                --(*m_refCount);
            }
        }

    private:
        T* m_ptr;
        uint32_t* m_refCount;
    };

    /** provides equality operator for shared pointers
    */
    template <typename T, typename U>
    inline bool operator==(const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
        return (lhs.get() == rhs.get());
    }

    /** provides inequality operator for shared pointers
    */
    template <typename T, typename U>
    inline bool operator!=(const SharedPtr<T>& lhs, const SharedPtr<U>& rhs) {
        return (lhs.get() != rhs.get());
    }

    /** provides less than operator for shared pointers
    */
    template<class T, class U>
    inline bool operator<(SharedPtr<T> const& lhs, SharedPtr<U> const& rhs) {
        return std::less<const void*>()(lhs.get(), rhs.get());
    }

    /** convenience function for making a shared pointer
    * can be used anytime a shared pointer should be created
    */
    template <typename T>
    SharedPtr<T> make_shared(T* ptr) {
        return SharedPtr<T>(ptr);
    }
} //FIFE

#endif //FIFE_SHARED_PTR_H_