[Orxonox-commit 2489] r7196 - code/branches/consolecommands3/src/libraries/core
landauf at orxonox.net
landauf at orxonox.net
Sat Aug 21 16:54:29 CEST 2010
Author: landauf
Date: 2010-08-21 16:54:29 +0200 (Sat, 21 Aug 2010)
New Revision: 7196
Added:
code/branches/consolecommands3/src/libraries/core/ExecutorPtr.h
code/branches/consolecommands3/src/libraries/core/FunctorPtr.h
Modified:
code/branches/consolecommands3/src/libraries/core/Executor.cc
code/branches/consolecommands3/src/libraries/core/Executor.h
code/branches/consolecommands3/src/libraries/core/Functor.h
code/branches/consolecommands3/src/libraries/core/SharedPtr.h
Log:
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.
Modified: code/branches/consolecommands3/src/libraries/core/Executor.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Executor.cc 2010-08-21 10:43:48 UTC (rev 7195)
+++ code/branches/consolecommands3/src/libraries/core/Executor.cc 2010-08-21 14:54:29 UTC (rev 7196)
@@ -43,7 +43,6 @@
Executor::Executor(Functor* functor, const std::string& name)
{
- this->references_ = 0;
this->functor_ = functor;
this->name_ = name;
++instances_s; COUT(0) << "executor ++: " << instances_s << std::endl;
Modified: code/branches/consolecommands3/src/libraries/core/Executor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Executor.h 2010-08-21 10:43:48 UTC (rev 7195)
+++ code/branches/consolecommands3/src/libraries/core/Executor.h 2010-08-21 14:54:29 UTC (rev 7196)
@@ -35,6 +35,7 @@
#include <string>
#include "util/MultiType.h"
#include "Functor.h"
+#include "ExecutorPtr.h"
namespace orxonox
{
@@ -111,12 +112,6 @@
MultiType defaultValue_[MAX_FUNCTOR_ARGUMENTS];
private:
- inline void incrementReferenceCount()
- { ++this->references_; }
- inline void decrementReferenceCount()
- { --this->references_; if (this->references_ == 0) delete this; }
-
- int references_;
static int instances_s;
};
@@ -197,23 +192,6 @@
}
};
-
-
- typedef SharedPtr<Executor> ExecutorPtr;
-
- typedef SharedChildPtr<ExecutorStatic, Executor> ExecutorStaticPtr;
-
- template <class T>
- class ExecutorMemberPtr : public SharedChildPtr<ExecutorMember<T>, Executor>
- {
- public:
- inline ExecutorMemberPtr() : SharedChildPtr<ExecutorMember<T>, Executor>() {}
- inline ExecutorMemberPtr(ExecutorMember<T>* pointer) : SharedChildPtr<ExecutorMember<T>, Executor>(pointer) {}
-// inline ExecutorMemberPtr(const ExecutorMemberPtr& other) : SharedChildPtr<ExecutorMember<T>, Executor>(other) {}
- };
-
-
-
inline Executor* createExecutor(Functor* functor, const std::string& name = "")
{
return new Executor(functor, name);
Added: code/branches/consolecommands3/src/libraries/core/ExecutorPtr.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/ExecutorPtr.h (rev 0)
+++ code/branches/consolecommands3/src/libraries/core/ExecutorPtr.h 2010-08-21 14:54:29 UTC (rev 7196)
@@ -0,0 +1,71 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _ExecutorPtr_H__
+#define _ExecutorPtr_H__
+
+#include "CorePrereqs.h"
+#include "SharedPtr.h"
+
+namespace orxonox
+{
+ typedef SharedPtr<Executor> ExecutorPtr;
+
+ typedef SharedPtr<ExecutorStatic> ExecutorStaticPtr;
+
+ template <class T>
+ class ExecutorMemberPtr : public SharedPtr<ExecutorMember<T> >
+ {
+ public:
+ inline ExecutorMemberPtr() : SharedPtr<ExecutorMember<T> >() {}
+ inline ExecutorMemberPtr(ExecutorMember<T>* pointer) : SharedPtr<ExecutorMember<T> >(pointer) {}
+// inline ExecutorMemberPtr(const ExecutorMemberPtr& other) : SharedPtr<ExecutorMember<T> >(other) {}
+ template <class O>
+ inline ExecutorMemberPtr(const SharedPtr<O>& other) : SharedPtr<ExecutorMember<T> >(other) {}
+/*
+ inline const ExecutorMemberPtr& operator=(const ExecutorMemberPtr& other) { this->SharedPtr<ExecutorMember<T> >::operator=(other); return *this; }
+ template <class O>
+ inline const ExecutorMemberPtr& operator=(const SharedPtr<O>& other) { this->SharedPtr<ExecutorMember<T> >::operator=(other); return *this; }
+*/
+ private:
+// inline ExecutorMemberPtr(ExecutorMember<T>* pointer, int* counter) : SharedPtr<ExecutorMember<T> >(pointer, counter) {}
+ };
+/*
+ typedef SharedChildPtr<ExecutorStatic, Executor> ExecutorStaticPtr;
+
+ template <class T>
+ class ExecutorMemberPtr : public SharedChildPtr<ExecutorMember<T>, Executor>
+ {
+ public:
+ inline ExecutorMemberPtr() : SharedChildPtr<ExecutorMember<T>, Executor>() {}
+ inline ExecutorMemberPtr(ExecutorMember<T>* pointer) : SharedChildPtr<ExecutorMember<T>, Executor>(pointer) {}
+ };
+*/
+}
+
+#endif /* _ExecutorPtr_H__ */
Property changes on: code/branches/consolecommands3/src/libraries/core/ExecutorPtr.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/consolecommands3/src/libraries/core/Functor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Functor.h 2010-08-21 10:43:48 UTC (rev 7195)
+++ code/branches/consolecommands3/src/libraries/core/Functor.h 2010-08-21 14:54:29 UTC (rev 7196)
@@ -37,7 +37,7 @@
#include "util/Convert.h"
#include "util/Debug.h"
#include "util/MultiType.h"
-#include "SharedPtr.h"
+#include "FunctorPtr.h"
namespace orxonox
{
@@ -103,7 +103,7 @@
};
public:
- Functor() : references_(0) { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }
+ Functor() { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }
virtual ~Functor() { --instances_s; COUT(0) << "functor --: " << instances_s << std::endl; }
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;
@@ -123,12 +123,6 @@
virtual const std::type_info& getHeaderIdentifier() const = 0;
private:
- inline void incrementReferenceCount()
- { ++this->references_; }
- inline void decrementReferenceCount()
- { --this->references_; if (this->references_ == 0) delete this; }
-
- int references_;
static int instances_s;
};
@@ -215,21 +209,6 @@
- typedef SharedPtr<Functor> FunctorPtr;
-
- typedef SharedChildPtr<FunctorStatic, Functor> FunctorStaticPtr;
-
- template <class T>
- class FunctorMemberPtr : public SharedChildPtr<FunctorMember<T>, Functor>
- {
- public:
- inline FunctorMemberPtr() : SharedChildPtr<FunctorMember<T>, Functor>() {}
- inline FunctorMemberPtr(FunctorMember<T>* pointer) : SharedChildPtr<FunctorMember<T>, Functor>(pointer) {}
-// inline FunctorMemberPtr(const FunctorMemberPtr& other) : SharedChildPtr<FunctorMember<T>, Functor>(other) {}
- };
-
-
-
template <class R, class P1, class P2, class P3, class P4, class P5>
struct FunctorHeaderIdentifier {};
Added: code/branches/consolecommands3/src/libraries/core/FunctorPtr.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/FunctorPtr.h (rev 0)
+++ code/branches/consolecommands3/src/libraries/core/FunctorPtr.h 2010-08-21 14:54:29 UTC (rev 7196)
@@ -0,0 +1,71 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _FunctorPtr_H__
+#define _FunctorPtr_H__
+
+#include "CorePrereqs.h"
+#include "SharedPtr.h"
+
+namespace orxonox
+{
+ typedef SharedPtr<Functor> FunctorPtr;
+
+ typedef SharedPtr<FunctorStatic> FunctorStaticPtr;
+
+ template <class T>
+ class FunctorMemberPtr : public SharedPtr<FunctorMember<T> >
+ {
+ public:
+ inline FunctorMemberPtr() : SharedPtr<FunctorMember<T> >() {}
+ inline FunctorMemberPtr(FunctorMember<T>* pointer) : SharedPtr<FunctorMember<T> >(pointer) {}
+// inline FunctorMemberPtr(const FunctorMemberPtr& other) : SharedPtr<FunctorMember<T> >(other) {}
+ template <class O>
+ inline FunctorMemberPtr(const SharedPtr<O>& other) : SharedPtr<FunctorMember<T> >(other) {}
+/*
+ inline const FunctorMemberPtr& operator=(const FunctorMemberPtr& other) { this->SharedPtr<FunctorMember<T> >::operator=(other); return *this; }
+ template <class O>
+ inline const FunctorMemberPtr& operator=(const SharedPtr<O>& other) { this->SharedPtr<FunctorMember<T> >::operator=(other); return *this; }
+*/
+ private:
+// inline FunctorMemberPtr(FunctorMember<T>* pointer, int* counter) : SharedPtr<FunctorMember<T> >(pointer, counter) {}
+ };
+/*
+ typedef SharedChildPtr<FunctorStatic, Functor> FunctorStaticPtr;
+
+ template <class T>
+ class FunctorMemberPtr : public SharedChildPtr<FunctorMember<T>, Functor>
+ {
+ public:
+ inline FunctorMemberPtr() : SharedChildPtr<FunctorMember<T>, Functor>() {}
+ inline FunctorMemberPtr(FunctorMember<T>* pointer) : SharedChildPtr<FunctorMember<T>, Functor>(pointer) {}
+ };
+*/
+}
+
+#endif /* _FunctorPtr_H__ */
Property changes on: code/branches/consolecommands3/src/libraries/core/FunctorPtr.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/consolecommands3/src/libraries/core/SharedPtr.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/SharedPtr.h 2010-08-21 10:43:48 UTC (rev 7195)
+++ code/branches/consolecommands3/src/libraries/core/SharedPtr.h 2010-08-21 14:54:29 UTC (rev 7196)
@@ -30,52 +30,107 @@
#define _SharedPtr_H__
#include "CorePrereqs.h"
+#include <algorithm>
namespace orxonox
{
+ class SharedPtrDestroyer
+ {
+ public:
+ virtual void destroy() = 0;
+ };
+
template <class T>
+ class SharedPtrDestroyerImpl : public SharedPtrDestroyer
+ {
+ public:
+ SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {}
+
+ void destroy()
+ {
+// COUT(0) << "delete " << this->pointer_ << std::endl;
+ delete this->pointer_;
+ }
+
+ private:
+ T* pointer_;
+ };
+
+ template <class T>
class SharedPtr
{
+ template <class O>
+ friend class SharedPtr;
+
public:
- inline SharedPtr() : pointer_(0)
+ inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0)
{
// COUT(0) << "SharedPtr (1): " << this->pointer_ << std::endl;
}
- inline SharedPtr(T* pointer) : pointer_(pointer)
+ inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0)
{
// COUT(0) << "SharedPtr (2): " << this->pointer_ << std::endl;
if (this->pointer_)
- this->pointer_->incrementReferenceCount();
+ {
+ this->counter_ = new int(1);
+ this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_);
+ }
}
- inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_)
+ inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
{
// COUT(0) << "SharedPtr (3): " << this->pointer_ << std::endl;
if (this->pointer_)
- this->pointer_->incrementReferenceCount();
+ ++(*this->counter_);
}
+ template <class O>
+ inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
+ {
+// COUT(0) << "SharedPtr (4): " << this->pointer_ << std::endl;
+ if (this->pointer_)
+ ++(*this->counter_);
+ }
+
inline ~SharedPtr()
{
// COUT(0) << "~SharedPtr: " << this->pointer_ << std::endl;
if (this->pointer_)
- this->pointer_->decrementReferenceCount();
+ {
+ --(*this->counter_);
+
+ if (*this->counter_ == 0)
+ {
+ this->destroyer_->destroy();
+ delete this->destroyer_;
+ delete this->counter_;
+ }
+ }
}
inline const SharedPtr& operator=(const SharedPtr& other)
{
-// COUT(0) << "SharedPtr=" << std::endl;
+// COUT(0) << "SharedPtr= (1)" << std::endl;
SharedPtr(other).swap(*this);
return *this;
}
- inline void cast(const SharedPtr& other)
+ template <class O>
+ inline const SharedPtr& operator=(const SharedPtr<O>& other)
{
-// COUT(0) << "SharedPtr cast" << std::endl;
+// COUT(0) << "SharedPtr= (2)" << std::endl;
SharedPtr(other).swap(*this);
+ return *this;
}
+ template <class O>
+ inline SharedPtr<O> cast() const
+ {
+ O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast
+ return SharedPtr<O>(temp, this->counter_, this->destroyer_);
+ }
+
inline T* operator->() const
{
assert(this->pointer_ != 0);
@@ -95,15 +150,24 @@
inline void swap(SharedPtr& other)
{
- T* temp = this->pointer_;
- this->pointer_ = other.pointer_;
- other.pointer_ = temp;
+ std::swap(this->pointer_, other.pointer_);
+ std::swap(this->counter_, other.counter_);
+ std::swap(this->destroyer_, other.destroyer_);
}
private:
+ inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer)
+ {
+// COUT(0) << "SharedPtr (5): " << this->pointer_ << std::endl;
+ if (this->pointer_)
+ ++(*this->counter_);
+ }
+
T* pointer_;
+ int* counter_;
+ SharedPtrDestroyer* destroyer_;
};
-
+/*
template <class T, class Parent>
class SharedChildPtr : public SharedPtr<Parent>
{
@@ -115,6 +179,7 @@
inline T* operator->() const { return static_cast<T*>(SharedPtr<Parent>::operator->()); }
inline T& operator*() const { return *static_cast<T*>(SharedPtr<Parent>::operator->()); }
};
+*/
}
#endif /* _SharedPtr_H__ */
More information about the Orxonox-commit
mailing list