Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7192


Ignore:
Timestamp:
Aug 20, 2010, 2:59:20 AM (14 years ago)
Author:
landauf
Message:

Added a small SharedPtr template for use in Functor and Executor. It's an intrusive approach that requires the object to implement a reference counter. The SharedPtr is extensible to reflect the hierarchy of Functor, FunctorStatic, FunctorMember<T>, and all subclasses (same for Executor).

Location:
code/branches/consolecommands3/src/libraries/core
Files:
1 added
3 edited

Legend:

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

    r7189 r7192  
    3939namespace orxonox
    4040{
     41    int Functor::instances_s = 0;
     42    int Executor::instances_s = 0;
     43
    4144    Executor::Executor(Functor* functor, const std::string& name)
    4245    {
     46        this->references_ = 0;
    4347        this->functor_ = functor;
    4448        this->name_ = name;
     49        ++instances_s; COUT(0) << "executor ++: " << instances_s << std::endl;
    4550    }
    4651
     
    4853    {
    4954        delete this->functor_;
     55        --instances_s; COUT(0) << "executor --: " << instances_s << std::endl;
    5056    }
    5157
  • code/branches/consolecommands3/src/libraries/core/Executor.h

    r7189 r7192  
    4141    class _CoreExport Executor
    4242    {
     43        friend class SharedPtr<Executor>;
     44
    4345        public:
    4446            Executor(Functor* functor, const std::string& name = "");
     
    108110            std::string name_;
    109111            MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS];
     112
     113        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_;
     120            static int instances_s;
    110121    };
    111122
     
    187198    };
    188199
     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
    189217    inline Executor* createExecutor(Functor* functor, const std::string& name = "")
    190218    {
  • code/branches/consolecommands3/src/libraries/core/Functor.h

    r7189 r7192  
    3838#include "util/Debug.h"
    3939#include "util/MultiType.h"
     40#include "SharedPtr.h"
    4041
    4142namespace orxonox
     
    8889    class _CoreExport Functor
    8990    {
     91        friend class SharedPtr<Functor>;
     92
    9093        public:
    9194            struct Type
     
    101104
    102105        public:
    103             Functor() {}
    104             virtual ~Functor() {}
     106            Functor() : references_(0) { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }
     107            virtual ~Functor() { --instances_s; COUT(0) << "functor --: " << instances_s << std::endl; }
    105108
    106109            virtual MultiType operator()(const MultiType& param1 = MT_Type::Null, const MultiType& param2 = MT_Type::Null, const MultiType& param3 = MT_Type::Null, const MultiType& param4 = MT_Type::Null, const MultiType& param5 = MT_Type::Null) = 0;
     
    119122
    120123            virtual const std::type_info& getHeaderIdentifier() const = 0;
     124
     125        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_;
     132            static int instances_s;
    121133    };
    122134
     
    200212            T* object_;
    201213            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) {}
    202229    };
    203230
Note: See TracChangeset for help on using the changeset viewer.