[Lemon-commits] [lemon_svn] alpar: r2563 - in hugo/trunk: lemon test

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:53:35 CET 2006


Author: alpar
Date: Tue Feb 21 13:37:00 2006
New Revision: 2563

Added:
   hugo/trunk/lemon/refptr.h
   hugo/trunk/test/refptr_test.cc
Modified:
   hugo/trunk/lemon/Makefile.am
   hugo/trunk/test/Makefile.am

Log:
RefPtr: a reference counted pointer class

Modified: hugo/trunk/lemon/Makefile.am
==============================================================================
--- hugo/trunk/lemon/Makefile.am	(original)
+++ hugo/trunk/lemon/Makefile.am	Tue Feb 21 13:37:00 2006
@@ -68,6 +68,7 @@
 	prim.h \
 	radix_heap.h \
 	radix_sort.h \
+	refptr.h \
 	simann.h \
 	smart_graph.h \
 	sub_graph.h \

Added: hugo/trunk/lemon/refptr.h
==============================================================================
--- (empty file)
+++ hugo/trunk/lemon/refptr.h	Tue Feb 21 13:37:00 2006
@@ -0,0 +1,146 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2006
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_REFPTR_H
+#define LEMON_REFPTR_H
+
+///\ingroup misc
+///\file
+///\brief A reference counted pointer implementation.
+///
+///\todo Undocumented
+
+
+namespace lemon {
+
+  
+  ///Reference counted pointer
+
+  ///This is a simple implementation of a reference counted pointer.
+  ///
+  ///\warning Current implementation is far from being thread-safe.
+  template<class T>
+  class RefPtr 
+  {
+    mutable RefPtr *prev, *next;
+    T *ref;
+
+    void lock() {}
+    void unlock() {}
+    
+    void attach(RefPtr &r) 
+    {
+      prev=&r; next=r.next; ref=r.ref;
+      r.next=this;
+    }
+    void attach(const T *p) 
+    {
+      prev=0; next=0; ref=p;
+    }
+    void release() 
+    {
+      if(ref) {
+	bool fr=true;
+	if(prev) { fr=false; prev->next=next; }
+	if(next) { fr=false; next->prev=prev; }
+	if(fr) delete ref;
+	ref=0;
+      }
+    }
+  
+  public:
+    ///\e
+    RefPtr() : ref(0) {}
+
+    ///\e
+    RefPtr(const RefPtr &r) {
+      lock();
+      attach(const_cast<RefPtr&>(r));
+      unlock();
+    }
+
+    ///\e
+    RefPtr(T *p) : prev(0), next(0), ref(p) {}
+
+    ///\e
+    ~RefPtr() {
+      lock();
+      release();
+      unlock();
+    }
+
+    ///\e
+    const RefPtr &operator=(const RefPtr &r) { 
+      if(ref!=r.ref) {
+	lock();
+	release(); attach(const_cast<RefPtr&>(r));
+	unlock();
+      }
+      return *this;
+    }
+  
+    ///\e
+    const RefPtr &operator=(const T* &p) { 
+      if(ref!=p) { lock(); release(); attach(p); unlock(); }
+      return *this;
+    }
+  
+    ///\e
+    void swap(RefPtr &r) {
+      RefPtr *p;
+      T *tp;
+      lock();
+      p=prev; prev=r.prev; r.prev=p;
+      p=next; next=r.next; r.next=p;
+      tp=ref; ref=r.ref; r.ref=tp;
+      unlock();
+    }
+
+    ///\e
+    void clear() { lock(); release(); unlock(); }
+
+    ///\e
+    T * operator->() { return ref; }
+    ///\e
+    const T * operator->() const { return ref; }
+    ///\e
+    operator T *() { return ref; }
+    ///\e
+    operator const T *() const { return ref; }
+    
+    ///\e
+    bool operator<(const RefPtr &r) const { return this->ref < r.ref; }
+    ///\e
+    bool operator<=(const RefPtr &r) const { return this->ref <= r.ref; }
+    ///\e
+    bool operator==(const RefPtr &r) const { return this->ref == r.ref; }
+    ///\e
+    bool operator>=(const RefPtr &r) const { return this->ref >= r.ref; }
+    ///\e
+    bool operator>(const RefPtr &r) const { return this->ref > r.ref; }
+    ///\e
+    bool operator!=(const RefPtr &r) const { return this->ref != r.ref; }
+    
+    ///\e
+    operator bool() const { return ref; }
+
+  };  //END OF CLASS REFPTR
+  
+} //END OF NAMESPACE LEMON
+
+#endif

Modified: hugo/trunk/test/Makefile.am
==============================================================================
--- hugo/trunk/test/Makefile.am	(original)
+++ hugo/trunk/test/Makefile.am	Tue Feb 21 13:37:00 2006
@@ -30,6 +30,7 @@
 	path_test \
 	preflow_test \
 	radix_sort_test \
+	refptr_test \
 	test_tools_fail \
 	test_tools_pass \
 	time_measure_test \
@@ -67,6 +68,7 @@
 suurballe_test_SOURCES = suurballe_test.cc
 path_test_SOURCES = path_test.cc
 radix_sort_test_SOURCES = radix_sort_test.cc
+refptr_test_SOURCES = refptr_test.cc
 preflow_test_SOURCES = preflow_test.cc
 time_measure_test_SOURCES = time_measure_test.cc
 test_tools_fail_SOURCES = test_tools_fail.cc

Added: hugo/trunk/test/refptr_test.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/test/refptr_test.cc	Tue Feb 21 13:37:00 2006
@@ -0,0 +1,61 @@
+/* -*- C++ -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2003-2006
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#include<lemon/refptr.h>
+#include "test_tools.h"
+#include <iostream>
+
+class Test 
+{
+  int &_counter;
+public:
+  Test(int &co) : _counter(co)
+  {
+    std::cerr << "Init\n";
+    _counter++;
+  }
+  ~Test()
+  {
+    std::cerr << "Destroy\n";
+    _counter--;
+  }
+  int &counter() { return _counter; }
+  
+};
+
+int main()
+{
+  int c=0;
+  
+  {
+    RefPtr<Test> a = new Test(c);
+    check(a->counter() == 1, "Wrong number of initialization");
+    {
+      RefPtr<Test> b = a;
+      check((*b).counter() == 1, "Wrong number of initialization");
+      b=a;
+      a=b;
+      check((*a).counter() == 1, "Wrong number of initialization");
+    }
+    check(a->counter() == 1, "Wrong number of initialization");
+  }
+  check(c == 0, "Wrong number of initialization");
+
+  return 0;
+}
+



More information about the Lemon-commits mailing list