Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7196


Ignore:
Timestamp:
Aug 21, 2010, 4:54:29 PM (14 years ago)
Author:
landauf
Message:

Changed implementation of SharedPtr, it's now non-intrusive and uses an allocated counter instead. Hence it's possible to create an instance of SharedPtr<T> even if T is only known from a forward declaration. Also changed some parts of the code to reflect the inheritance of the underlying object pointers without using inheritance in the SharedPtr itself.

Location:
code/branches/consolecommands3/src/libraries/core
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/core/Executor.cc

    r7192 r7196  
    4444    Executor::Executor(Functor* functor, const std::string& name)
    4545    {
    46         this->references_ = 0;
    4746        this->functor_ = functor;
    4847        this->name_ = name;
  • code/branches/consolecommands3/src/libraries/core/Executor.h

    r7192 r7196  
    3636#include "util/MultiType.h"
    3737#include "Functor.h"
     38#include "ExecutorPtr.h"
    3839
    3940namespace orxonox
     
    112113
    113114        private:
    114             inline void incrementReferenceCount()
    115                 { ++this->references_; }
    116             inline void decrementReferenceCount()
    117                 { --this->references_; if (this->references_ == 0) delete this; }
    118 
    119             int references_;
    120115            static int instances_s;
    121116    };
     
    198193    };
    199194
    200 
    201 
    202     typedef SharedPtr<Executor> ExecutorPtr;
    203 
    204     typedef SharedChildPtr<ExecutorStatic, Executor> ExecutorStaticPtr;
    205 
    206     template <class T>
    207     class ExecutorMemberPtr : public SharedChildPtr<ExecutorMember<T>, Executor>
    208     {
    209         public:
    210             inline ExecutorMemberPtr() : SharedChildPtr<ExecutorMember<T>, Executor>() {}
    211             inline ExecutorMemberPtr(ExecutorMember<T>* pointer) : SharedChildPtr<ExecutorMember<T>, Executor>(pointer) {}
    212 //            inline ExecutorMemberPtr(const ExecutorMemberPtr& other) : SharedChildPtr<ExecutorMember<T>, Executor>(other) {}
    213     };
    214 
    215 
    216 
    217195    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
    218196    {
  • code/branches/consolecommands3/src/libraries/core/Functor.h

    r7192 r7196  
    3838#include "util/Debug.h"
    3939#include "util/MultiType.h"
    40 #include "SharedPtr.h"
     40#include "FunctorPtr.h"
    4141
    4242namespace orxonox
     
    104104
    105105        public:
    106             Functor() : references_(0) { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }
     106            Functor() { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }
    107107            virtual ~Functor() { --instances_s; COUT(0) << "functor --: " << instances_s << std::endl; }
    108108
     
    124124
    125125        private:
    126             inline void incrementReferenceCount()
    127                 { ++this->references_; }
    128             inline void decrementReferenceCount()
    129                 { --this->references_; if (this->references_ == 0) delete this; }
    130 
    131             int references_;
    132126            static int instances_s;
    133127    };
     
    212206            T* object_;
    213207            const T* constObject_;
    214     };
    215 
    216 
    217 
    218     typedef SharedPtr<Functor> FunctorPtr;
    219 
    220     typedef SharedChildPtr<FunctorStatic, Functor> FunctorStaticPtr;
    221 
    222     template <class T>
    223     class FunctorMemberPtr : public SharedChildPtr<FunctorMember<T>, Functor>
    224     {
    225         public:
    226             inline FunctorMemberPtr() : SharedChildPtr<FunctorMember<T>, Functor>() {}
    227             inline FunctorMemberPtr(FunctorMember<T>* pointer) : SharedChildPtr<FunctorMember<T>, Functor>(pointer) {}
    228 //            inline FunctorMemberPtr(const FunctorMemberPtr& other) : SharedChildPtr<FunctorMember<T>, Functor>(other) {}
    229208    };
    230209
  • code/branches/consolecommands3/src/libraries/core/SharedPtr.h

    r7192 r7196  
    3131
    3232#include "CorePrereqs.h"
     33#include <algorithm>
    3334
    3435namespace orxonox
    3536{
     37    class SharedPtrDestroyer
     38    {
     39        public:
     40            virtual void destroy() = 0;
     41    };
     42
     43    template <class T>
     44    class SharedPtrDestroyerImpl : public SharedPtrDestroyer
     45    {
     46        public:
     47            SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {}
     48
     49            void destroy()
     50            {
     51//                COUT(0) << "delete " << this->pointer_ << std::endl;
     52                delete this->pointer_;
     53            }
     54
     55        private:
     56            T* pointer_;
     57    };
     58
    3659    template <class T>
    3760    class SharedPtr
    3861    {
     62        template <class O>
     63        friend class SharedPtr;
     64
    3965        public:
    40             inline SharedPtr() : pointer_(0)
     66            inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0)
    4167            {
    4268//                COUT(0) << "SharedPtr (1): " << this->pointer_ << std::endl;
    4369            }
    4470
    45             inline SharedPtr(T* pointer) : pointer_(pointer)
     71            inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0)
    4672            {
    4773//                COUT(0) << "SharedPtr (2): " << this->pointer_ << std::endl;
    4874                if (this->pointer_)
    49                     this->pointer_->incrementReferenceCount();
     75                {
     76                    this->counter_ = new int(1);
     77                    this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_);
     78                }
    5079            }
    5180
    52             inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_)
     81            inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
    5382            {
    5483//                COUT(0) << "SharedPtr (3): " << this->pointer_ << std::endl;
    5584                if (this->pointer_)
    56                     this->pointer_->incrementReferenceCount();
     85                    ++(*this->counter_);
     86            }
     87
     88            template <class O>
     89            inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
     90            {
     91//                COUT(0) << "SharedPtr (4): " << this->pointer_ << std::endl;
     92                if (this->pointer_)
     93                    ++(*this->counter_);
    5794            }
    5895
     
    6198//                COUT(0) << "~SharedPtr: " << this->pointer_ << std::endl;
    6299                if (this->pointer_)
    63                     this->pointer_->decrementReferenceCount();
     100                {
     101                    --(*this->counter_);
     102
     103                    if (*this->counter_ == 0)
     104                    {
     105                        this->destroyer_->destroy();
     106                        delete this->destroyer_;
     107                        delete this->counter_;
     108                    }
     109                }
    64110            }
    65111
    66112            inline const SharedPtr& operator=(const SharedPtr& other)
    67113            {
    68 //                COUT(0) << "SharedPtr=" << std::endl;
     114//                COUT(0) << "SharedPtr= (1)" << std::endl;
    69115                SharedPtr(other).swap(*this);
    70116                return *this;
    71117            }
    72118
    73             inline void cast(const SharedPtr& other)
     119            template <class O>
     120            inline const SharedPtr& operator=(const SharedPtr<O>& other)
    74121            {
    75 //                COUT(0) << "SharedPtr cast" << std::endl;
     122//                COUT(0) << "SharedPtr= (2)" << std::endl;
    76123                SharedPtr(other).swap(*this);
     124                return *this;
     125            }
     126
     127            template <class O>
     128            inline SharedPtr<O> cast() const
     129            {
     130                O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast
     131                return SharedPtr<O>(temp, this->counter_, this->destroyer_);
    77132            }
    78133
     
    96151            inline void swap(SharedPtr& other)
    97152            {
    98                 T* temp = this->pointer_;
    99                 this->pointer_ = other.pointer_;
    100                 other.pointer_ = temp;
     153                std::swap(this->pointer_, other.pointer_);
     154                std::swap(this->counter_, other.counter_);
     155                std::swap(this->destroyer_, other.destroyer_);
    101156            }
    102157
    103158        private:
     159            inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer)
     160            {
     161//                COUT(0) << "SharedPtr (5): " << this->pointer_ << std::endl;
     162                if (this->pointer_)
     163                    ++(*this->counter_);
     164            }
     165
    104166            T* pointer_;
     167            int* counter_;
     168            SharedPtrDestroyer* destroyer_;
    105169    };
    106 
     170/*
    107171    template <class T, class Parent>
    108172    class SharedChildPtr : public SharedPtr<Parent>
     
    116180            inline T& operator*() const { return *static_cast<T*>(SharedPtr<Parent>::operator->()); }
    117181    };
     182*/
    118183}
    119184
Note: See TracChangeset for help on using the changeset viewer.