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

Add a FileSystem classes factory, to ease up FS mocking

parent 4832f1e1
......@@ -5,6 +5,7 @@
#include <string>
#include "Types.h"
#include "factory/IFileSystem.h"
class IParserCb
{
......@@ -31,7 +32,14 @@ class IMediaLibrary
{
public:
virtual ~IMediaLibrary() {}
virtual bool initialize( const std::string& dbPath ) = 0;
///
/// \brief initialize Initializes the media library.
/// \param dbPath Path to the database
/// \param fsFactory An instance to a filesystem factory.
/// In case this is a nullptr, a default factory will be used
/// \return true in case of success, false otherwise
///
virtual bool initialize( const std::string& dbPath, std::unique_ptr<factory::IFileSystem> fsFactory = nullptr ) = 0;
/// Adds a stand alone file
virtual FilePtr addFile( const std::string& path ) = 0;
virtual FilePtr file( const std::string& path ) = 0;
......
#pragma once
#include <memory>
namespace fs
{
class IDirectory;
}
namespace factory
{
class IFileSystem
{
public:
virtual ~IFileSystem() = default;
virtual std::unique_ptr<fs::IDirectory> createDirectory( const std::string& path ) = 0;
};
}
......@@ -23,6 +23,8 @@ list(APPEND HEADERS_LIST
${CMAKE_SOURCE_DIR}/include/IVideoTrack.h
${CMAKE_SOURCE_DIR}/include/IMovie.h
${CMAKE_SOURCE_DIR}/include/factory/IFileSystem.h
Cache.h
SqliteTools.h
......@@ -31,6 +33,8 @@ list(APPEND HEADERS_LIST
filesystem/${ARCH_FOLDER}/Directory.h
filesystem/${ARCH_FOLDER}/File.h
factory/FileSystem.h
)
include_directories("${CMAKE_SOURCE_DIR}/include")
......@@ -53,7 +57,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
metadata_services/VLCMetadataService.cpp
filesystem/Factory.cpp
factory/FileSystem.cpp
filesystem/${ARCH_FOLDER}/Directory.cpp
filesystem/${ARCH_FOLDER}/File.cpp
)
......
......@@ -19,6 +19,8 @@
#include "filesystem/IDirectory.h"
#include "filesystem/IFile.h"
#include "factory/FileSystem.h"
MediaLibrary::MediaLibrary()
: m_parser( new Parser )
{
......@@ -38,8 +40,13 @@ MediaLibrary::~MediaLibrary()
AudioTrack::clear();
}
bool MediaLibrary::initialize(const std::string& dbPath)
bool MediaLibrary::initialize( const std::string& dbPath, std::unique_ptr<factory::IFileSystem> fsFactory )
{
if ( fsFactory != nullptr )
m_fsFactory = std::move( fsFactory );
else
m_fsFactory.reset( new factory::FileSystemDefaultFactory );
sqlite3* dbConnection;
int res = sqlite3_open( dbPath.c_str(), &dbConnection );
if ( res != SQLITE_OK )
......@@ -84,7 +91,7 @@ FolderPtr MediaLibrary::addFolder( const std::string& path )
try
{
dir = fs::createDirectory( path );
dir = m_fsFactory->createDirectory( path );
}
catch ( std::runtime_error& )
{
......
......@@ -12,7 +12,7 @@ class MediaLibrary : public IMediaLibrary
public:
MediaLibrary();
~MediaLibrary();
virtual bool initialize( const std::string& dbPath );
virtual bool initialize( const std::string& dbPath, std::unique_ptr<factory::IFileSystem> fsFactory );
virtual std::vector<FilePtr> files();
virtual FilePtr file( const std::string& path );
......@@ -43,5 +43,6 @@ class MediaLibrary : public IMediaLibrary
private:
std::shared_ptr<sqlite3> m_dbConnection;
std::unique_ptr<Parser> m_parser;
std::unique_ptr<factory::IFileSystem> m_fsFactory;
};
#endif // MEDIALIBRARY_H
#include "IDirectory.h"
#include "factory/FileSystem.h"
#include "filesystem/IDirectory.h"
#if defined(__linux__) || defined(__APPLE__)
# include "unix/Directory.h"
# include "filesystem/unix/Directory.h"
#else
# error No filesystem implementation for this architecture
#endif
std::unique_ptr<fs::IDirectory> fs::createDirectory( const std::string& path )
namespace factory
{
std::unique_ptr<fs::IDirectory> FileSystemDefaultFactory::createDirectory( const std::string& path )
{
return std::unique_ptr<fs::IDirectory>( new fs::Directory( path ) );
}
}
#pragma once
#include "factory/IFileSystem.h"
namespace factory
{
class FileSystemDefaultFactory : public IFileSystem
{
public:
virtual std::unique_ptr<fs::IDirectory> createDirectory( const std::string& path ) override;
};
}
......@@ -15,6 +15,4 @@ namespace fs
virtual const std::string& path() const = 0;
virtual std::vector<std::unique_ptr<IFile>> files() const = 0;
};
std::unique_ptr<IDirectory> createDirectory( const std::string& path );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment