Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/SmartPtr.h @ 7363

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

assigned a group to each header file in the core library

  • Property svn:eol-style set to native
File size: 5.3 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// Inspired by boost::intrusive_ptr by Peter Dimov
30
31/**
32    @defgroup SmartPtr SmartPtr<T> and WeakPtr<T>
33    @ingroup Object
34*/
35
36/**
37    @file
38    @ingroup Object SmartPtr
39*/
40
41#ifndef _SmartPtr_H__
42#define _SmartPtr_H__
43
44#include "CorePrereqs.h"
45
46#include <cassert>
47
48#include "Identifier.h"
49#include "OrxonoxClass.h"
50#include "WeakPtr.h"
51
52namespace orxonox
53{
54    template <class T>
55    class SmartPtr
56    {
57        public:
58            inline SmartPtr() : pointer_(0), base_(0)
59            {
60            }
61
62            inline SmartPtr(int) : pointer_(0), base_(0)
63            {
64            }
65
66            inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)
67            {
68                if (this->base_ && bAddRef)
69                    this->base_->incrementReferenceCount();
70            }
71
72            inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_)
73            {
74                if (this->base_)
75                    this->base_->incrementReferenceCount();
76            }
77
78            template <class O>
79            inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_)
80            {
81                if (this->base_)
82                    this->base_->incrementReferenceCount();
83            }
84
85            template <class O>
86            inline SmartPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase())
87            {
88                if (this->base_)
89                    this->base_->incrementReferenceCount();
90            }
91
92            inline ~SmartPtr()
93            {
94                if (this->base_)
95                    this->base_->decrementReferenceCount();
96            }
97
98            inline SmartPtr& operator=(int)
99            {
100                SmartPtr(0).swap(*this);
101                return *this;
102            }
103
104            inline SmartPtr& operator=(T* pointer)
105            {
106                SmartPtr(pointer).swap(*this);
107                return *this;
108            }
109
110            inline SmartPtr& operator=(const SmartPtr& other)
111            {
112                SmartPtr(other).swap(*this);
113                return *this;
114            }
115
116            template <class O>
117            inline SmartPtr& operator=(const SmartPtr<O>& other)
118            {
119                SmartPtr(other).swap(*this);
120                return *this;
121            }
122
123            template <class O>
124            inline SmartPtr& operator=(const WeakPtr<O>& other)
125            {
126                SmartPtr(other).swap(*this);
127                return *this;
128            }
129
130            inline T* get() const
131            {
132                return this->pointer_;
133            }
134
135            inline OrxonoxClass* getBase() const
136            {
137                return this->base_;
138            }
139
140            inline operator T*() const
141            {
142                return this->pointer_;
143            }
144
145            inline T* operator->() const
146            {
147                assert(this->pointer_ != 0);
148                return this->pointer_;
149            }
150
151            inline T& operator*() const
152            {
153                assert(this->pointer_ != 0);
154                return *this->pointer_;
155            }
156
157            inline bool operator!() const
158            {
159                return (this->pointer_ == 0);
160            }
161
162            inline void swap(SmartPtr& other)
163            {
164                {
165                    T* temp = this->pointer_;
166                    this->pointer_ = other.pointer_;
167                    other.pointer_ = temp;
168                }
169                {
170                    OrxonoxClass* temp = this->base_;
171                    this->base_ = other.base_;
172                    other.base_ = temp;
173                }
174            }
175
176            inline void reset()
177            {
178                SmartPtr().swap(*this);
179            }
180
181        private:
182            T* pointer_;
183            OrxonoxClass* base_;
184    };
185
186    template <class T>
187    void swap(SmartPtr<T>& a, SmartPtr<T>& b)
188    {
189        a.swap(b);
190    }
191
192    template <class T, class U>
193    SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p)
194    {
195        return static_cast<T*>(p.get());
196    }
197
198    template <class T, class U>
199    SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p)
200    {
201        return const_cast<T*>(p.get());
202    }
203
204    template <class T, class U>
205    SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)
206    {
207        return orxonox_cast<T*>(p.get());
208    }
209}
210
211#endif /* _SmartPtr_H__ */
Note: See TracBrowser for help on using the repository browser.