trefcounter.h
Go to the documentation of this file.
1 /***************************************************************************
2  copyright : (C) 2013 by Tsuda Kageyu
3  email : tsuda.kageyu@gmail.com
4  ***************************************************************************/
5 
6 /***************************************************************************
7  * This library is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU Lesser General Public License version *
9  * 2.1 as published by the Free Software Foundation. *
10  * *
11  * This library is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the Free Software *
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19  * 02110-1301 USA *
20  * *
21  * Alternatively, this file is available under the Mozilla Public *
22  * License Version 1.1. You may obtain a copy of the License at *
23  * http://www.mozilla.org/MPL/ *
24  ***************************************************************************/
25 
26 #ifndef TAGLIB_REFCOUNTER_H
27 #define TAGLIB_REFCOUNTER_H
28 
29 #include "taglib_export.h"
30 #include "taglib.h"
31 
32 #ifdef __APPLE__
33 # include <libkern/OSAtomic.h>
34 # define TAGLIB_ATOMIC_MAC
35 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
36 # define NOMINMAX
37 # include <windows.h>
38 # define TAGLIB_ATOMIC_WIN
39 #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 401) \
40  && (defined(__i386__) || defined(__i486__) || defined(__i586__) || \
41  defined(__i686__) || defined(__x86_64) || defined(__ia64)) \
42  && !defined(__INTEL_COMPILER)
43 # define TAGLIB_ATOMIC_GCC
44 #elif defined(__ia64) && defined(__INTEL_COMPILER)
45 # include <ia64intrin.h>
46 # define TAGLIB_ATOMIC_GCC
47 #endif
48 
49 #ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class.
50 
56 namespace TagLib
57 {
58 
59  class TAGLIB_EXPORT RefCounter
60  {
61  public:
62  RefCounter();
63  virtual ~RefCounter();
64 
65  void ref();
66  bool deref();
67  int count() const;
68 
69  private:
70  class RefCounterPrivate;
71  RefCounterPrivate *d;
72  };
73 
74  // BIC this old class is needed by tlist.tcc and tmap.tcc
75  class RefCounterOld
76  {
77  public:
78  RefCounterOld() : refCount(1) {}
79 
80 #ifdef TAGLIB_ATOMIC_MAC
81  void ref() { OSAtomicIncrement32Barrier(const_cast<int32_t*>(&refCount)); }
82  bool deref() { return ! OSAtomicDecrement32Barrier(const_cast<int32_t*>(&refCount)); }
83  int32_t count() { return refCount; }
84  private:
85  volatile int32_t refCount;
86 #elif defined(TAGLIB_ATOMIC_WIN)
87  void ref() { InterlockedIncrement(&refCount); }
88  bool deref() { return ! InterlockedDecrement(&refCount); }
89  long count() { return refCount; }
90  private:
91  volatile long refCount;
92 #elif defined(TAGLIB_ATOMIC_GCC)
93  void ref() { __sync_add_and_fetch(&refCount, 1); }
94  bool deref() { return ! __sync_sub_and_fetch(&refCount, 1); }
95  int count() { return refCount; }
96  private:
97  volatile int refCount;
98 #else
99  void ref() { refCount++; }
100  bool deref() { return ! --refCount; }
101  int count() { return refCount; }
102  private:
103  uint refCount;
104 #endif
105  };
106 
107 }
108 
109 #endif // DO_NOT_DOCUMENT
110 #endif
111