mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-17 13:34:57 +00:00
common LoaderMgr: code refactoring
keep clean & neat thorvg coding style.
This commit is contained in:
parent
e9d1d13edd
commit
200016fea0
2 changed files with 44 additions and 33 deletions
|
@ -25,8 +25,43 @@
|
||||||
#include "tvgSvgLoader.h"
|
#include "tvgSvgLoader.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/************************************************************************/
|
||||||
|
/* Internal Class Implementation */
|
||||||
|
/************************************************************************/
|
||||||
|
|
||||||
static int initCnt = 0;
|
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()
|
bool LoaderMgr::init()
|
||||||
{
|
{
|
||||||
if (initCnt > 0) return true;
|
if (initCnt > 0) return true;
|
||||||
|
@ -37,6 +72,7 @@ bool LoaderMgr::init()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool LoaderMgr::term()
|
bool LoaderMgr::term()
|
||||||
{
|
{
|
||||||
--initCnt;
|
--initCnt;
|
||||||
|
@ -47,47 +83,22 @@ bool LoaderMgr::term()
|
||||||
return true;
|
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> LoaderMgr::loader(const string& path)
|
||||||
{
|
{
|
||||||
unique_ptr<Loader> loader = nullptr;
|
auto loader = find(path);
|
||||||
loader = findLoaderByExt(path);
|
|
||||||
if (loader && loader->open(path.c_str())) {
|
if (loader && loader->open(path.c_str())) return unique_ptr<Loader>(loader);
|
||||||
return loader;
|
|
||||||
}
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unique_ptr<Loader> LoaderMgr::loader(const char* data, uint32_t size)
|
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++) {
|
for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
|
||||||
loader = findLoaderByType(static_cast<FileType>(i));
|
auto loader = find(static_cast<FileType>(i));
|
||||||
if (loader && loader->open(data, size)) {
|
if (loader && loader->open(data, size)) return unique_ptr<Loader>(loader);
|
||||||
return loader;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
#include "tvgLoader.h"
|
#include "tvgLoader.h"
|
||||||
|
|
||||||
enum class FileType { Svg = 0, Unknown = 1};
|
enum class FileType { Svg = 0, Unknown};
|
||||||
|
|
||||||
struct LoaderMgr
|
struct LoaderMgr
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue