forked from mia/Aegisub
Convert Factory to variadic templates
This commit is contained in:
parent
a8492fc02d
commit
3747d436cc
1 changed files with 21 additions and 63 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
||||||
//
|
//
|
||||||
// Permission to use, copy, modify, and distribute this software for any
|
// Permission to use, copy, modify, and distribute this software for any
|
||||||
// purpose with or without fee is hereby granted, provided that the above
|
// purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -27,9 +27,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
template <typename func>
|
namespace agi { class ProgressSink; }
|
||||||
class FactoryBase {
|
|
||||||
protected:
|
template<typename Base, typename... Args>
|
||||||
|
class Factory {
|
||||||
|
typedef Base *(*func)(Args const&...);
|
||||||
typedef std::map<std::string, std::pair<bool, func>> map;
|
typedef std::map<std::string, std::pair<bool, func>> map;
|
||||||
|
|
||||||
static map& classes() {
|
static map& classes() {
|
||||||
|
@ -37,81 +39,37 @@ protected:
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DoRegister(func function, std::string name, bool hide, std::vector<std::string> &subtypes) {
|
|
||||||
if (subtypes.empty())
|
|
||||||
classes().insert(std::make_pair(name, std::make_pair(hide, function)));
|
|
||||||
else {
|
|
||||||
for (auto const& subtype : subtypes)
|
|
||||||
classes().insert(std::make_pair(name + '/' + subtype, std::make_pair(hide, function)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static func Find(std::string const& name) {
|
|
||||||
auto factory = classes().find(name);
|
|
||||||
return factory != classes().end() ? factory->second.second : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::vector<std::string> GetClasses(std::string favourite="") {
|
static std::vector<std::string> GetClasses(std::string favorite="") {
|
||||||
std::vector<std::string> list;
|
std::vector<std::string> list;
|
||||||
std::string cmp;
|
std::string cmp;
|
||||||
std::transform(favourite.begin(), favourite.end(), favourite.begin(), ::tolower);
|
for (auto& c : favorite) c = ::tolower(c);
|
||||||
for (auto const& cls : classes()) {
|
for (auto const& cls : classes()) {
|
||||||
cmp.clear();
|
cmp.clear();
|
||||||
std::transform(cls.first.begin(), cls.first.end(), std::back_inserter(cmp), ::tolower);
|
std::transform(cls.first.begin(), cls.first.end(), std::back_inserter(cmp), ::tolower);
|
||||||
if (cmp == favourite)
|
if (cmp == favorite)
|
||||||
list.insert(list.begin(), cls.first);
|
list.insert(list.begin(), cls.first);
|
||||||
else if (!cls.second.first)
|
else if (!cls.second.first)
|
||||||
list.push_back(cls.first);
|
list.push_back(cls.first);
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Base, typename Arg1=void, typename Arg2=void>
|
static std::unique_ptr<Base> Create(std::string const& name, Args const&... args) {
|
||||||
class Factory : public FactoryBase<Base *(*)(Arg1, Arg2)> {
|
auto factory = classes().find(name);
|
||||||
typedef Base *(*func)(Arg1, Arg2);
|
if (factory == classes().end()) return nullptr;
|
||||||
|
return std::unique_ptr<Base>(factory->second.second(args...));
|
||||||
public:
|
|
||||||
static std::unique_ptr<Base> Create(std::string const& name, Arg1 a1, Arg2 a2) {
|
|
||||||
auto factory = FactoryBase<func>::Find(name);
|
|
||||||
return factory ? std::unique_ptr<Base>(factory(a1, a2)) : nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = {}) {
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subtypes = {}) {
|
||||||
FactoryBase<func>::DoRegister([](Arg1 a1, Arg2 a2) -> Base * { return new T(a1, a2); }, name, hide, subTypes);
|
func factory = [](Args const&... args) -> Base * { return new T(args...); };
|
||||||
|
if (subtypes.empty())
|
||||||
|
classes().insert(std::make_pair(name, std::make_pair(hide, factory)));
|
||||||
|
else {
|
||||||
|
for (auto const& subtype : subtypes)
|
||||||
|
classes().insert(std::make_pair(name + '/' + subtype, std::make_pair(hide, factory)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Base, typename Arg1>
|
|
||||||
class Factory<Base, Arg1, void> : public FactoryBase<Base *(*)(Arg1)> {
|
|
||||||
typedef Base *(*func)(Arg1);
|
|
||||||
|
|
||||||
public:
|
|
||||||
static std::unique_ptr<Base> Create(std::string const& name, Arg1 a1) {
|
|
||||||
auto factory = FactoryBase<func>::Find(name);
|
|
||||||
return factory ? std::unique_ptr<Base>(factory(a1)) : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = {}) {
|
|
||||||
FactoryBase<func>::DoRegister([](Arg1 a1) -> Base * { return new T(a1); }, name, hide, subTypes);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Base>
|
|
||||||
class Factory<Base, void, void> : public FactoryBase<Base *(*)()> {
|
|
||||||
typedef Base *(*func)();
|
|
||||||
|
|
||||||
public:
|
|
||||||
static std::unique_ptr<Base> Create(std::string const& name) {
|
|
||||||
auto factory = FactoryBase<func>::Find(name);
|
|
||||||
return factory ? std::unique_ptr<Base>(factory()) : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = {}) {
|
|
||||||
FactoryBase<func>::DoRegister([]() -> Base * { return new T; }, name, hide, subTypes);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue