[Orxonox-commit 2485] r7192 - code/branches/consolecommands3/src/libraries/core
landauf at orxonox.net
landauf at orxonox.net
Fri Aug 20 02:59:21 CEST 2010
Author: landauf
Date: 2010-08-20 02:59:20 +0200 (Fri, 20 Aug 2010)
New Revision: 7192
Added:
code/branches/consolecommands3/src/libraries/core/SharedPtr.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
Log:
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).
Modified: code/branches/consolecommands3/src/libraries/core/Executor.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Executor.cc 2010-08-19 16:07:52 UTC (rev 7191)
+++ code/branches/consolecommands3/src/libraries/core/Executor.cc 2010-08-20 00:59:20 UTC (rev 7192)
@@ -38,15 +38,21 @@
namespace orxonox
{
+ int Functor::instances_s = 0;
+ int Executor::instances_s = 0;
+
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;
}
Executor::~Executor()
{
delete this->functor_;
+ --instances_s; COUT(0) << "executor --: " << instances_s << std::endl;
}
MultiType Executor::parse(const std::string& params, bool* success, const std::string& delimiter) const
Modified: code/branches/consolecommands3/src/libraries/core/Executor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Executor.h 2010-08-19 16:07:52 UTC (rev 7191)
+++ code/branches/consolecommands3/src/libraries/core/Executor.h 2010-08-20 00:59:20 UTC (rev 7192)
@@ -40,6 +40,8 @@
{
class _CoreExport Executor
{
+ friend class SharedPtr<Executor>;
+
public:
Executor(Functor* functor, const std::string& name = "");
virtual ~Executor();
@@ -107,6 +109,15 @@
Functor* functor_;
std::string name_;
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;
};
class _CoreExport ExecutorStatic : public Executor
@@ -186,6 +197,23 @@
}
};
+
+
+ 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);
Modified: code/branches/consolecommands3/src/libraries/core/Functor.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/Functor.h 2010-08-19 16:07:52 UTC (rev 7191)
+++ code/branches/consolecommands3/src/libraries/core/Functor.h 2010-08-20 00:59:20 UTC (rev 7192)
@@ -37,6 +37,7 @@
#include "util/Convert.h"
#include "util/Debug.h"
#include "util/MultiType.h"
+#include "SharedPtr.h"
namespace orxonox
{
@@ -87,6 +88,8 @@
class _CoreExport Functor
{
+ friend class SharedPtr<Functor>;
+
public:
struct Type
{
@@ -100,8 +103,8 @@
};
public:
- Functor() {}
- virtual ~Functor() {}
+ Functor() : references_(0) { ++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;
@@ -118,6 +121,15 @@
virtual void* getRawObjectPointer() const { return 0; }
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;
};
class _CoreExport FunctorStatic : public Functor
@@ -203,6 +215,21 @@
+ 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/SharedPtr.h
===================================================================
--- code/branches/consolecommands3/src/libraries/core/SharedPtr.h (rev 0)
+++ code/branches/consolecommands3/src/libraries/core/SharedPtr.h 2010-08-20 00:59:20 UTC (rev 7192)
@@ -0,0 +1,120 @@
+/*
+ * 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 _SharedPtr_H__
+#define _SharedPtr_H__
+
+#include "CorePrereqs.h"
+
+namespace orxonox
+{
+ template <class T>
+ class SharedPtr
+ {
+ public:
+ inline SharedPtr() : pointer_(0)
+ {
+// COUT(0) << "SharedPtr (1): " << this->pointer_ << std::endl;
+ }
+
+ inline SharedPtr(T* pointer) : pointer_(pointer)
+ {
+// COUT(0) << "SharedPtr (2): " << this->pointer_ << std::endl;
+ if (this->pointer_)
+ this->pointer_->incrementReferenceCount();
+ }
+
+ inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_)
+ {
+// COUT(0) << "SharedPtr (3): " << this->pointer_ << std::endl;
+ if (this->pointer_)
+ this->pointer_->incrementReferenceCount();
+ }
+
+ inline ~SharedPtr()
+ {
+// COUT(0) << "~SharedPtr: " << this->pointer_ << std::endl;
+ if (this->pointer_)
+ this->pointer_->decrementReferenceCount();
+ }
+
+ inline const SharedPtr& operator=(const SharedPtr& other)
+ {
+// COUT(0) << "SharedPtr=" << std::endl;
+ SharedPtr(other).swap(*this);
+ return *this;
+ }
+
+ inline void cast(const SharedPtr& other)
+ {
+// COUT(0) << "SharedPtr cast" << std::endl;
+ SharedPtr(other).swap(*this);
+ }
+
+ inline T* operator->() const
+ {
+ assert(this->pointer_ != 0);
+ return this->pointer_;
+ }
+
+ inline T& operator*() const
+ {
+ assert(this->pointer_ != 0);
+ return *this->pointer_;
+ }
+
+ inline operator bool() const
+ {
+ return (this->pointer_ != 0);
+ }
+
+ inline void swap(SharedPtr& other)
+ {
+ T* temp = this->pointer_;
+ this->pointer_ = other.pointer_;
+ other.pointer_ = temp;
+ }
+
+ private:
+ T* pointer_;
+ };
+
+ template <class T, class Parent>
+ class SharedChildPtr : public SharedPtr<Parent>
+ {
+ public:
+ inline SharedChildPtr() : SharedPtr<Parent>() {}
+ inline SharedChildPtr(T* pointer) : SharedPtr<Parent>(pointer) {}
+ inline SharedChildPtr(const SharedChildPtr& other) : SharedPtr<Parent>(other) {}
+ inline const SharedChildPtr& operator=(const SharedChildPtr& other) { SharedPtr<Parent>::operator=(other); return *this; }
+ 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__ */
Property changes on: code/branches/consolecommands3/src/libraries/core/SharedPtr.h
___________________________________________________________________
Added: svn:eol-style
+ native
More information about the Orxonox-commit
mailing list