Clean up factory_manager a bit

This commit is contained in:
Thomas Goyne 2013-10-16 17:56:54 -07:00
parent 7bd1da7d45
commit 4116f030af
6 changed files with 29 additions and 38 deletions

View file

@ -27,11 +27,10 @@
#include <string>
#include <vector>
template <class func>
template <typename func>
class FactoryBase {
protected:
typedef std::map<std::string, std::pair<bool, func>> map;
typedef typename map::iterator iterator;
static map& classes() {
static map classes;
@ -48,7 +47,7 @@ protected:
}
static func Find(std::string const& name) {
iterator factory = classes().find(name);
auto factory = classes().find(name);
return factory != classes().end() ? factory->second.second : nullptr;
}
@ -69,42 +68,34 @@ public:
}
};
template<class Base>
class Factory0 : public FactoryBase<std::unique_ptr<Base>(*)()> {
typedef std::unique_ptr<Base>(*func)();
template<class T>
static std::unique_ptr<Base> create() {
return std::unique_ptr<Base>(new T);
}
public:
static std::unique_ptr<Base> Create(std::string const& name) {
func factory = FactoryBase<func>::Find(name);
return factory ? factory() : nullptr;
}
template<class T>
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = std::vector<std::string>()) {
FactoryBase<func>::DoRegister(&Factory0<Base>::template create<T>, name, hide, subTypes);
}
};
template<class Base, class Arg1>
class Factory1 : public FactoryBase<std::unique_ptr<Base>(*)(Arg1)> {
typedef std::unique_ptr<Base>(*func)(Arg1);
template<class T>
static std::unique_ptr<Base> create(Arg1 a1) {
return std::unique_ptr<Base>(new T(a1));
}
template<typename Base, typename Arg1=void>
class Factory : public FactoryBase<Base *(*)(Arg1)> {
typedef Base *(*func)(Arg1);
public:
static std::unique_ptr<Base> Create(std::string const& name, Arg1 a1) {
func factory = FactoryBase<func>::Find(name);
return factory ? factory(a1) : nullptr;
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 = std::vector<std::string>()) {
FactoryBase<func>::DoRegister(&Factory1<Base, Arg1>::template create<T>, name, hide, subTypes);
FactoryBase<func>::DoRegister([](Arg1 a1) -> Base * { return new T(a1); }, name, hide, subTypes);
}
};
template<typename Base>
class Factory<Base, 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 = std::vector<std::string>()) {
FactoryBase<func>::DoRegister([]() -> Base * { return new T; }, name, hide, subTypes);
}
};

View file

@ -57,7 +57,7 @@ public:
virtual void SetEndPosition(int64_t pos)=0;
};
class AudioPlayerFactory : public Factory1<AudioPlayer, AudioProvider*> {
class AudioPlayerFactory : public Factory<AudioPlayer, AudioProvider*> {
public:
static void RegisterProviders();
static std::unique_ptr<AudioPlayer> GetAudioPlayer(AudioProvider *provider);

View file

@ -90,7 +90,7 @@ public:
}
};
class AudioProviderFactory : public Factory1<AudioProvider, agi::fs::path> {
class AudioProviderFactory : public Factory<AudioProvider, agi::fs::path> {
public:
static void RegisterProviders();

View file

@ -23,7 +23,7 @@
namespace agi { class SpellChecker; }
class SpellCheckerFactory : public Factory0<agi::SpellChecker> {
class SpellCheckerFactory : public Factory<agi::SpellChecker> {
public:
static std::unique_ptr<agi::SpellChecker> GetSpellChecker();
static void RegisterProviders();

View file

@ -47,7 +47,7 @@ public:
virtual void DrawSubtitles(VideoFrame &dst,double time)=0;
};
class SubtitlesProviderFactory : public Factory1<SubtitlesProvider, std::string> {
class SubtitlesProviderFactory : public Factory<SubtitlesProvider, std::string> {
public:
static std::unique_ptr<SubtitlesProvider> GetProvider();
static void RegisterProviders();

View file

@ -19,7 +19,7 @@
#include <libaegisub/fs_fwd.h>
class VideoProviderFactory : public Factory1<VideoProvider, agi::fs::path> {
class VideoProviderFactory : public Factory<VideoProvider, agi::fs::path> {
public:
static std::unique_ptr<VideoProvider> GetProvider(agi::fs::path const& video_file);
static void RegisterProviders();