Internal.hpp 2.46 KB
Newer Older
1
/*****************************************************************************
2
 * Internal.hpp: Wraps an internal vlc type.
3
 *****************************************************************************
4
 * Copyright © 2014 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifndef VLCPP_H
#define VLCPP_H

#include <cassert>
#include <stdlib.h>
#include <vlc/libvlc.h>
29 30
#include <memory>
#include <stdexcept>
31 32 33 34

namespace VLC
{

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
35 36 37 38
///
/// @brief The Internal class is a helper to wrap a raw libvlc type in a common
///         C++ type.
///
39
template <typename T, typename Releaser = void(*)(T*)>
40
class Internal
41 42
{
    public:
43 44
        using InternalType  = T;
        using InternalPtr   = T*;
45 46
        using Pointer       = std::shared_ptr<T>;

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
47 48 49 50
        ///
        /// \brief get returns the underlying libvlc type, or nullptr if this
        ///        is an empty instance
        ///
51
        InternalPtr get() const { return m_obj.get(); }
52

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53 54 55 56
        ///
        /// \brief isValid returns true if this instance isn't wrapping a nullptr
        /// \return
        ///
57
        bool isValid() const { return (bool)m_obj; }
58

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59 60 61
        ///
        /// \brief operator T * helper to convert to the underlying libvlc type
        ///
62 63
        operator T*() const { return m_obj.get(); }

64
    protected:
65 66
        Internal() = default;

67

68 69
        Internal( InternalPtr obj, Releaser releaser )
        {
70 71
            if ( obj == nullptr )
                throw std::runtime_error("Wrapping a NULL instance");
72
            m_obj.reset( obj, releaser );
73
        }
74

75 76
        Internal(Releaser releaser)
            : m_obj{ nullptr, releaser }
77 78
        {
        }
79

80 81
    protected:
        Pointer     m_obj;
82 83 84 85 86
};

}

#endif // VLCPP_H