strawberry-audio-player-win.../src/engine/vlcscopedref.h

72 lines
1.8 KiB
C
Raw Normal View History

/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
2018-02-27 18:06:05 +01:00
#ifndef VLCSCOPEDREF_H
#define VLCSCOPEDREF_H
#include "config.h"
#include <vlc/vlc.h>
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
class VlcScopedRef {
public:
2021-06-12 20:53:23 +02:00
explicit VlcScopedRef(T *ptr);
2018-02-27 18:06:05 +01:00
~VlcScopedRef();
2022-03-22 21:09:05 +01:00
operator T *() const { return ptr_; }
operator bool() const { return ptr_; }
2021-06-12 20:53:23 +02:00
T *operator->() const { return ptr_; }
2018-02-27 18:06:05 +01:00
private:
2020-06-26 22:41:38 +02:00
VlcScopedRef(VlcScopedRef&) {}
2021-06-12 20:53:23 +02:00
VlcScopedRef &operator =(const VlcScopedRef&) { return *this; }
2018-02-27 18:06:05 +01:00
2021-06-12 20:53:23 +02:00
T *ptr_;
2018-02-27 18:06:05 +01:00
};
#define VLCSCOPEDREF_DEFINE2(type, dtor) \
2021-06-12 20:53:23 +02:00
template <> void VlcScopedRef_Release<libvlc_##type##_t>(libvlc_##type##_t *ptr) { \
2018-02-27 18:06:05 +01:00
dtor(ptr); \
}
#define VLCSCOPEDREF_DEFINE(type) VLCSCOPEDREF_DEFINE2(type, libvlc_##type##_release)
2022-03-22 21:09:05 +01:00
template<typename T>
2021-06-12 20:53:23 +02:00
void VlcScopedRef_Release(T *ptr);
2018-02-27 18:06:05 +01:00
2019-09-15 20:27:32 +02:00
VLCSCOPEDREF_DEFINE2(instance, libvlc_release)
VLCSCOPEDREF_DEFINE(media_player)
VLCSCOPEDREF_DEFINE(media)
2018-02-27 18:06:05 +01:00
2022-03-22 21:09:05 +01:00
template<> void VlcScopedRef_Release<char>(char *ptr) { free(ptr); }
2018-02-27 18:06:05 +01:00
2022-03-22 21:09:05 +01:00
template<typename T>
2021-06-12 20:53:23 +02:00
VlcScopedRef<T>::VlcScopedRef(T *ptr)
2022-03-22 21:09:05 +01:00
: ptr_(ptr) {
2018-02-27 18:06:05 +01:00
}
2022-03-22 21:09:05 +01:00
template<typename T>
2018-02-27 18:06:05 +01:00
VlcScopedRef<T>::~VlcScopedRef() {
VlcScopedRef_Release(ptr_);
}
2022-03-22 21:09:05 +01:00
#endif // VLCSCOPEDREF_H