Remove .gitignore do nothing is ignored.
[synfig.git] / ETL / trunk / ETL / _handle.h
index d0af39c..393894c 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \legal
 **     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2007, 2008 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -66,6 +67,9 @@ class shared_object
 {
 private:
        mutable int refcount;
+#ifdef ETL_LOCK_REFCOUNTS
+       mutable etl::mutex mtx;
+#endif
 
 protected:
        shared_object():refcount(0) { }
@@ -78,28 +82,43 @@ protected:
 
 public:
        void ref()const
-               { assert(refcount>=0); refcount++; }
+       {
+#ifdef ETL_LOCK_REFCOUNTS
+               etl::mutex::lock lock(mtx);
+#endif
+               assert(refcount>=0);
+               refcount++;
+       }
 
        //! Returns \c false if object needs to be deleted
        bool unref()const
        {
-               assert(refcount>0);
+               bool ret = true;
+               {
+#ifdef ETL_LOCK_REFCOUNTS
+                       etl::mutex::lock lock(mtx);
+#endif
+                       assert(refcount>0);
 
-               refcount--;
+                       refcount--;
 
-               if(refcount==0) {
+                       if(refcount==0) {
+                               ret = false;
 #ifdef ETL_SELF_DELETING_SHARED_OBJECT
-                       refcount=-666;
-                       delete this;
+                               refcount=-666;
 #endif
-                       return false;
+                       }
                }
 
-               return true;
+#ifdef ETL_SELF_DELETING_SHARED_OBJECT
+               if (!ret)
+                       delete this;
+#endif
+               return ret;
        }
 
-       int count()const
-               { return refcount; }
+       int count()const { return refcount; }
+
 }; // END of class shared_object
 
 // ========================================================================
@@ -140,6 +159,9 @@ public:
        typedef int size_type;
 
 protected:
+#ifdef _DEBUG
+public:
+#endif
        value_type *obj;                //!< Pointer to object
 
 public:
@@ -202,7 +224,7 @@ public:
        swap(handle<value_type> &x)
        {
                pointer ptr=x.obj;
-               x.obj=x.get();
+               x.obj=obj;
                obj=ptr;
                return *this;
        }
@@ -264,41 +286,29 @@ public:
        operator handle<const value_type>()const
        { return handle<const value_type>(static_cast<const_pointer>(obj)); }
 
-
        //! <tt> static_cast\<\> </tt> wrapper
-       template <class U> static
-       handle<T> cast_static(const handle<U> &x)
-       { return handle<T>(static_cast<T*>(x.get())); }
-
+       template <class U> static handle<T> cast_static         (const handle<U> &x) { return handle<T>(static_cast             <T*>(x.get())); }
        //! <tt> dynamic_cast\<\> </tt> wrapper
-       template <class U> static
-       handle<T> cast_dynamic(const handle<U> &x)
-       { return handle<T>(dynamic_cast<T*>(x.get())); }
-
+       template <class U> static handle<T> cast_dynamic        (const handle<U> &x) { return handle<T>(dynamic_cast    <T*>(x.get())); }
        //! <tt> const_cast\<\> </tt> wrapper
-       template <class U> static
-       handle<T> cast_const(const handle<U> &x)
-       { return handle<T>(const_cast<T*>(x.get())); }
-
+       template <class U> static handle<T> cast_const          (const handle<U> &x) { return handle<T>(const_cast              <T*>(x.get())); }
        //! <tt> reinterpret_cast\<\> </tt> wrapper
-       template <class U> static
-       handle<T> cast_reinterpret(const handle<U> &x)
-       { return handle<T>(reinterpret_cast<T*>(x.get())); }
+       template <class U> static handle<T> cast_reinterpret(const handle<U> &x) { return handle<T>(reinterpret_cast<T*>(x.get())); }
 
-       template<class U> static handle<T> cast_static(const loose_handle<U> &x);
-       template<class U> static handle<T> cast_dynamic(const loose_handle<U> &x);
-       template<class U> static handle<T> cast_const(const loose_handle<U> &x);
-       template<class U> static handle<T> cast_reinterpret(const loose_handle<U> &x);
+       template <class U> static handle<T> cast_static         (const loose_handle<U> &x);
+       template <class U> static handle<T> cast_dynamic        (const loose_handle<U> &x);
+       template <class U> static handle<T> cast_const          (const loose_handle<U> &x);
+       template <class U> static handle<T> cast_reinterpret(const loose_handle<U> &x);
 
-       template<class U> static handle<T> cast_static(const rhandle<U> &x);
-       template<class U> static handle<T> cast_dynamic(const rhandle<U> &x);
-       template<class U> static handle<T> cast_const(const rhandle<U> &x);
-       template<class U> static handle<T> cast_reinterpret(const rhandle<U> &x);
+       template <class U> static handle<T> cast_static         (const rhandle<U> &x);
+       template <class U> static handle<T> cast_dynamic        (const rhandle<U> &x);
+       template <class U> static handle<T> cast_const          (const rhandle<U> &x);
+       template <class U> static handle<T> cast_reinterpret(const rhandle<U> &x);
 
-       template<class U> static handle<T> cast_static(U* x);
-       template<class U> static handle<T> cast_dynamic(U* x);
-       template<class U> static handle<T> cast_const(U* x);
-       template<class U> static handle<T> cast_reinterpret(U* x);
+       template <class U> static handle<T> cast_static         (U* x);
+       template <class U> static handle<T> cast_dynamic        (U* x);
+       template <class U> static handle<T> cast_const          (U* x);
+       template <class U> static handle<T> cast_reinterpret(U* x);
 
        //! Returns pointer to the object that is being wrapped
        pointer get()const { return obj; }
@@ -370,8 +380,8 @@ public:
        using handle<value_type>::unique;
        using handle<value_type>::operator bool;
        using handle<value_type>::get;
-       using handle<value_type>::operator *;
-       using handle<value_type>::operator ->;
+       using handle<value_type>::operator*;
+       using handle<value_type>::operator->;
 
        /*
        operator const handle<value_type>&()const
@@ -615,7 +625,7 @@ public:
        {
                assert(0);
                pointer ptr=x.obj;
-               x.obj=x.get();
+               x.obj=obj;
                obj=ptr;
                return *this;
        }
@@ -643,6 +653,9 @@ public:
        typedef int size_type;
 
 protected:
+#ifdef _DEBUG
+public:
+#endif
        value_type *obj;                //!< Pointer to object
 
 public:
@@ -694,7 +707,7 @@ public:
        swap(loose_handle<value_type> &x)
        {
                pointer ptr=x.obj;
-               x.obj=x.get();
+               x.obj=obj;
                obj=ptr;
                return *this;
        }