trefcounter.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     copyright            : (C) 2013 by Tsuda Kageyu
00003     email                : tsuda.kageyu@gmail.com
00004  ***************************************************************************/
00005 
00006 /***************************************************************************
00007  *   This library is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU Lesser General Public License version   *
00009  *   2.1 as published by the Free Software Foundation.                     *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful, but   *
00012  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the Free Software   *
00018  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
00019  *   02110-1301  USA                                                       *
00020  *                                                                         *
00021  *   Alternatively, this file is available under the Mozilla Public        *
00022  *   License Version 1.1.  You may obtain a copy of the License at         *
00023  *   http://www.mozilla.org/MPL/                                           *
00024  ***************************************************************************/
00025 
00026 #ifndef TAGLIB_REFCOUNTER_H
00027 #define TAGLIB_REFCOUNTER_H
00028 
00029 #include "taglib_export.h"
00030 #include "taglib.h"
00031 
00032 #ifdef __APPLE__
00033 #  include <libkern/OSAtomic.h>
00034 #  define TAGLIB_ATOMIC_MAC
00035 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
00036 #  ifndef NOMINMAX
00037 #    define NOMINMAX
00038 #  endif
00039 #  include <windows.h>
00040 #  define TAGLIB_ATOMIC_WIN
00041 #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 401)    \
00042       && (defined(__i386__) || defined(__i486__) || defined(__i586__) || \
00043           defined(__i686__) || defined(__x86_64) || defined(__ia64)) \
00044       && !defined(__INTEL_COMPILER)
00045 #  define TAGLIB_ATOMIC_GCC
00046 #elif defined(__ia64) && defined(__INTEL_COMPILER)
00047 #  include <ia64intrin.h>
00048 #  define TAGLIB_ATOMIC_GCC
00049 #endif
00050 
00051 #ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class.
00052 
00058 namespace TagLib
00059 {
00060 
00061   class TAGLIB_EXPORT RefCounter
00062   {
00063   public:
00064     RefCounter();
00065     virtual ~RefCounter();
00066 
00067     void ref();
00068     bool deref();
00069     int count() const;
00070 
00071   private:
00072     class RefCounterPrivate;
00073     RefCounterPrivate *d;
00074   };
00075 
00076   // BIC this old class is needed by tlist.tcc and tmap.tcc
00077   class RefCounterOld
00078   {
00079   public:
00080     RefCounterOld() : refCount(1) {}
00081 
00082 #ifdef TAGLIB_ATOMIC_MAC
00083     void ref() { OSAtomicIncrement32Barrier(const_cast<int32_t*>(&refCount)); }
00084     bool deref() { return ! OSAtomicDecrement32Barrier(const_cast<int32_t*>(&refCount)); }
00085     int32_t count() { return refCount; }
00086   private:
00087     volatile int32_t refCount;
00088 #elif defined(TAGLIB_ATOMIC_WIN)
00089     void ref() { InterlockedIncrement(&refCount); }
00090     bool deref() { return ! InterlockedDecrement(&refCount); }
00091     long count() { return refCount; }
00092   private:
00093     volatile long refCount;
00094 #elif defined(TAGLIB_ATOMIC_GCC)
00095     void ref() { __sync_add_and_fetch(&refCount, 1); }
00096     bool deref() { return ! __sync_sub_and_fetch(&refCount, 1); }
00097     int count() { return refCount; }
00098   private:
00099     volatile int refCount;
00100 #else
00101     void ref() { refCount++; }
00102     bool deref() { return ! --refCount; }
00103     int count() { return refCount; }
00104   private:
00105     unsigned int refCount;
00106 #endif
00107   };
00108 
00109 }
00110 
00111 #endif // DO_NOT_DOCUMENT
00112 #endif
00113