new audio engine backend for native CoreAudio audio I/O, and PortMIDI for MIDI.
[ardour.git] / libs / backends / wavesaudio / wavesapi / refmanager / WCRefManager.h
1 /*
2     Copyright (C) 2013 Waves Audio Ltd.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 #ifndef WCREFMANAGER_H
20 #define WCREFMANAGER_H
21
22
23 #define SAFE_RELEASE(p) if (p) {p->Release(); p = NULL;}
24
25
26 //In order to use this interface, derive the Interface class
27 //from WCRefManager_Interface and derive the implementation class
28 //from WCRefManager_Impl. Further, in the implementation class
29 //declaration, place the macro WCREFMANAGER_IMPL.
30 class WCRefManager_Interface
31 {
32 public:
33         /// Constructor.
34         WCRefManager_Interface() {};
35         /// Destructor.
36         virtual ~WCRefManager_Interface() {};
37         /// Adds a reference to class.
38         virtual void AddRef() = 0;
39         /// Decrements reference count and deletes the object if reference count becomes zero.
40         virtual void Release() = 0;
41 };
42
43 ///! See details at WCRefManager_Interface for how to use this.
44 class WCRefManager_Impl
45 {
46 public:
47     WCRefManager_Impl () : m_RefCount(1) {}
48     virtual ~WCRefManager_Impl() {}
49 protected:
50         /// Variable to store reference count.
51         unsigned int m_RefCount;
52
53 /// Helper to put implementation in an interface derived class, don't forget to
54 /// derive the impl from WCRefManager_Impl
55 #define WCREFMAN_IMPL \
56     public: \
57         virtual void AddRef() {m_RefCount++;} \
58         virtual void Release() {m_RefCount--; if (m_RefCount<=0) delete this;}
59
60 };
61
62
63 class WCRefManager
64 {
65 public:
66         /// Construcotr.
67         WCRefManager();
68         /// Destructor.
69         virtual ~WCRefManager();
70         /// Adds a reference to class.
71         void AddRef();
72         /// Decrements reference count and deletes the object if reference count becomes zero.
73         void Release();
74
75 private:
76         /// Variable to store reference count.
77         unsigned int m_RefCount;
78 };
79
80 #endif // WCREFMANAGER_H