[Orxonox-commit 1083] r5804 - code/branches/core5/src/libraries/core

landauf at orxonox.net landauf at orxonox.net
Sat Sep 26 23:16:49 CEST 2009


Author: landauf
Date: 2009-09-26 23:16:49 +0200 (Sat, 26 Sep 2009)
New Revision: 5804

Added:
   code/branches/core5/src/libraries/core/SmartPtr.h
Modified:
   code/branches/core5/src/libraries/core/CorePrereqs.h
   code/branches/core5/src/libraries/core/OrxonoxClass.cc
   code/branches/core5/src/libraries/core/OrxonoxClass.h
Log:
added SmartPtr class

Modified: code/branches/core5/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/core5/src/libraries/core/CorePrereqs.h	2009-09-26 21:06:44 UTC (rev 5803)
+++ code/branches/core5/src/libraries/core/CorePrereqs.h	2009-09-26 21:16:49 UTC (rev 5804)
@@ -154,6 +154,8 @@
     class Shell;
     class ShellListener;
     template <class T>
+    class SmartPtr;
+    template <class T>
     class SubclassIdentifier;
     class TclBind;
     struct TclInterpreterBundle;
@@ -161,6 +163,8 @@
     class TclThreadList;
     class TclThreadManager;
     class Template;
+    template <class T>
+    class WeakPtr;
     class WindowEventListener;
     class XMLFile;
     class XMLNameListener;

Modified: code/branches/core5/src/libraries/core/OrxonoxClass.cc
===================================================================
--- code/branches/core5/src/libraries/core/OrxonoxClass.cc	2009-09-26 21:06:44 UTC (rev 5803)
+++ code/branches/core5/src/libraries/core/OrxonoxClass.cc	2009-09-26 21:16:49 UTC (rev 5804)
@@ -51,6 +51,11 @@
     /** @brief Destructor: Deletes, if existing, the list of the parents. */
     OrxonoxClass::~OrxonoxClass()
     {
+//        if (!this->requestedDestruction_)
+//            COUT(2) << "Warning: Destroyed object without destroy() (" << this->getIdentifier()->getName() << ")" << std::endl;
+
+        assert(this->referenceCount_ == 0);
+
         delete this->metaList_;
 
         // parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class
@@ -61,9 +66,8 @@
     /** @brief Deletes the object if no smart pointers point to this object. Otherwise schedules the object to be deleted as soon as possible. */
     void OrxonoxClass::destroy()
     {
-        if (this->referenceCount_ > 0)
-            this->requestedDestruction_ = true;
-        else
+        this->requestedDestruction_ = true;
+        if (this->referenceCount_ == 0)
             delete this;
     }
 

Modified: code/branches/core5/src/libraries/core/OrxonoxClass.h
===================================================================
--- code/branches/core5/src/libraries/core/OrxonoxClass.h	2009-09-26 21:06:44 UTC (rev 5803)
+++ code/branches/core5/src/libraries/core/OrxonoxClass.h	2009-09-26 21:16:49 UTC (rev 5804)
@@ -54,6 +54,9 @@
         template <class T>
         friend class ClassIdentifier;
 
+        template <class T>
+        friend class SmartPtr;
+
         public:
             OrxonoxClass();
             virtual ~OrxonoxClass();

Added: code/branches/core5/src/libraries/core/SmartPtr.h
===================================================================
--- code/branches/core5/src/libraries/core/SmartPtr.h	                        (rev 0)
+++ code/branches/core5/src/libraries/core/SmartPtr.h	2009-09-26 21:16:49 UTC (rev 5804)
@@ -0,0 +1,183 @@
+/*
+ *   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:
+ *      ...
+ *
+ */
+
+// Inspired by boost::intrusive_ptr by Peter Dimov
+
+#ifndef _SmartPtr_H__
+#define _SmartPtr_H__
+
+#include "CorePrereqs.h"
+
+#include <ostream>
+
+namespace orxonox
+{
+    template <class T>
+    class SmartPtr
+    {
+        public:
+            inline SmartPtr() : pointer_(0)
+            {
+            }
+
+            inline SmartPtr(T* pointer) : pointer_(pointer)
+            {
+                if (this->pointer_)
+                    this->pointer_->incrementReferenceCount();
+            }
+
+            inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_)
+            {
+                if (this->pointer_)
+                    this->pointer_->incrementReferenceCount();
+            }
+
+            template <class O>
+            inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get())
+            {
+                if (this->pointer_)
+                    this->pointer_->incrementReferenceCount();
+            }
+
+            inline ~SmartPtr()
+            {
+                if (this->pointer_)
+                    this->pointer_->decrementReferenceCount();
+            }
+
+            inline const SmartPtr& operator=(T* pointer)
+            {
+                SmartPtr(pointer).swap(*this);
+                return *this;
+            }
+
+            inline const SmartPtr& operator=(const SmartPtr& other)
+            {
+                SmartPtr(other).swap(*this);
+                return *this;
+            }
+
+            template <class O>
+            inline const SmartPtr& operator=(const SmartPtr<O>& other)
+            {
+                SmartPtr(other).swap(*this);
+                return *this;
+            }
+
+            inline T* get() const
+            {
+                return this->pointer_;
+            }
+
+            inline operator T*() const
+            {std::cout << "(implizit)";
+                return this->pointer_;
+            }
+
+            inline T* operator->() const
+            {
+                return this->pointer_;
+            }
+
+            inline T& operator*() const
+            {
+                return *this->pointer_;
+            }
+
+            inline bool operator!() const
+            {
+                return (this->pointer_ == 0);
+            }
+
+            inline void swap(SmartPtr& other)
+            {
+                T* temp = this->pointer_;
+                this->pointer_ = other.pointer_;
+                other.pointer_ = temp;
+            }
+
+            inline void reset()
+            {
+                SmartPtr().swap(*this);
+            }
+
+        private:
+            T* pointer_;
+    };
+
+    template <class A, class B>
+    inline bool operator==(const SmartPtr<A>& a, const SmartPtr<B>& b)
+    {
+        return (a.get() == b.get());
+    }
+
+    template <class A, class B>
+    inline bool operator!=(const SmartPtr<A>& a, const SmartPtr<B>& b)
+    {
+        return (a.get() != b.get());
+    }
+
+    template <class T>
+    inline bool operator<(const SmartPtr<T>& a, const SmartPtr<T>& b)
+    {
+        return std::less<T*>()(a.get(), b.get());
+    }
+
+    template <class T>
+    void swap(SmartPtr<T>& a, SmartPtr<T>& b)
+    {
+        a.swap(b);
+    }
+
+    template <class T, class U>
+    SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p)
+    {
+        return static_cast<T*>(p.get());
+    }
+
+    template <class T, class U>
+    SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p)
+    {
+        return const_cast<T*>(p.get());
+    }
+
+    template <class T, class U>
+    SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)
+    {
+        return dynamic_cast<T*>(p.get());
+    }
+
+    template <class T>
+    std::ostream& operator<<(std::ostream& os, const SmartPtr<T>& p)
+    {
+        os << p.get();
+        return os;
+    }
+}
+
+#endif /* _SmartPtr_H__ */


Property changes on: code/branches/core5/src/libraries/core/SmartPtr.h
___________________________________________________________________
Added: svn:eol-style
   + native




More information about the Orxonox-commit mailing list