Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/util/SharedPtr.h @ 7212

Last change on this file since 7212 was 7212, checked in by landauf, 14 years ago

re-implemented Functor - without macros!

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _SharedPtr_H__
30#define _SharedPtr_H__
31
32#include "UtilPrereqs.h"
33#include <algorithm>
34#include <cassert>
35
36namespace orxonox
37{
38    class SharedPtrDestroyer
39    {
40        public:
41            virtual void destroy() = 0;
42    };
43
44    template <class T>
45    class SharedPtrDestroyerImpl : public SharedPtrDestroyer
46    {
47        public:
48            SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {}
49
50            void destroy()
51            {
52                delete this->pointer_;
53            }
54
55        private:
56            T* pointer_;
57    };
58
59    template <class T>
60    class SharedPtr
61    {
62        template <class O>
63        friend class SharedPtr;
64
65        public:
66            inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0)
67            {
68            }
69
70            inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0)
71            {
72                if (this->pointer_)
73                {
74                    this->counter_ = new int(1);
75                    this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_);
76                }
77            }
78
79            inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
80            {
81                if (this->pointer_)
82                    ++(*this->counter_);
83            }
84
85            template <class O>
86            inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
87            {
88                if (this->pointer_)
89                    ++(*this->counter_);
90            }
91
92            inline ~SharedPtr()
93            {
94                if (this->pointer_)
95                {
96                    --(*this->counter_);
97
98                    if (*this->counter_ == 0)
99                    {
100                        this->destroyer_->destroy();
101                        delete this->destroyer_;
102                        delete this->counter_;
103                    }
104                }
105            }
106
107            inline const SharedPtr& operator=(const SharedPtr& other)
108            {
109                SharedPtr(other).swap(*this);
110                return *this;
111            }
112
113            template <class O>
114            inline const SharedPtr& operator=(const SharedPtr<O>& other)
115            {
116                SharedPtr(other).swap(*this);
117                return *this;
118            }
119
120            template <class O>
121            inline SharedPtr<O> cast() const
122            {
123                O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast
124                return SharedPtr<O>(temp, this->counter_, this->destroyer_);
125            }
126
127            inline T* operator->() const
128            {
129                assert(this->pointer_ != 0);
130                return this->pointer_;
131            }
132
133            inline T& operator*() const
134            {
135                assert(this->pointer_ != 0);
136                return *this->pointer_;
137            }
138
139            inline T* get() const
140            {
141                return this->pointer_;
142            }
143
144            inline operator bool() const
145            {
146                return (this->pointer_ != 0);
147            }
148
149            inline void swap(SharedPtr& other)
150            {
151                std::swap(this->pointer_, other.pointer_);
152                std::swap(this->counter_, other.counter_);
153                std::swap(this->destroyer_, other.destroyer_);
154            }
155
156        private:
157            inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer)
158            {
159                if (this->pointer_)
160                    ++(*this->counter_);
161            }
162
163            T* pointer_;
164            int* counter_;
165            SharedPtrDestroyer* destroyer_;
166    };
167
168    template <class T, class Parent>
169    class SharedChildPtr : public Parent
170    {
171        public:
172            inline SharedChildPtr() : Parent() {}
173            inline SharedChildPtr(T* pointer) : Parent(pointer) {}
174            inline SharedChildPtr(const SharedPtr<T>& other) : Parent(other) {}
175
176            inline T* operator->() const { return static_cast<T*>(Parent::operator->()); }
177            inline T& operator*() const { return *static_cast<T*>(Parent::operator->()); }
178    };
179
180}
181
182#endif /* _SharedPtr_H__ */
Note: See TracBrowser for help on using the repository browser.