1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  *
6  * Strawberry is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Strawberry is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef VLCSCOPEDREF_H
22 #define VLCSCOPEDREF_H
23 
24 #include "config.h"
25 
26 #include <vlc/vlc.h>
27 
28 template <typename T>
29 class VlcScopedRef {
30  public:
31   explicit VlcScopedRef(T *ptr);
32   ~VlcScopedRef();
33 
34   operator T*() const { return ptr_; }
35   operator bool () const { return ptr_; }
36   T *operator->() const { return ptr_; }
37 
38  private:
39   VlcScopedRef(VlcScopedRef&) {}
40   VlcScopedRef &operator =(const VlcScopedRef&) { return *this; }
41 
42   T *ptr_;
43 };
44 
45 
46 #define VLCSCOPEDREF_DEFINE2(type, dtor) \
47     template <> void VlcScopedRef_Release<libvlc_##type##_t>(libvlc_##type##_t *ptr) { \
48       dtor(ptr); \
49     }
50 #define VLCSCOPEDREF_DEFINE(type) VLCSCOPEDREF_DEFINE2(type, libvlc_##type##_release)
51 
52 template <typename T>
53 void VlcScopedRef_Release(T *ptr);
54 
55 VLCSCOPEDREF_DEFINE2(instance, libvlc_release)
56 VLCSCOPEDREF_DEFINE(media_player)
57 VLCSCOPEDREF_DEFINE(media)
58 
59 template <> void VlcScopedRef_Release<char>(char *ptr) { free(ptr); }
60 
61 template <typename T>
62 VlcScopedRef<T>::VlcScopedRef(T *ptr)
63   : ptr_(ptr) {
64 }
65 
66 template <typename T>
67 VlcScopedRef<T>::~VlcScopedRef() {
68   VlcScopedRef_Release(ptr_);
69 }
70 
71 #endif // VLCSCOPEDREF_H
72