Commit 024ee4d9 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Use smart pointers only when necessary

parent a79d424e
......@@ -19,22 +19,15 @@ include(cpp11)
add_definitions("-Wall -Wextra -pedantic")
EnableCpp11()
file(GLOB LIBVLCPP_HEADERS "${CMAKE_SOURCE_DIR}/src/*.hpp")
add_executable(${PROJECT_NAME}
test/main.cpp
${LIBVLCPP_HEADERS}
)
include_directories("${CMAKE_SOURCE_DIR}/src/")
file(GLOB LIBVLCPP_PUBLIC_HEADERS "${CMAKE_SOURCE_DIR}/src/*.hpp")
set_target_properties(${PROJECT_NAME} PROPERTIES
PUBLIC_HEADER "${LIBVLCPP_PUBLIC_HEADERS}"
)
find_package(LIBVLC REQUIRED)
target_link_libraries( ${PROJECT_NAME} ${LIBVLC_LIBRARY} ${LIBVLCCORE_LIBRARY} )
include_directories(${LIBVLC_INCLUDE_DIR})
file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
set(CONF_INCLUDE_DIRS "\${LIBVLCPP_CMAKE_DIR}/${REL_INCLUDE_DIR}")
......@@ -135,7 +135,7 @@ class VLCPP_API EventManager : public Internal<libvlc_event_manager_t>
return false;
if ( type < libvlc_MediaMetaChanged || type > libvlc_MediaSubItemTreeAdded )
return false;
libvlc_event_attach( get(), type, &handleMediaEvent, cb );
libvlc_event_attach( *this,type, &handleMediaEvent, cb );
return true;
}
......@@ -145,7 +145,7 @@ class VLCPP_API EventManager : public Internal<libvlc_event_manager_t>
return false;
if ( type < libvlc_MediaPlayerMediaChanged || type > libvlc_MediaPlayerESSelected )
return false;
libvlc_event_attach( get(), type, &handleMediaPlayerEvent, cb );
libvlc_event_attach( *this,type, &handleMediaPlayerEvent, cb );
return true;
}
......@@ -155,7 +155,7 @@ class VLCPP_API EventManager : public Internal<libvlc_event_manager_t>
return false;
if ( type < libvlc_MediaListItemAdded || type > libvlc_MediaListWillDeleteItem )
return false;
libvlc_event_attach( get(), type, &handleMediaListEvent, cb );
libvlc_event_attach( *this,type, &handleMediaListEvent, cb );
return true;
}
......@@ -165,7 +165,7 @@ class VLCPP_API EventManager : public Internal<libvlc_event_manager_t>
return false;
if ( type < libvlc_MediaListPlayerPlayed || type > libvlc_MediaListPlayerStopped )
return false;
libvlc_event_attach( get(), type, &handleMediaListPlayerEvent, cb );
libvlc_event_attach( *this,type, &handleMediaListPlayerEvent, cb );
return true;
}
......@@ -175,7 +175,7 @@ class VLCPP_API EventManager : public Internal<libvlc_event_manager_t>
return false;
if ( type < libvlc_MediaDiscovererStarted || type > libvlc_MediaDiscovererEnded )
return false;
libvlc_event_attach( get(), type, &handleMediaDiscovererEvent, cb );
libvlc_event_attach( *this,type, &handleMediaDiscovererEvent, cb );
return true;
}
......@@ -185,38 +185,38 @@ class VLCPP_API EventManager : public Internal<libvlc_event_manager_t>
return false;
if ( type < libvlc_VlmMediaAdded || type > libvlc_VlmMediaInstanceStatusError )
return false;
libvlc_event_attach( get(), type, &handleVLMEvent, cb );
libvlc_event_attach( *this,type, &handleVLMEvent, cb );
return true;
}
void detach( libvlc_event_type_t type, IMediaEventCb* cb )
{
libvlc_event_detach( get(), type, &handleMediaEvent, cb );
libvlc_event_detach( *this,type, &handleMediaEvent, cb );
}
void detach( libvlc_event_type_t type, IMediaPlayerEventCb* cb )
{
libvlc_event_detach( get(), type, &handleMediaPlayerEvent, cb );
libvlc_event_detach( *this,type, &handleMediaPlayerEvent, cb );
}
void detach( libvlc_event_type_t type, IMediaListEventCb* cb )
{
libvlc_event_detach( get(), type, &handleMediaListEvent, cb );
libvlc_event_detach( *this,type, &handleMediaListEvent, cb );
}
void detach( libvlc_event_type_t type, IMediaListPlayerEventCb* cb )
{
libvlc_event_detach( get(), type, &handleMediaListPlayerEvent, cb );
libvlc_event_detach( *this,type, &handleMediaListPlayerEvent, cb );
}
void detach( libvlc_event_type_t type, IMediaDiscovererEventCb* cb )
{
libvlc_event_detach( get(), type, &handleMediaDiscovererEvent, cb );
libvlc_event_detach( *this,type, &handleMediaDiscovererEvent, cb );
}
void detach( libvlc_event_type_t type, IVLMEventCb* cb )
{
libvlc_event_detach( get(), type, &handleVLMEvent, cb );
libvlc_event_detach( *this,type, &handleVLMEvent, cb );
}
private:
......
......@@ -84,7 +84,7 @@ public:
*/
int addIntf(const std::string& name)
{
return libvlc_add_intf( get(), name.c_str() );
return libvlc_add_intf( *this, name.c_str() );
}
/**
......@@ -108,7 +108,7 @@ public:
*/
void setExitHandler(void(*cb)(void *), void * opaque)
{
libvlc_set_exit_handler( get(), cb, opaque );
libvlc_set_exit_handler( *this, cb, opaque );
}
/**
......@@ -124,7 +124,7 @@ public:
*/
void setUserAgent(const std::string& name, const std::string& http)
{
libvlc_set_user_agent( get(), name.c_str(), http.c_str() );
libvlc_set_user_agent( *this, name.c_str(), http.c_str() );
}
/**
......@@ -141,7 +141,7 @@ public:
*/
void setAppId(const std::string& id, const std::string& version, const std::string& icon)
{
libvlc_set_app_id( get(), id.c_str(), version.c_str(), icon.c_str() );
libvlc_set_app_id( *this, id.c_str(), version.c_str(), icon.c_str() );
}
/**
......@@ -155,7 +155,7 @@ public:
*/
void logUnset()
{
libvlc_log_unset( get() );
libvlc_log_unset( *this );
}
/**
......@@ -178,7 +178,7 @@ public:
*/
void logSet(libvlc_log_cb cb, void * data)
{
libvlc_log_set(get(), cb, data);
libvlc_log_set(*this, cb, data);
}
/**
......@@ -191,7 +191,7 @@ public:
*/
void logSetFile(FILE * stream)
{
libvlc_log_set_file( get(), stream );
libvlc_log_set_file( *this, stream );
}
/**
......@@ -207,7 +207,7 @@ public:
*/
std::vector<ModuleDescription> audioFilterList()
{
libvlc_module_description_t* result = libvlc_audio_filter_list_get(get());
libvlc_module_description_t* result = libvlc_audio_filter_list_get(*this);
std::vector<ModuleDescription> res;
if ( result == NULL )
return res;
......@@ -235,7 +235,7 @@ public:
*/
std::vector<ModuleDescription> videoFilterList()
{
libvlc_module_description_t* result = libvlc_video_filter_list_get(get());
libvlc_module_description_t* result = libvlc_video_filter_list_get(*this);
std::vector<ModuleDescription> res;
if ( result == NULL )
return res;
......@@ -260,7 +260,7 @@ public:
*/
std::vector<AudioOutputDescription> audioOutputList()
{
libvlc_audio_output_t* result = libvlc_audio_output_list_get(get());
libvlc_audio_output_t* result = libvlc_audio_output_list_get(*this);
std::vector<AudioOutputDescription> res;
if ( result == NULL )
return res;
......@@ -298,7 +298,7 @@ public:
*/
std::vector<AudioOutputDeviceDescription> audioOutputDeviceList(const std::string& aout)
{
libvlc_audio_output_device_t* devices = libvlc_audio_output_device_list_get( get(), aout.c_str() );
libvlc_audio_output_device_t* devices = libvlc_audio_output_device_list_get( *this, aout.c_str() );
std::vector<AudioOutputDeviceDescription> res;
if ( devices == NULL )
return res;
......
......@@ -38,17 +38,24 @@ class Internal
public:
using InternalType = T;
using InternalPtr = T*;
using Pointer = std::unique_ptr<T, Releaser>;
using Pointer = std::shared_ptr<T>;
InternalPtr get() const { return m_obj.get(); }
InternalPtr get() { return m_obj.get(); }
bool isValid() const { return (bool)m_obj; }
operator T*() const { return m_obj.get(); }
protected:
Internal() = default;
Internal( InternalPtr obj, Releaser releaser )
: m_obj{ obj, releaser }
{
if ( obj == nullptr )
throw std::runtime_error("Wrapping a NULL instance");
}
Internal(Releaser releaser)
: m_obj{ nullptr, releaser }
{
......
......@@ -34,6 +34,8 @@ namespace VLC
class MediaPlayer;
class EventManager;
class Instance;
class MediaList;
class VLCPP_API Media : public Internal<libvlc_media_t>
{
......@@ -67,27 +69,27 @@ public:
* @param mrl A path, location, or node name, depending on the 3rd parameter
* @param type The type of the 2nd argument. \sa{FromType}
*/
Media(InstancePtr instance, const std::string& mrl, FromType type)
Media(Instance& instance, const std::string& mrl, FromType type)
: Internal{ libvlc_media_release }
{
InternalPtr ptr = nullptr;
switch (type)
{
case FromLocation:
ptr = libvlc_media_new_location( instance->get(), mrl.c_str() );
ptr = libvlc_media_new_location( getInternalPtr<libvlc_instance_t>( instance ), mrl.c_str() );
break;
case FromPath:
ptr = libvlc_media_new_path( instance->get(), mrl.c_str() );
ptr = libvlc_media_new_path( getInternalPtr<libvlc_instance_t>( instance ), mrl.c_str() );
break;
case AsNode:
ptr = libvlc_media_new_as_node( instance->get(), mrl.c_str() );
ptr = libvlc_media_new_as_node( getInternalPtr<libvlc_instance_t>( instance ), mrl.c_str() );
break;
default:
break;
}
if ( ptr == nullptr )
throw std::runtime_error("Failed to construct a media");
m_obj.reset( ptr );
m_obj.reset( ptr, libvlc_media_release );
}
/**
......@@ -110,8 +112,9 @@ public:
* \param fd open file descriptor
* \return the newly created media or NULL on error
*/
Media(InstancePtr instance, int fd)
: Internal { libvlc_media_new_fd( instance->get(), fd ), libvlc_media_release }
Media(Instance& instance, int fd)
: Internal { libvlc_media_new_fd( getInternalPtr<libvlc_instance_t>( instance ), fd ),
libvlc_media_release }
{
}
......@@ -123,13 +126,13 @@ public:
* \param p_ml a media list instance
* \return media instance
*/
Media(MediaListPtr list)
Media(MediaList& list)
: Internal{ libvlc_media_list_media( getInternalPtr<libvlc_media_list_t>( list ) ),
libvlc_media_release }
{
}
explicit Media(Internal::InternalPtr ptr, bool incrementRefCount)
explicit Media( Internal::InternalPtr ptr, bool incrementRefCount)
: Internal{ ptr, libvlc_media_release }
{
if (incrementRefCount)
......@@ -166,7 +169,7 @@ public:
*/
void addOption(const std::string& psz_options)
{
libvlc_media_add_option(get(), psz_options.c_str());
libvlc_media_add_option(*this,psz_options.c_str());
}
/**
......@@ -188,7 +191,7 @@ public:
*/
void addOptionFlag(const std::string& psz_options, unsigned i_flags)
{
libvlc_media_add_option_flag(get(), psz_options.c_str(), i_flags);
libvlc_media_add_option_flag(*this,psz_options.c_str(), i_flags);
}
/**
......@@ -198,7 +201,7 @@ public:
*/
std::string mrl()
{
char* c_result = libvlc_media_get_mrl(get());
char* c_result = libvlc_media_get_mrl(*this);
if ( c_result == NULL )
return std::string();
std::string result = c_result;
......@@ -211,7 +214,7 @@ public:
*/
MediaPtr duplicate()
{
InternalPtr obj = libvlc_media_duplicate(get());
auto obj = libvlc_media_duplicate(*this);
return std::make_shared<Media>( obj, false );
}
......@@ -237,7 +240,7 @@ public:
*/
std::string meta(libvlc_meta_t e_meta)
{
char* c_result = libvlc_media_get_meta(get(), e_meta);
char* c_result = libvlc_media_get_meta(*this, e_meta);
if ( c_result == NULL )
return std::string();
std::string result = c_result;
......@@ -255,7 +258,7 @@ public:
*/
void setMeta(libvlc_meta_t e_meta, const std::string& psz_value)
{
libvlc_media_set_meta(get(), e_meta, psz_value.c_str());
libvlc_media_set_meta(*this, e_meta, psz_value.c_str());
}
......@@ -266,7 +269,7 @@ public:
*/
int saveMeta()
{
return libvlc_media_save_meta(get());
return libvlc_media_save_meta(*this);
}
/**
......@@ -281,7 +284,7 @@ public:
*/
libvlc_state_t state()
{
return libvlc_media_get_state(get());
return libvlc_media_get_state(*this);
}
/**
......@@ -294,7 +297,7 @@ public:
*/
bool stats(libvlc_media_stats_t * p_stats)
{
return libvlc_media_get_stats(get(), p_stats);
return libvlc_media_get_stats(*this,p_stats);
}
/**
......@@ -307,7 +310,7 @@ public:
{
if ( m_eventManager == NULL )
{
libvlc_event_manager_t* obj = libvlc_media_event_manager(get());
libvlc_event_manager_t* obj = libvlc_media_event_manager(*this);
m_eventManager = std::make_shared<EventManager>( obj );
}
return m_eventManager;
......@@ -320,7 +323,7 @@ public:
*/
libvlc_time_t duration()
{
return libvlc_media_get_duration(get());
return libvlc_media_get_duration(*this);
}
/**
......@@ -337,7 +340,7 @@ public:
*/
void parse()
{
libvlc_media_parse(get());
libvlc_media_parse(*this);
}
/**
......@@ -360,7 +363,7 @@ public:
*/
void parseAsync()
{
libvlc_media_parse_async(get());
libvlc_media_parse_async(*this);
}
/**
......@@ -373,7 +376,7 @@ public:
*/
bool isParsed()
{
return libvlc_media_is_parsed(get());
return libvlc_media_is_parsed(*this);
}
/**
......@@ -385,7 +388,7 @@ public:
*/
void setUserData(void * p_new_user_data)
{
libvlc_media_set_user_data(get(), p_new_user_data);
libvlc_media_set_user_data(*this, p_new_user_data);
}
/**
......@@ -395,7 +398,7 @@ public:
*/
void* userData()
{
return libvlc_media_get_user_data(get());
return libvlc_media_get_user_data(*this);
}
/**
......@@ -412,7 +415,7 @@ public:
std::vector<MediaTrack> tracks()
{
libvlc_media_track_t** tracks;
uint32_t nbTracks = libvlc_media_tracks_get(get(), &tracks);
uint32_t nbTracks = libvlc_media_tracks_get(*this, &tracks);
std::vector<MediaTrack> res;
if ( nbTracks == 0 )
......@@ -434,7 +437,7 @@ private:
void retain()
{
if ( isValid() )
libvlc_media_retain(get());
libvlc_media_retain(*this);
}
......
......@@ -32,6 +32,7 @@ namespace VLC
{
class EventManager;
class Instance;
class VLCPP_API MediaDiscoverer : public Internal<libvlc_media_discoverer_t>
{
......@@ -46,8 +47,8 @@ public:
* \warning This is returned as a pointer, as this is not refcounter by VLC, and is
* fairly expensive to instantiate.
*/
MediaDiscoverer(InstancePtr inst, const std::string& name)
: Internal{ libvlc_media_discoverer_new_from_name(inst->get(), name.c_str()),
MediaDiscoverer(Instance& inst, const std::string& name)
: Internal{ libvlc_media_discoverer_new_from_name(getInternalPtr<libvlc_instance_t>( inst ), name.c_str()),
libvlc_media_discoverer_release }
{
}
......@@ -59,7 +60,7 @@ public:
*/
std::string localizedName()
{
char* c_result = libvlc_media_discoverer_localized_name(get());
char* c_result = libvlc_media_discoverer_localized_name(*this);
if ( c_result == NULL )
return std::string();
std::string result = c_result;
......@@ -76,7 +77,7 @@ public:
{
if ( m_eventManager )
{
libvlc_event_manager_t* obj = libvlc_media_discoverer_event_manager( get() );
libvlc_event_manager_t* obj = libvlc_media_discoverer_event_manager( *this );
m_eventManager = std::make_shared<EventManager>( obj );
}
return m_eventManager;
......@@ -89,7 +90,7 @@ public:
*/
bool isRunning()
{
return libvlc_media_discoverer_is_running(get());
return libvlc_media_discoverer_is_running(*this);
}
private:
......
......@@ -38,8 +38,9 @@ public:
*
* \param p_instance the libvlc instance
*/
MediaLibrary(InstancePtr instance)
: Internal{ libvlc_media_library_new( instance->get() ), libvlc_media_library_release }
MediaLibrary(Instance& instance)
: Internal{ libvlc_media_library_new( getInternalPtr<libvlc_instance_t>( instance ) ),
libvlc_media_library_release }
{
}
......@@ -60,7 +61,7 @@ public:
*/
int load()
{
return libvlc_media_library_load(get());
return libvlc_media_library_load(*this);
}
};
......
......@@ -29,7 +29,10 @@
namespace VLC
{
class Media;
class EventManager;
class MediaDiscoverer;
class MediaLibrary;
class VLCPP_API MediaList : public Internal<libvlc_media_list_t>
{
......@@ -52,8 +55,8 @@ public:
*
* \param p_md media descriptor object
*/
MediaList(MediaPtr md)
: Internal{ libvlc_media_subitems( md->get() ), libvlc_media_list_release }
MediaList(Media& md)
: Internal{ libvlc_media_subitems( getInternalPtr<libvlc_media_t>( md ) ), libvlc_media_list_release }
{
}
......@@ -63,8 +66,9 @@ public:
*
* \param p_mdis media service discover object
*/
MediaList(MediaDiscovererPtr mdis)
: Internal{ libvlc_media_discoverer_media_list( mdis->get() ), libvlc_media_list_release }
MediaList(MediaDiscoverer& mdis)
: Internal{ libvlc_media_discoverer_media_list( getInternalPtr<libvlc_media_discoverer_t>( mdis ) ),
libvlc_media_list_release }
{
}
......@@ -74,8 +78,8 @@ public:
*
* \param p_mlib media library object
*/
MediaList(MediaLibraryPtr mlib )
: Internal{ libvlc_media_library_media_list( mlib->get() ), libvlc_media_list_release }
MediaList(MediaLibrary& mlib )
: Internal{ libvlc_media_library_media_list( getInternalPtr<libvlc_media_library_t>( mlib ) ), libvlc_media_list_release }
{
}
......@@ -86,8 +90,9 @@ public:
*
* \param p_instance libvlc instance
*/
MediaList(InstancePtr instance)
: Internal{ libvlc_media_list_new( instance->get() ), libvlc_media_list_release }
MediaList(Instance& instance)
: Internal{ libvlc_media_list_new( getInternalPtr<libvlc_instance_t>( instance ) ),
libvlc_media_list_release }
{
}
......@@ -98,9 +103,9 @@ public:
*
* \param p_md media instance to add
*/
void setMedia(MediaPtr md)
void setMedia(Media& md)
{
libvlc_media_list_set_media( get(), md->get() );
libvlc_media_list_set_media( *this, getInternalPtr<libvlc_media_t>( md ) );
}
/**
......@@ -111,9 +116,9 @@ public:
*
* \return 0 on success, -1 if the media list is read-only
*/
int addMedia(MediaPtr p_md)
int addMedia(Media& md)
{
return libvlc_media_list_add_media( get(), p_md->get() );
return libvlc_media_list_add_media( *this, getInternalPtr<libvlc_media_t>( md ) );
}
/**
......@@ -126,9 +131,9 @@ public:
*
* \return 0 on success, -1 if the media list is read-only
*/
int insertMedia(MediaPtr md, int pos)
int insertMedia(Media& md, int pos)
{
return libvlc_media_list_insert_media( get(), md->get(), pos );
return libvlc_media_list_insert_media( *this, getInternalPtr<libvlc_media_t>( md ), pos );
}
/**
......@@ -142,7 +147,7 @@ public:
*/
int removeIndex(int i_pos)
{
return libvlc_media_list_remove_index(get(), i_pos);
return libvlc_media_list_remove_index(*this,i_pos);
}
/**
......@@ -153,7 +158,7 @@ public:
*/
int count()
{
return libvlc_media_list_count(get());
return libvlc_media_list_count(*this);
}
/**
......@@ -166,10 +171,10 @@ public:
* case of success, Media::retain() is called to increase the refcount on
* the media.
*/
Media itemAtIndex(int i_pos)
MediaPtr itemAtIndex(int i_pos)
{
Media::InternalPtr ptr = libvlc_media_list_item_at_index(get(), i_pos);
return Media( ptr, false );
auto ptr = libvlc_media_list_item_at_index(*this,i_pos);
return std::make_shared<Media>( ptr, false );
}
/**
......@@ -181,9 +186,9 @@ public:
*
* \return position of media instance or -1 if media not found
*/
int indexOfItem(MediaPtr md)
int indexOfItem(Media& md)
{
return libvlc_media_list_index_of_item( get(), md->get() );
return libvlc_media_list_index_of_item( *this, getInternalPtr<libvlc_media_t>( md ) );
}
/**
......@@ -194,7 +199,7 @@ public:
*/
bool isReadonly()
{
return libvlc_media_list_is_readonly(get());
return libvlc_media_list_is_readonly(*this);
}
/**
......@@ -202,7 +207,7 @@ public:
*/
void lock()
{
libvlc_media_list_lock(get());
libvlc_media_list_lock(*this);
}
/**
......@@ -211,7 +216,7 @@ public:
*/
void unlock()
{
libvlc_media_list_unlock(get());
libvlc_media_list_unlock(*this);
}
/**
......@@ -224,7 +229,7 @@ public:
{
if ( m_eventManager )
{
libvlc_event_manager_t* obj = libvlc_media_list_event_manager( get() );
libvlc_event_manager_t* obj = libvlc_media_list_event_manager( *this );
m_eventManager = std::make_shared<EventManager>( obj );
}
return m_eventManager;
......
......@@ -53,8 +53,9 @@ public:
*
* \param p_instance libvlc instance
*/
MediaListPlayer(InstancePtr instance)
: Internal{ libvlc_media_list_player_new( instance->get() ), libvlc_media_list_player_release }
MediaListPlayer(Instance& instance)
: Internal{ libvlc_media_list_player_new( getInternalPtr<libvlc_instance_t>( instance ) ),
libvlc_media_list_player_release }
{
}
......@@ -68,7 +69,7 @@ public:
{
if ( m_eventManager )
{
libvlc_event_manager_t* obj = libvlc_media_list_player_event_manager(get());
libvlc_event_manager_t* obj = libvlc_media_list_player_event_manager(*this);
m_eventManager = std::make_shared<EventManager>( obj );
}
return m_eventManager;
......@@ -80,9 +81,9 @@ public:
*
* \param p_mi media player instance
*/
void setMediaPlayer(MediaPlayerPtr mi)
void setMediaPlayer(const MediaPlayer& mi)
{
libvlc_media_list_player_set_media_player( get(),
libvlc_media_list_player_set_media_player( *this,
getInternalPtr<libvlc_media_player_t>( mi ) );
}
......@@ -91,9 +92,9 @@ public:
*
* \param p_mlist list of media
*/
void setMediaList(MediaListPtr mlist)
void setMediaList(const MediaList& mlist)
{
libvlc_media_list_player_set_media_list( get(),
libvlc_media_list_player_set_media_list( *this,
getInternalPtr<libvlc_media_list_t>( mlist ) );
}
......@@ -102,7 +103,7 @@ public:
*/
void play()
{
libvlc_media_list_player_play(get());
libvlc_media_list_player_play(*this);
}
/**
......@@ -110,7 +111,7 @@ public:
*/
void pause()
{
libvlc_media_list_player_pause(get());
libvlc_media_list_player_pause(*this);
}
/**
......@@ -120,7 +121,7 @@ public:
*/