Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/SharedPtr.h @ 7287

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

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 5.3 KB
RevLine 
[7192]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
[7205]32#include "UtilPrereqs.h"
[7264]33
[7196]34#include <algorithm>
[7197]35#include <cassert>
[7192]36
[7264]37#include "SmallObjectAllocator.h"
38
[7192]39namespace orxonox
40{
[7264]41    class SharedCounter
[7196]42    {
43        public:
[7264]44            SharedCounter() : count_(1) {}
[7196]45            virtual void destroy() = 0;
[7264]46
47            int count_;
[7196]48    };
49
[7192]50    template <class T>
[7264]51    class SharedCounterImpl : public SharedCounter
[7196]52    {
53        public:
[7264]54            SharedCounterImpl(T* pointer) : pointer_(pointer) {}
[7196]55
56            void destroy()
57            {
58                delete this->pointer_;
59            }
60
61        private:
62            T* pointer_;
63    };
64
[7264]65    _UtilExport SmallObjectAllocator& createSharedCounterPool();
66
67    FORCEINLINE SmallObjectAllocator& getSharedCounterPool()
68    {
69        static SmallObjectAllocator& instance = createSharedCounterPool();
70        return instance;
71    }
72
[7196]73    template <class T>
[7192]74    class SharedPtr
75    {
[7196]76        template <class O>
77        friend class SharedPtr;
78
[7192]79        public:
[7264]80            inline SharedPtr() : pointer_(0), counter_(0)
[7192]81            {
82            }
83
[7264]84            inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0)
[7192]85            {
86                if (this->pointer_)
[7196]87                {
[7264]88                    void* chunk = getSharedCounterPool().alloc();
89                    this->counter_ = new (chunk) SharedCounterImpl<T>(this->pointer_);
[7196]90                }
[7192]91            }
92
[7264]93            inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_)
[7192]94            {
95                if (this->pointer_)
[7264]96                    ++this->counter_->count_;
[7192]97            }
98
[7196]99            template <class O>
[7264]100            inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_)
[7196]101            {
102                if (this->pointer_)
[7264]103                    ++this->counter_->count_;
[7196]104            }
105
[7192]106            inline ~SharedPtr()
107            {
108                if (this->pointer_)
[7196]109                {
[7264]110                    --this->counter_->count_;
[7196]111
[7264]112                    if (this->counter_->count_ == 0)
[7196]113                    {
[7264]114                        this->counter_->destroy();
115                        getSharedCounterPool().free(this->counter_);
[7196]116                    }
117                }
[7192]118            }
119
[7269]120            inline SharedPtr& operator=(const SharedPtr& other)
[7192]121            {
122                SharedPtr(other).swap(*this);
123                return *this;
124            }
125
[7196]126            template <class O>
[7269]127            inline SharedPtr& operator=(const SharedPtr<O>& other)
[7192]128            {
129                SharedPtr(other).swap(*this);
[7196]130                return *this;
[7192]131            }
132
[7196]133            template <class O>
134            inline SharedPtr<O> cast() const
135            {
136                O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast
[7264]137                return SharedPtr<O>(temp, this->counter_);
[7196]138            }
139
[7192]140            inline T* operator->() const
141            {
142                assert(this->pointer_ != 0);
143                return this->pointer_;
144            }
145
146            inline T& operator*() const
147            {
148                assert(this->pointer_ != 0);
149                return *this->pointer_;
150            }
151
[7212]152            inline T* get() const
153            {
154                return this->pointer_;
155            }
156
[7192]157            inline operator bool() const
158            {
159                return (this->pointer_ != 0);
160            }
161
162            inline void swap(SharedPtr& other)
163            {
[7196]164                std::swap(this->pointer_, other.pointer_);
165                std::swap(this->counter_, other.counter_);
[7192]166            }
167
168        private:
[7264]169            inline SharedPtr(T* pointer, SharedCounter* counter) : pointer_(pointer), counter_(counter)
[7196]170            {
171                if (this->pointer_)
[7264]172                    ++this->counter_->count_;
[7196]173            }
174
[7192]175            T* pointer_;
[7264]176            SharedCounter* counter_;
[7192]177    };
[7197]178
[7192]179    template <class T, class Parent>
[7197]180    class SharedChildPtr : public Parent
[7192]181    {
182        public:
[7197]183            inline SharedChildPtr() : Parent() {}
184            inline SharedChildPtr(T* pointer) : Parent(pointer) {}
185            inline SharedChildPtr(const SharedPtr<T>& other) : Parent(other) {}
186
187            inline T* operator->() const { return static_cast<T*>(Parent::operator->()); }
188            inline T& operator*() const { return *static_cast<T*>(Parent::operator->()); }
[7192]189    };
[7197]190
[7192]191}
192
193#endif /* _SharedPtr_H__ */
Note: See TracBrowser for help on using the repository browser.