common LoaderMgr: code refactoring

keep clean & neat thorvg coding style.
This commit is contained in:
Hermet Park 2020-11-03 13:40:47 +09:00 committed by Hermet Park
parent e9d1d13edd
commit 200016fea0
2 changed files with 44 additions and 33 deletions

View file

@ -25,8 +25,43 @@
#include "tvgSvgLoader.h"
#endif
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
static int initCnt = 0;
static Loader* find(FileType type)
{
switch(type) {
case FileType::Svg: {
#ifdef THORVG_SVG_LOADER_SUPPORT
return new SvgLoader;
#endif
break;
}
default: {
break;
}
}
return nullptr;
}
static Loader* find(const string& path)
{
auto ext = path.substr(path.find_last_of(".") + 1);
if (!ext.compare("svg")) return find(FileType::Svg);
return nullptr;
}
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
bool LoaderMgr::init()
{
if (initCnt > 0) return true;
@ -37,6 +72,7 @@ bool LoaderMgr::init()
return true;
}
bool LoaderMgr::term()
{
--initCnt;
@ -47,47 +83,22 @@ bool LoaderMgr::term()
return true;
}
unique_ptr<Loader> findLoaderByType(FileType type)
{
switch (type) {
case FileType::Svg :
#ifdef THORVG_SVG_LOADER_SUPPORT
return unique_ptr<SvgLoader>(new SvgLoader);
#endif
break;
default:
break;
}
return nullptr;
}
unique_ptr<Loader> findLoaderByExt(const string& path)
{
string ext = path.substr(path.find_last_of(".") + 1);
if (!ext.compare("svg")) {
return findLoaderByType(FileType::Svg);
}
return nullptr;
}
unique_ptr<Loader> LoaderMgr::loader(const string& path)
{
unique_ptr<Loader> loader = nullptr;
loader = findLoaderByExt(path);
if (loader && loader->open(path.c_str())) {
return loader;
}
auto loader = find(path);
if (loader && loader->open(path.c_str())) return unique_ptr<Loader>(loader);
return nullptr;
}
unique_ptr<Loader> LoaderMgr::loader(const char* data, uint32_t size)
{
unique_ptr<Loader> loader = nullptr;
for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
loader = findLoaderByType(static_cast<FileType>(i));
if (loader && loader->open(data, size)) {
return loader;
}
auto loader = find(static_cast<FileType>(i));
if (loader && loader->open(data, size)) return unique_ptr<Loader>(loader);
}
return nullptr;
}

View file

@ -24,7 +24,7 @@
#include "tvgLoader.h"
enum class FileType { Svg = 0, Unknown = 1};
enum class FileType { Svg = 0, Unknown};
struct LoaderMgr
{