From bceb568ea8d8b73546ee4f1724c22cc3d64b659d Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Mon, 7 Apr 2014 16:05:35 +0900 Subject: [PATCH] Removed some redundant code from tsmartptr.h. --- taglib/toolkit/tsmartptr.h | 130 ++++++++++++++----------------------- 1 file changed, 49 insertions(+), 81 deletions(-) diff --git a/taglib/toolkit/tsmartptr.h b/taglib/toolkit/tsmartptr.h index f04c5f11..cf85b02a 100644 --- a/taglib/toolkit/tsmartptr.h +++ b/taglib/toolkit/tsmartptr.h @@ -37,12 +37,12 @@ * \warning This is not part of the TagLib public API! */ -#if defined(HAVE_STD_SHARED_PTR) +#if defined(HAVE_STD_SHARED_PTR) # include # define SHARED_PTR std::shared_ptr -#elif defined(HAVE_TR1_SHARED_PTR) +#elif defined(HAVE_TR1_SHARED_PTR) # include # define SHARED_PTR std::tr1::shared_ptr @@ -52,50 +52,50 @@ # include # define SHARED_PTR boost::shared_ptr -#else // HAVE_STD_SHARED_PTR +#else // !HAVE_STD_SHARED_PTR && !HAVE_TR1_SHARED_PTR && !HAVE_BOOST_SHARED_PTR # include # if defined(HAVE_GCC_ATOMIC) # define ATOMIC_INT int -# define ATOMIC_INC(x) __sync_add_and_fetch(&x, 1) -# define ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1) +# define ATOMIC_INC(x) ::__sync_add_and_fetch(&x, 1) +# define ATOMIC_DEC(x) ::__sync_sub_and_fetch(&x, 1) # elif defined(HAVE_WIN_ATOMIC) # if !defined(NOMINMAX) # define NOMINMAX # endif # include -# define ATOMIC_INT long -# define ATOMIC_INC(x) InterlockedIncrement(&x) -# define ATOMIC_DEC(x) InterlockedDecrement(&x) +# define ATOMIC_INT volatile LONG +# define ATOMIC_INC(x) ::InterlockedIncrement(&x) +# define ATOMIC_DEC(x) ::InterlockedDecrement(&x) # elif defined(HAVE_MAC_ATOMIC) # include # define ATOMIC_INT int32_t -# define ATOMIC_INC(x) OSAtomicIncrement32Barrier(&x) -# define ATOMIC_DEC(x) OSAtomicDecrement32Barrier(&x) +# define ATOMIC_INC(x) ::OSAtomicIncrement32Barrier(&x) +# define ATOMIC_DEC(x) ::OSAtomicDecrement32Barrier(&x) # elif defined(HAVE_IA64_ATOMIC) # include # define ATOMIC_INT int -# define ATOMIC_INC(x) __sync_add_and_fetch(&x, 1) -# define ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1) +# define ATOMIC_INC(x) ::__sync_add_and_fetch(&x, 1) +# define ATOMIC_DEC(x) ::__sync_sub_and_fetch(&x, 1) # else # define ATOMIC_INT int # define ATOMIC_INC(x) (++x) # define ATOMIC_DEC(x) (--x) # endif -namespace TagLib +namespace TagLib { // Self-implements RefCountPtr if shared_ptr is not available. // I STRONGLY RECOMMEND using standard shared_ptr rather than this class. - + // Counter base class. Provides a reference counter. class CounterBase { public: - CounterBase() - : refCount(1) + CounterBase() : + refCount(1) { } @@ -103,8 +103,8 @@ namespace TagLib { } - void addref() - { + void addref() + { ATOMIC_INC(refCount); } @@ -124,7 +124,7 @@ namespace TagLib virtual void dispose() = 0; private: - volatile ATOMIC_INT refCount; + ATOMIC_INT refCount; }; // Counter impl class. Provides a dynamic deleter. @@ -133,14 +133,14 @@ namespace TagLib class CounterImpl : public CounterBase { public: - CounterImpl(T *p) - : p(p) + CounterImpl(T *p) : + p(p) { } - virtual void dispose() - { - delete p; + virtual void dispose() + { + delete p; } T *get() const @@ -156,31 +156,31 @@ namespace TagLib class RefCountPtr { public: - RefCountPtr() - : px(0) - , counter(0) + RefCountPtr() : + px(0), + counter(0) { } template - explicit RefCountPtr(U *p) - : px(p) - , counter(new CounterImpl(p)) + explicit RefCountPtr(U *p) : + px(p), + counter(new CounterImpl(p)) { } - RefCountPtr(const RefCountPtr &x) - : px(x.px) - , counter(x.counter) + RefCountPtr(const RefCountPtr &x) : + px(x.px), + counter(x.counter) { if(counter) counter->addref(); } template - RefCountPtr(const RefCountPtr &x) - : px(x.px) - , counter(x.counter) + RefCountPtr(const RefCountPtr &x) : + px(x.px), + counter(x.counter) { if(counter) counter->addref(); @@ -205,8 +205,8 @@ namespace TagLib return 0; } - bool unique() const - { + bool unique() const + { return (use_count() == 1); } @@ -230,32 +230,14 @@ namespace TagLib RefCountPtr &operator=(const RefCountPtr &x) { - if(px != x.px) { - if(counter) - counter->release(); - - px = x.px; - counter = x.counter; - - if(counter) - counter->addref(); - } + RefCountPtr(x).swap(*this); return *this; } template RefCountPtr &operator=(const RefCountPtr &x) { - if(px != x.px) { - if(counter) - counter->release(); - - px = x.px; - counter = x.counter; - - if(counter) - counter->addref(); - } + RefCountPtr(x).swap(*this); return *this; } @@ -309,7 +291,7 @@ namespace TagLib #endif // HAVE_STD_SHARED_PTR etc. -#if defined(HAVE_STD_UNIQUE_PTR) +#if defined(HAVE_STD_UNIQUE_PTR) # include # define SCOPED_PTR std::unique_ptr @@ -319,8 +301,8 @@ namespace TagLib # include # define SCOPED_PTR boost::scoped_ptr -#else // HAVE_STD_UNIQUE_PTR - +#else // !HAVE_STD_UNIQUE_PTR && !HAVE_BOOST_SCOPED_PTR + # include namespace TagLib @@ -328,12 +310,12 @@ namespace TagLib // Self-implements NonRefCountPtr if unique_ptr is not available. // I STRONGLY RECOMMEND using standard unique_ptr rather than this class. - template + template class NonRefCountPtr { public: - explicit NonRefCountPtr(T *p = 0) - : px(p) + explicit NonRefCountPtr(T *p = 0) : + px(p) { } @@ -352,7 +334,7 @@ namespace TagLib return *px; } - T *operator->() const + T *operator->() const { return px; } @@ -372,8 +354,7 @@ namespace TagLib return (px == 0); } - - void swap(NonRefCountPtr &x) + void swap(NonRefCountPtr &x) { std::swap(px, x.px); } @@ -390,20 +371,8 @@ namespace TagLib T *px; }; - template - bool operator==(const NonRefCountPtr &a, U *b) - { - return (a.get() == b); - } - - template - bool operator!=(const NonRefCountPtr &a, U *b) - { - return (a.get() != b); - } - template - void swap(NonRefCountPtr &a, NonRefCountPtr &b) + void swap(NonRefCountPtr &a, NonRefCountPtr &b) { a.swap(b); } @@ -413,6 +382,5 @@ namespace TagLib #endif // HAVE_STD_UNIQUE_PTR etc. - #endif // DO_NOT_DOCUMENT #endif