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>
|
2010-08-02 08:31:38 +02:00
|
|
|
#include <cctype>
|
2008-03-07 03:32:29 +01:00
|
|
|
#include <map>
|
2010-08-02 08:31:38 +02:00
|
|
|
#include <vector>
|
2009-09-11 04:36:34 +02:00
|
|
|
|
|
|
|
#include <wx/string.h>
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template <class func>
|
|
|
|
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;
|
2010-08-02 08:31:38 +02:00
|
|
|
typedef typename map::iterator iterator;
|
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
|
|
|
static map *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) {
|
|
|
|
if (!classes) classes = new 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
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
if (subtypes.empty()) {
|
|
|
|
classes->insert(std::make_pair(name, std::make_pair(hide, function)));
|
|
|
|
}
|
|
|
|
else {
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto const& subtype : subtypes)
|
|
|
|
classes->insert(std::make_pair(name + '/' + subtype, std::make_pair(hide, function)));
|
2009-05-18 07:42:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
static func Find(std::string name) {
|
2012-11-13 17:51:01 +01:00
|
|
|
if (!classes) return nullptr;
|
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
|
|
|
iterator factory = classes->find(name);
|
|
|
|
if (factory != classes->end()) return factory->second.second;
|
2012-11-13 17:51:01 +01:00
|
|
|
return 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 void Clear() {
|
|
|
|
delete classes;
|
|
|
|
}
|
|
|
|
static std::vector<std::string> GetClasses(std::string favourite="") {
|
|
|
|
std::vector<std::string> list;
|
|
|
|
if (!classes) return list;
|
|
|
|
std::string cmp;
|
|
|
|
std::transform(favourite.begin(), favourite.end(), favourite.begin(), ::tolower);
|
2012-11-04 04:53:03 +01: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);
|
|
|
|
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
|
|
|
virtual ~FactoryBase() {
|
|
|
|
delete classes;
|
|
|
|
}
|
|
|
|
};
|
2008-03-07 03:32:29 +01:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class Base>
|
|
|
|
class Factory0 : public FactoryBase<Base *(*)()> {
|
|
|
|
typedef Base *(*func)();
|
|
|
|
template<class T>
|
|
|
|
static Base* create() {
|
|
|
|
return new T;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
static Base* Create(std::string name) {
|
|
|
|
func factory = FactoryBase<func>::Find(name);
|
|
|
|
if (factory) {
|
|
|
|
return factory();
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
2010-08-02 08:31:38 +02:00
|
|
|
else {
|
2012-11-13 17:51:01 +01:00
|
|
|
return nullptr;
|
2008-03-07 03:32:29 +01:00
|
|
|
}
|
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>
|
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = std::vector<std::string>()) {
|
2011-11-07 07:18:34 +01:00
|
|
|
FactoryBase<func>::DoRegister(&Factory0<Base>::template create<T>, 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
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
template<class Base, class Arg1>
|
|
|
|
class Factory1 : public FactoryBase<Base *(*)(Arg1)> {
|
|
|
|
typedef Base *(*func)(Arg1);
|
|
|
|
template<class T>
|
|
|
|
static Base* create(Arg1 a1) {
|
|
|
|
return new T(a1);
|
|
|
|
}
|
2008-03-07 03:32:29 +01:00
|
|
|
public:
|
2010-08-02 08:31:38 +02:00
|
|
|
static Base* Create(std::string name, Arg1 a1) {
|
|
|
|
func factory = FactoryBase<func>::Find(name);
|
|
|
|
if (factory) {
|
|
|
|
return factory(a1);
|
|
|
|
}
|
|
|
|
else {
|
2012-11-13 17:51:01 +01:00
|
|
|
return 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>
|
|
|
|
static void Register(std::string name, bool hide = false, std::vector<std::string> subTypes = std::vector<std::string>()) {
|
2011-11-07 07:18:34 +01:00
|
|
|
FactoryBase<func>::DoRegister(&Factory1<Base, Arg1>::template create<T>, name, hide, subTypes);
|
2010-08-02 08:31:38 +02:00
|
|
|
}
|
|
|
|
};
|