2012-12-02 22:14:24 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
2008-03-07 03:32:29 +01:00
|
|
|
//
|
2012-12-02 22:14:24 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2008-03-07 03:32:29 +01:00
|
|
|
//
|
2012-12-02 22:14:24 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2008-03-07 03:32:29 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file factory_manager.h
|
|
|
|
/// @brief Template/base-class for factory classes
|
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2008-03-07 03:32:29 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2010-08-03 00:14:11 +02:00
|
|
|
#include <algorithm>
|
2008-03-07 03:32:29 +01:00
|
|
|
#include <map>
|
2013-06-08 06:19:40 +02:00
|
|
|
#include <memory>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <string>
|
2010-08-02 08:31:38 +02:00
|
|
|
#include <vector>
|
2009-09-11 04:36:34 +02:00
|
|
|
|
2013-10-17 02:56:54 +02:00
|
|
|
template <typename func>
|
2010-08-02 08:31:38 +02:00
|
|
|
class FactoryBase {
|
2008-03-07 03:32:29 +01:00
|
|
|
protected:
|
2012-12-11 19:58:28 +01:00
|
|
|
typedef std::map<std::string, std::pair<bool, func>> map;
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2013-06-08 06:19:40 +02:00
|
|
|
static map& classes() {
|
|
|
|
static map classes;
|
|
|
|
return classes;
|
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
static void DoRegister(func function, std::string name, bool hide, std::vector<std::string> &subtypes) {
|
2013-06-08 06:19:40 +02:00
|
|
|
if (subtypes.empty())
|
|
|
|
classes().insert(std::make_pair(name, std::make_pair(hide, function)));
|
2010-08-02 08:31:38 +02:00
|
|
|
else {
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& subtype : subtypes)
|
2013-06-08 06:19:40 +02:00
|
|
|
classes().insert(std::make_pair(name + '/' + subtype, std::make_pair(hide, function)));
|
2009-05-18 07:42:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-08 06:19:40 +02:00
|
|
|
static func Find(std::string const& name) {
|
2013-10-17 02:56:54 +02:00
|
|
|
auto factory = classes().find(name);
|
2013-06-08 06:19:40 +02:00
|
|
|
return factory != classes().end() ? factory->second.second : nullptr;
|
2010-08-02 08:31:38 +02:00
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
public:
|
|
|
|
static std::vector<std::string> GetClasses(std::string favourite="") {
|
|
|
|
std::vector<std::string> list;
|
|
|
|
std::string cmp;
|
|
|
|
std::transform(favourite.begin(), favourite.end(), favourite.begin(), ::tolower);
|
2013-06-08 06:19:40 +02:00
|
|
|
for (auto const& cls : classes()) {
|
2010-08-02 08:31:38 +02:00
|
|
|
cmp.clear();
|
2012-11-04 04:53:03 +01:00
|
|
|
std::transform(cls.first.begin(), cls.first.end(), std::back_inserter(cmp), ::tolower);
|
2013-06-08 06:19:40 +02:00
|
|
|
if (cmp == favourite)
|
|
|
|
list.insert(list.begin(), cls.first);
|
|
|
|
else if (!cls.second.first)
|
|
|
|
list.push_back(cls.first);
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
return list;
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
};
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2013-10-17 16:23:45 +02:00
|
|
|
template<typename Base, typename Arg1=void, typename Arg2=void>
|
|
|
|
class Factory : public FactoryBase<Base *(*)(Arg1, Arg2)> {
|
|
|
|
typedef Base *(*func)(Arg1, Arg2);
|
|
|
|
|
|
|
|
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>
|
2013-12-12 00:11:06 +01:00
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = {}) {
|
2013-10-17 16:23:45 +02:00
|
|
|
FactoryBase<func>::DoRegister([](Arg1 a1, Arg2 a2) -> Base * { return new T(a1, a2); }, name, hide, subTypes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Base, typename Arg1>
|
|
|
|
class Factory<Base, Arg1, void> : public FactoryBase<Base *(*)(Arg1)> {
|
2013-10-17 02:56:54 +02:00
|
|
|
typedef Base *(*func)(Arg1);
|
2013-06-08 06:19:40 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
public:
|
2013-10-17 02:56:54 +02:00
|
|
|
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;
|
2010-08-02 08:31:38 +02:00
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class T>
|
2013-12-12 00:11:06 +01:00
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = {}) {
|
2013-10-17 02:56:54 +02:00
|
|
|
FactoryBase<func>::DoRegister([](Arg1 a1) -> Base * { return new T(a1); }, name, hide, subTypes);
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
};
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2013-10-17 02:56:54 +02:00
|
|
|
template<typename Base>
|
2013-10-17 16:23:45 +02:00
|
|
|
class Factory<Base, void, void> : public FactoryBase<Base *(*)()> {
|
2013-10-17 02:56:54 +02:00
|
|
|
typedef Base *(*func)();
|
2013-06-08 06:19:40 +02:00
|
|
|
|
2008-03-07 03:32:29 +01:00
|
|
|
public:
|
2013-10-17 02:56:54 +02:00
|
|
|
static std::unique_ptr<Base> Create(std::string const& name) {
|
|
|
|
auto factory = FactoryBase<func>::Find(name);
|
|
|
|
return factory ? std::unique_ptr<Base>(factory()) : nullptr;
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class T>
|
2013-12-12 00:11:06 +01:00
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = {}) {
|
2013-10-17 02:56:54 +02:00
|
|
|
FactoryBase<func>::DoRegister([]() -> Base * { return new T; }, name, hide, subTypes);
|
2010-08-02 08:31:38 +02:00
|
|
|
}
|
|
|
|
};
|