windstille-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Windstille-devel] rev 337 - trunk/src


From: Ingo Ruhnke
Subject: [Windstille-devel] rev 337 - trunk/src
Date: Sun, 16 May 2004 16:01:04 +0200

Author: grumbel
Date: 2004-05-16 16:01:03 +0200 (Sun, 16 May 2004)
New Revision: 337

Added:
   trunk/src/shared_ptr.hxx
Modified:
   trunk/src/SConstruct
   trunk/src/flexlay.i
   trunk/src/layer.cxx
   trunk/src/layer.hxx
   trunk/src/tilemap_layer.cxx
   trunk/src/tilemap_layer.hxx
Log:
- added sharedptr class

Modified: trunk/src/SConstruct
===================================================================
--- trunk/src/SConstruct        2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/SConstruct        2004-05-16 14:01:03 UTC (rev 337)
@@ -30,6 +30,9 @@
                              'object_selector.hxx']) 
 env.Command('flexlay_wrap.cxx', 'flexlay.i', "swig -python -c++ $SOURCE")
 
+env.Program('sharedptrtest', 'sharedptrtest.cxx',
+            
CPPPATH=['/home/ingo/run/ClanLib-0.7-current//include/ClanLib-0.7/'])
+
 env.SharedLibrary(
     target = '_flexlay.so',
     source = [

Modified: trunk/src/flexlay.i
===================================================================
--- trunk/src/flexlay.i 2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/flexlay.i 2004-05-16 14:01:03 UTC (rev 337)
@@ -65,4 +65,5 @@
 %include "object_selector.hxx"
 %include "sexpr_parser.hxx"
 
+
 /* EOF */

Modified: trunk/src/layer.cxx
===================================================================
--- trunk/src/layer.cxx 2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/layer.cxx 2004-05-16 14:01:03 UTC (rev 337)
@@ -21,19 +21,23 @@
 #include "layer.hxx"
 
 Layer::Layer(LayerImpl* i)
-  : impl(i)
 {
+  impl = SharedPtr<LayerImpl>(i);
 }
 
 Layer::Layer()
 {  
 }
 
-Layer::Layer(CL_SharedPtr<LayerImpl> i)
+Layer::Layer(SharedPtr<LayerImpl> i)
   : impl(i)
 {
 }
 
+Layer::~Layer()
+{
+}
+
 void
 Layer::draw(EditorMapComponent* parent) 
 { 

Modified: trunk/src/layer.hxx
===================================================================
--- trunk/src/layer.hxx 2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/layer.hxx 2004-05-16 14:01:03 UTC (rev 337)
@@ -20,8 +20,8 @@
 #ifndef HEADER_LAYER_HXX
 #define HEADER_LAYER_HXX
 
-#include <ClanLib/Core/System/sharedptr.h>
 #include <ClanLib/Core/Math/rect.h>
+#include "shared_ptr.hxx"
 
 class EditorMapComponent;
 class LayerImpl;
@@ -37,14 +37,15 @@
 public:
   Layer();
   Layer(LayerImpl* i);
-  Layer(CL_SharedPtr<LayerImpl> i);
+  Layer(SharedPtr<LayerImpl> i);
+  ~Layer();
   
   void draw(EditorMapComponent* parent);
   bool has_bounding_rect() const;
   CL_Rect get_bounding_rect();
 
 private:
-  CL_SharedPtr<LayerImpl> impl;
+  SharedPtr<LayerImpl> impl;
 };
 
 #endif

Added: trunk/src/shared_ptr.hxx
===================================================================
--- trunk/src/shared_ptr.hxx    2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/shared_ptr.hxx    2004-05-16 14:01:03 UTC (rev 337)
@@ -0,0 +1,151 @@
+//  $Id$
+// 
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
+//
+//  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#ifndef HEADER_SHARED_PTR_HXX
+#define HEADER_SHARED_PTR_HXX
+ 
+template<class T>
+class SharedPtrDeleter
+{
+public:
+  T* ptr; 
+
+  SharedPtrDeleter(T* p) : ptr(p) {}
+  virtual ~SharedPtrDeleter() {}
+  
+  virtual void del() =0;
+  virtual SharedPtrDeleter* clone() =0;
+};
+
+template<class T>
+class SharedPtrDeleterImpl : public SharedPtrDeleter<T>
+{
+public:
+  SharedPtrDeleterImpl(T* p)
+    : SharedPtrDeleter<T>(p) {}
+
+  ~SharedPtrDeleterImpl()
+  {
+  }  
+
+  void del() {
+    delete this->ptr;
+  }
+
+  SharedPtrDeleter<T>* clone() {
+    return new SharedPtrDeleterImpl<T>(ptr);
+  }
+};
+
+template<class T>
+class SharedPtr
+{
+private:
+  SharedPtrDeleter<T>* deleter;
+  unsigned int* ref_count;
+
+  void inc() {
+    if (ref_count) *ref_count += 1;
+  }
+  
+  void dec() {
+    if (ref_count) 
+      {
+        *ref_count -= 1;
+        if (*ref_count == 0) {
+          deleter->del();
+          delete ref_count;
+        }
+      }
+  }
+public:
+  template<class Base> friend class SharedPtr;
+
+  SharedPtr()
+    : deleter(0), ref_count(0)
+  {}
+
+  template<typename D>
+  SharedPtr(D* p)
+    : deleter(new SharedPtrDeleterImpl<T>(p)), ref_count(new unsigned int(1))
+  {}
+  
+  template<class Base>
+  SharedPtr(const SharedPtr<Base>& copy)
+  {
+    dec();
+    delete deleter;
+    deleter   = new SharedPtrDeleterImpl<T>(copy.deleter->ptr);
+    ref_count = copy.ref_count;
+    inc();
+  }
+
+  SharedPtr(const SharedPtr<T>& copy) 
+    : deleter(copy.deleter->clone()), ref_count(copy.ref_count)
+  {
+    inc();
+  }
+
+  template<class Base>
+  SharedPtr<T>& operator= (const SharedPtr<Base>& copy) 
+  {
+    dec();
+    delete deleter;
+    deleter   = new SharedPtrDeleterImpl<T>(copy.deleter->ptr);
+    ref_count = copy.ref_count;
+    inc();
+
+    return *this;
+  }
+  
+  SharedPtr<T>& operator= (const SharedPtr<T>& copy) 
+  {
+    if (this != &copy)
+      {
+        dec();
+        delete deleter;
+        deleter   = copy.deleter->clone();
+        ref_count = copy.ref_count;
+        inc();
+      }
+    return *this;
+  }
+
+  ~SharedPtr()
+  {
+    dec();
+    delete deleter;
+  }
+
+  //: Dereferencing operator.
+  T& operator*() { return *deleter->ptr; }
+
+  T const& operator*() const { return *deleter->ptr; }
+       
+  //: Indirect member access operator.
+  T* operator->() { return deleter->ptr; }
+
+  T const* operator->() const { return deleter->ptr; }
+
+  T* get() const { return deleter->ptr; }
+};
+
+#endif
+
+/* EOF */

Modified: trunk/src/tilemap_layer.cxx
===================================================================
--- trunk/src/tilemap_layer.cxx 2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/tilemap_layer.cxx 2004-05-16 14:01:03 UTC (rev 337)
@@ -421,8 +421,7 @@
 Layer
 TilemapLayer::to_layer()
 {
-  // FIXME: BUG!!!!!
-  return Layer(impl.get());
+  return Layer(impl);
 }
 
 /* EOF */

Modified: trunk/src/tilemap_layer.hxx
===================================================================
--- trunk/src/tilemap_layer.hxx 2004-05-16 02:56:45 UTC (rev 336)
+++ trunk/src/tilemap_layer.hxx 2004-05-16 14:01:03 UTC (rev 337)
@@ -20,9 +20,9 @@
 #ifndef HEADER_TILEMAP_LAYER_HXX
 #define HEADER_TILEMAP_LAYER_HXX
 
-#include <ClanLib/Core/System/sharedptr.h>
 #include <ClanLib/Display/pixel_buffer.h>
 #include "field.hxx"
+#include "shared_ptr.hxx"
 #include "layer.hxx"
 
 class Tileset;
@@ -97,7 +97,7 @@
   Layer to_layer();
 
 private:
-  CL_SharedPtr<TilemapLayerImpl> impl;
+  SharedPtr<TilemapLayerImpl> impl;
 };
 
 #endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]